transcribeAudio static method

Future<(GroqAudioResponse, GroqRateLimitInformation)> transcribeAudio({
  1. required String apiKey,
  2. required String filePath,
  3. required String modelId,
  4. required Map<String, String> optionalParameters,
})

transcribes the audio file at the given path using the model with the given model id

Implementation

static Future<(GroqAudioResponse, GroqRateLimitInformation)> transcribeAudio({
  required String apiKey,
  required String filePath,
  required String modelId,
  required Map<String, String> optionalParameters,
}) async {
  final request =
      http.MultipartRequest('POST', Uri.parse(_getAudioTranscriptionUrl));

  request.headers['Authorization'] = 'Bearer $apiKey';
  request.headers['Content-Type'] = 'multipart/form-data';
  request.files.add(await http.MultipartFile.fromPath('file', filePath));
  request.fields['model'] = modelId;

  // Add optional fields from the map
  optionalParameters.forEach((key, value) {
    request.fields[key] = value;
  });

  final response = await request.send();
  final responseBody = await response.stream.bytesToString();

  final jsonBody = json.decode(responseBody);
  if (response.statusCode == 200) {
    final audioResponse = GroqParser.audioResponseFromJson(jsonBody);
    // final usage =
    //     GroqParser.groqUsageFromAudioJson(jsonBody['x_groq']['usage']);
    final rateLimitInfo =
        GroqParser.rateLimitInformationFromHeaders(response.headers);
    return (audioResponse, rateLimitInfo);
  } else {
    throw GroqException(
        statusCode: response.statusCode, error: GroqError.fromJson(jsonBody));
  }
}