sendAudiotoText method

Future<String> sendAudiotoText(
  1. String audioPath, {
  2. String? prompt,
  3. Map<String, dynamic>? extraOptions,
})

Implementation

Future<String> sendAudiotoText(
  String audioPath, {
  String? prompt,
  Map<String, dynamic>? extraOptions,
}) async {
  final url = Uri.parse('https://api.openai.com/v1/audio/transcriptions');
  var request = http.MultipartRequest('POST', url)
    ..headers['Authorization'] = 'Bearer $apiKey';
  request.files.add(await http.MultipartFile.fromPath('file', audioPath));
  if (extraOptions != null) {
    extraOptions.forEach((key, value) {
      request.fields[key] = value.toString();
    });
  }
  final streamedResponse = await request.send();
  final response = await http.Response.fromStream(streamedResponse);
  if (response.statusCode == 200) {
    return response.body;
  } else {
    throw APIException(
      'OpenAI audio error: ${response.statusCode} ${response.body}',
      statusCode: response.statusCode,
    );
  }
}