createSpeech method

Future<Uint8List> createSpeech({
  1. required String apiKey,
  2. required String organizationId,
  3. required GptSpeechRequest request,
})

Generates audio from the input text.

Implementation

Future<Uint8List> createSpeech({
  required String apiKey,
  required String organizationId,
  required GptSpeechRequest request,
}) async {
  Map<String, String> headers = {
    "Authorization": "Bearer $apiKey",
    "OpenAI-Organization": organizationId,
    'Content-Type': 'application/json',
    "Access-Control-Allow-Origin": "*", // Required for CORS support to work
  };

  Uri endpoint;
  if (secure) {
    endpoint = Uri.https(baseUrl, "/v1/audio/speech");
  } else {
    endpoint = Uri.http(baseUrl, "/v1/audio/speech");
  }

  var response = await http.post(endpoint,
      headers: headers, body: jsonEncode(request.toJson()));

  if (response.statusCode == 200 || response.statusCode == 201) {
    return response.bodyBytes;
  } else {
    var error = ServerError.fromJson(jsonDecode(response.body));
    throw AudioException(
        statusCode: response.statusCode, message: error.message);
  }
}