Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of getting bytes instead of saving directly to file for speech #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ packages:
path: ".."
relative: true
source: path
version: "4.1.4"
version: "5.1.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was automatic, please revert if incorrect

dart_style:
dependency: transitive
description:
Expand Down
9 changes: 9 additions & 0 deletions lib/src/core/base/audio/interfaces.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:typed_data';

import '../../../../dart_openai.dart';

Expand All @@ -13,6 +14,14 @@ abstract class CreateInterface {
Directory? outputDirectory,
});

Future<Uint8List> createSpeechBytes({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't create an overload with the same param names, rename if unsuitable.

required String model,
required String input,
required String voice,
OpenAIAudioSpeechResponseFormat? responseFormat,
double? speed,
});

Future<OpenAIAudioModel> createTranscription({
required File file,
required String model,
Expand Down
79 changes: 51 additions & 28 deletions lib/src/core/networking/client.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "dart:async";
import "dart:convert";
import "dart:io";
import "dart:typed_data";
import "package:dart_openai/src/core/constants/config.dart";
import "package:dart_openai/src/core/utils/extensions.dart";

Expand Down Expand Up @@ -206,6 +207,55 @@ abstract class OpenAINetworkingClient {
Map<String, dynamic>? body,
http.Client? client,
}) async {

var response = await postAndGetResponse(to: to, body: body, client: client);

final fileTypeHeader = "content-type";

final fileExtensionFromBodyResponseFormat =
response.headers[fileTypeHeader]?.split("/").last ?? "mp3";

final fileName =
outputFileName + "." + fileExtensionFromBodyResponseFormat;

File file = File(
"${outputDirectory != null ? outputDirectory.path : ''}" +
"/" +
fileName,
);

OpenAILogger.creatingFile(fileName);

await file.create();
OpenAILogger.fileCreatedSuccessfully(fileName);
OpenAILogger.writingFileContent(fileName);

file = await file.writeAsBytes(
response.bodyBytes,
mode: FileMode.write,
);

OpenAILogger.fileContentWrittenSuccessfully(fileName);

return onFileResponse(file);
}


static Future<Uint8List> postAndGetBytes({
required String to,
Map<String, dynamic>? body,
http.Client? client,
}) async {

var response = await postAndGetResponse(to: to, body: body, client: client);
return response.bodyBytes;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case just get the bytes, should mean we can play with AudioPlayer BytesSource data source on platforms which don't support file operations

}

static Future<http.Response> postAndGetResponse({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separated the basics of getting the response with bytes data so we still have the error handling/logging

required String to,
Map<String, dynamic>? body,
http.Client? client,
}) async {
OpenAILogger.logStartRequest(to);

final uri = Uri.parse(to);
Expand Down Expand Up @@ -255,34 +305,7 @@ abstract class OpenAINetworkingClient {

OpenAILogger.requestFinishedSuccessfully();

final fileTypeHeader = "content-type";

final fileExtensionFromBodyResponseFormat =
response.headers[fileTypeHeader]?.split("/").last ?? "mp3";

final fileName =
outputFileName + "." + fileExtensionFromBodyResponseFormat;

File file = File(
"${outputDirectory != null ? outputDirectory.path : ''}" +
"/" +
fileName,
);

OpenAILogger.creatingFile(fileName);

await file.create();
OpenAILogger.fileCreatedSuccessfully(fileName);
OpenAILogger.writingFileContent(fileName);

file = await file.writeAsBytes(
response.bodyBytes,
mode: FileMode.write,
);

OpenAILogger.fileContentWrittenSuccessfully(fileName);

return onFileResponse(file);
return response;
}
}

Expand Down
22 changes: 22 additions & 0 deletions lib/src/instance/audio/audio.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:dart_openai/src/core/builder/base_api_url.dart';
import 'package:dart_openai/src/core/networking/client.dart';

Expand Down Expand Up @@ -151,4 +153,24 @@ interface class OpenAIAudio implements OpenAIAudioBase {
outputDirectory: outputDirectory,
);
}

@override
Future<Uint8List> createSpeechBytes({
required String model,
required String input,
required String voice,
OpenAIAudioSpeechResponseFormat? responseFormat,
double? speed,
}) async {
return await OpenAINetworkingClient.postAndGetBytes(
to: BaseApiUrlBuilder.build(endpoint + "/speech"),
body: {
"model": model,
"input": input,
"voice": voice,
if (responseFormat != null) "response_format": responseFormat.name,
if (speed != null) "speed": speed,
},
);
}
}