Skip to content

Commit 4a47501

Browse files
committed
[ Add, Edit ] added tests for audio methods, documnented more models properties and added toMap() for models
1 parent 537cebd commit 4a47501

File tree

47 files changed

+196
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+196
-88
lines changed

1678360427710524.mp3

557 KB
Binary file not shown.

example/lib/create_audio_translation.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@ Future<void> main() async {
1111
OpenAI.apiKey = Env.apiKey;
1212

1313
// create the audio transcription.
14-
final translation = OpenAI.instance.audio.createTranslation(
14+
final translation = await OpenAI.instance.audio.createTranslation(
1515
file: await getFileFromUrl(
16-
'https://www.cbvoiceovers.com/wp-content/uploads/2017/05/Commercial-showreel.mp3',
17-
),
16+
'https://www.cbvoiceovers.com/wp-content/uploads/2017/05/Commercial-showreel.mp3',
17+
fileExtension: "mp3"),
1818
model: "whisper-1",
19+
responseFormat: "json",
1920
);
2021

2122
// print the translation.
2223
print(translation);
2324
}
2425

25-
Future<File> getFileFromUrl(String networkUrl) async {
26+
Future<File> getFileFromUrl(
27+
String networkUrl, {
28+
String fileExtension = 'png',
29+
}) async {
2630
final response = await http.get(Uri.parse(networkUrl));
2731
final uniqueImageName = DateTime.now().microsecondsSinceEpoch;
28-
final file = File("$uniqueImageName.mp3");
32+
final file = File("$uniqueImageName.$fileExtension");
2933
await file.writeAsBytes(response.bodyBytes);
3034
return file;
3135
}

lib/src/core/models/audio/audio.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
1-
class OpenAIAudioModel {
2-
// lass from json
3-
// {
4-
// "text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger. This is a place where you can get to do that."
5-
// }
1+
import 'package:meta/meta.dart';
62

3+
/// {@template openai_audio}
4+
/// This class represents the audio model of the OpenAI API, which is used and get returned while using the [OpenAIAudio] methods.
5+
/// {@endtemplate}
6+
@immutable
7+
class OpenAIAudioModel {
8+
// This the generated text.
79
final String text;
810

911
@override
1012
int get hashCode => text.hashCode;
1113

14+
/// {@macro openai_audio}
1215
OpenAIAudioModel({
1316
required this.text,
1417
});
15-
factory OpenAIAudioModel.fromJson(Map<String, dynamic> json) {
18+
19+
/// This is used to convert a [Map<String, dynamic>] object to a [OpenAIAudioModel] object.
20+
factory OpenAIAudioModel.fromMap(Map<String, dynamic> json) {
1621
return OpenAIAudioModel(
1722
text: json['text'],
1823
);
1924
}
2025

21-
Map<String, dynamic> toJson() {
26+
/// This method used to convert the [OpenAIAudioModel] to a [Map<String, dynamic>] object.
27+
///
28+
/// could be useful if you want to save an audio response to a database.
29+
Map<String, dynamic> toMap() {
2230
return {
2331
'text': text,
2432
};

lib/src/core/models/chat/chat.dart

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:collection/collection.dart';
2+
import 'package:meta/meta.dart';
23

34
import 'sub_models/choices/choices.dart';
45
import 'sub_models/usage.dart';
@@ -7,6 +8,10 @@ export 'sub_models/usage.dart';
78
export 'sub_models/choices/choices.dart';
89
export 'stream/chat.dart';
910

11+
/// {@template openai_chat_completion}
12+
/// This class represents the chat completion response model of the OpenAI API, which is used and get returned while using the [OpenAIChat] methods.
13+
/// {@endtemplate}
14+
@immutable
1015
class OpenAIChatCompletionModel {
1116
/// The [id] of the chat completion.
1217
final String id;
@@ -25,24 +30,36 @@ class OpenAIChatCompletionModel {
2530
return id.hashCode ^ created.hashCode ^ choices.hashCode ^ usage.hashCode;
2631
}
2732

33+
/// {@macro openai_chat_completion}
2834
OpenAIChatCompletionModel({
2935
required this.id,
3036
required this.created,
3137
required this.choices,
3238
required this.usage,
3339
});
3440

35-
factory OpenAIChatCompletionModel.fromJson(Map<String, dynamic> json) {
41+
/// This is used to convert a [Map<String, dynamic>] object to a [OpenAIChatCompletionModel] object.
42+
factory OpenAIChatCompletionModel.fromMap(Map<String, dynamic> json) {
3643
return OpenAIChatCompletionModel(
3744
id: json['id'],
3845
created: DateTime.fromMillisecondsSinceEpoch(json['created'] * 1000),
3946
choices: (json['choices'] as List)
40-
.map((e) => OpenAIChatCompletionChoiceModel.fromJson(e))
47+
.map((e) => OpenAIChatCompletionChoiceModel.fromMap(e))
4148
.toList(),
42-
usage: OpenAIChatCompletionUsageModel.fromJson(json['usage']),
49+
usage: OpenAIChatCompletionUsageModel.fromMap(json['usage']),
4350
);
4451
}
4552

53+
/// This is used to convert a [OpenAIChatCompletionModel] object to a [Map<String, dynamic>] object.
54+
Map<String, dynamic> toMap() {
55+
return {
56+
"id": id,
57+
"created": created.millisecondsSinceEpoch,
58+
"choices": choices.map((e) => e.toMap()).toList(),
59+
"usage": usage.toMap(),
60+
};
61+
}
62+
4663
@override
4764
String toString() {
4865
return 'OpenAIChatCompletionModel(id: $id, created: $created, choices: $choices, usage: $usage)';

lib/src/core/models/chat/stream/chat.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ class OpenAIStreamChatCompletionModel {
2626
required this.choices,
2727
});
2828

29-
factory OpenAIStreamChatCompletionModel.fromJson(Map<String, dynamic> json) {
29+
factory OpenAIStreamChatCompletionModel.fromMap(Map<String, dynamic> json) {
3030
return OpenAIStreamChatCompletionModel(
3131
id: json['id'],
3232
created: DateTime.fromMillisecondsSinceEpoch(json['created'] * 1000),
3333
choices: (json['choices'] as List)
34-
.map((e) => OpenAIStreamChatCompletionChoiceModel.fromJson(e))
34+
.map((e) => OpenAIStreamChatCompletionChoiceModel.fromMap(e))
3535
.toList(),
3636
);
3737
}

lib/src/core/models/chat/stream/sub_models/choices/choices.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class OpenAIStreamChatCompletionChoiceModel {
1717
required this.finishReason,
1818
});
1919

20-
factory OpenAIStreamChatCompletionChoiceModel.fromJson(
20+
factory OpenAIStreamChatCompletionChoiceModel.fromMap(
2121
Map<String, dynamic> json,
2222
) {
2323
return OpenAIStreamChatCompletionChoiceModel(
2424
index: json['index'],
25-
delta: OpenAIStreamChatCompletionChoiceDeltaModel.fromJson(json['delta']),
25+
delta: OpenAIStreamChatCompletionChoiceDeltaModel.fromMap(json['delta']),
2626
finishReason: json['finish_reason'],
2727
);
2828
}

lib/src/core/models/chat/stream/sub_models/choices/sub_models/delta.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class OpenAIStreamChatCompletionChoiceDeltaModel {
1212
required this.content,
1313
});
1414

15-
factory OpenAIStreamChatCompletionChoiceDeltaModel.fromJson(
15+
factory OpenAIStreamChatCompletionChoiceDeltaModel.fromMap(
1616
Map<String, dynamic> json,
1717
) {
1818
return OpenAIStreamChatCompletionChoiceDeltaModel(

lib/src/core/models/chat/stream/sub_models/usage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class OpenAIStreamChatCompletionUsageModel {
1818
required this.totalTokens,
1919
});
2020

21-
factory OpenAIStreamChatCompletionUsageModel.fromJson(
21+
factory OpenAIStreamChatCompletionUsageModel.fromMap(
2222
Map<String, dynamic> json) {
2323
return OpenAIStreamChatCompletionUsageModel(
2424
promptTokens: json['prompt_tokens'],

lib/src/core/models/chat/sub_models/choices/choices.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
11
import 'sub_models/message.dart';
22

3+
/// {@template openai_chat_completion_choice}
4+
/// This class represents a choice of the [OpenAIChatCompletionModel] model of the OpenAI API, which is used and get returned while using the [OpenAIChat] methods.
5+
/// {@endtemplate}
36
class OpenAIChatCompletionChoiceModel {
7+
/// The [index] of the choice.
48
final int index;
9+
10+
/// The [message] of the choice.
511
final OpenAIChatCompletionChoiceMessageModel message;
12+
13+
/// The [finishReason] of the choice.
614
final String? finishReason;
715

816
@override
917
int get hashCode {
1018
return index.hashCode ^ message.hashCode ^ finishReason.hashCode;
1119
}
1220

21+
/// {@macro openai_chat_completion_choice}
1322
OpenAIChatCompletionChoiceModel({
1423
required this.index,
1524
required this.message,
1625
required this.finishReason,
1726
});
1827

19-
factory OpenAIChatCompletionChoiceModel.fromJson(Map<String, dynamic> json) {
28+
/// This is used to convert a [Map<String, dynamic>] object to a [OpenAIChatCompletionChoiceModel] object.
29+
factory OpenAIChatCompletionChoiceModel.fromMap(Map<String, dynamic> json) {
2030
return OpenAIChatCompletionChoiceModel(
2131
index: json['index'],
22-
message: OpenAIChatCompletionChoiceMessageModel.fromJson(json['message']),
32+
message: OpenAIChatCompletionChoiceMessageModel.fromMap(json['message']),
2333
finishReason: json['finish_reason'],
2434
);
2535
}
2636

37+
/// This method used to convert the [OpenAIChatCompletionChoiceModel] to a [Map<String, dynamic>] object.
38+
Map<String, dynamic> toMap() {
39+
return {
40+
"index": index,
41+
"message": message.toMap(),
42+
"finish_reason": finishReason,
43+
};
44+
}
45+
2746
@override
2847
String toString() {
2948
return 'OpenAIChatCompletionChoiceModel(index: $index, message: $message, finishReason: $finishReason)';

0 commit comments

Comments
 (0)