Skip to content

Commit

Permalink
[ Edit ] exposed more enums for chat, audio, umage for better develop…
Browse files Browse the repository at this point in the history
…er experience and refactored and added more docs
  • Loading branch information
anasfik committed Mar 26, 2023
1 parent 9ca9db0 commit 7011bc7
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 48 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ Creates a completion for the chat message, note you need to set each message as
OpenAIChatCompletionModel chatCompletion = await OpenAI.instance.chat.create(
model: "gpt-3.5-turbo",
messages: [
OpenAIChatCompletionChoiceMessageModel(content: "hello, what is Flutter and Dart ?", role: "user"),
OpenAIChatCompletionChoiceMessageModel(
content: "hello, what is Flutter and Dart ?",
role: OpenAIChatMessageRole.user,
),
],
);
```
Expand All @@ -234,7 +237,7 @@ OpenAIStreamChatCompletionModel chatStream = OpenAI.instance.chat.createStream(
messages: [
OpenAIChatCompletionChoiceMessageModel(
content: "hello",
role: "user",
role: OpenAIChatMessageRole.user,
)
],
);
Expand Down Expand Up @@ -277,7 +280,7 @@ Generates a new image based on a prompt given.
prompt: 'an astronaut on the sea',
n: 1,
size: OpenAIImageSize.size1024,
responseFormat: OpenAIResponseFormat.url,
responseFormat: OpenAIImageResponseFormat.url,
);
```

Expand All @@ -292,7 +295,7 @@ OpenAiImageEditModel imageEdit = await OpenAI.instance.image.edit(
prompt: "mask the image with a dinosaur",
n: 1,
size: OpenAIImageSize.size1024,
responseFormat: OpenAIResponseFormat.url,
responseFormat: OpenAIImageResponseFormat.url,
);
```

Expand All @@ -305,7 +308,7 @@ OpenAIImageVariationModel imageVariation = await OpenAI.instance.image.variation
image: File(/* IMAGE PATH HERE */),
n: 1,
size: OpenAIImageSize.size1024,
responseFormat: OpenAIResponseFormat.url,
responseFormat: OpenAIImageResponseFormat.url,
);
```

Expand Down
2 changes: 1 addition & 1 deletion example/lib/chat_campletion_stream_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main() {
messages: [
OpenAIChatCompletionChoiceMessageModel(
content: "hello",
role: "user",
role: OpenAIChatMessageRole.user,
)
],
);
Expand Down
12 changes: 7 additions & 5 deletions example/lib/chat_completion_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ void main() async {
// Set the OpenAI API key from the .env file.
OpenAI.apiKey = Env.apiKey;

OpenAIChatCompletionModel chatCompletion = await OpenAI.instance.chat.create(
model: "gpt-3.5-turbo",
messages: [
OpenAIChatCompletionChoiceMessageModel(content: "hello", role: "user")
]);
OpenAIChatCompletionModel chatCompletion =
await OpenAI.instance.chat.create(model: "gpt-3.5-turbo", messages: [
OpenAIChatCompletionChoiceMessageModel(
content: "hello",
role: OpenAIChatMessageRole.user,
)
]);

print(chatCompletion.id);
print(chatCompletion.choices.first.message);
Expand Down
2 changes: 1 addition & 1 deletion example/lib/image_variation_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Future<void> main() async {
image: File("example.png"),
n: 1,
size: OpenAIImageSize.size256,
responseFormat: OpenAIResponseFormat.b64Json,
responseFormat: OpenAIImageResponseFormat.b64Json,
);

// Prints the result.
Expand Down
6 changes: 3 additions & 3 deletions lib/src/core/base/images/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ extension SizeToStingExtension on OpenAIImageSize {
}
}

extension ResponseFormatToStingExtension on OpenAIResponseFormat {
extension ResponseFormatToStingExtension on OpenAIImageResponseFormat {
String get value {
switch (this) {
case OpenAIResponseFormat.url:
case OpenAIImageResponseFormat.url:
return "url";
case OpenAIResponseFormat.b64Json:
case OpenAIImageResponseFormat.b64Json:
return "b64_json";
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/base/images/interfaces/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abstract class CreateInterface {
required String prompt,
int? n,
OpenAIImageSize? size,
OpenAIResponseFormat? responseFormat,
OpenAIImageResponseFormat? responseFormat,
String? user,
});
}
2 changes: 1 addition & 1 deletion lib/src/core/base/images/interfaces/edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ abstract class EditInterface {
required String prompt,
int? n,
OpenAIImageSize? size,
OpenAIResponseFormat? responseFormat,
OpenAIImageResponseFormat? responseFormat,
String? user,
});
}
2 changes: 1 addition & 1 deletion lib/src/core/base/images/interfaces/variations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ abstract class VariationInterface {
required File image,
int? n,
OpenAIImageSize? size,
OpenAIResponseFormat? responseFormat,
OpenAIImageResponseFormat? responseFormat,
String? user,
});
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import '../../../../image/enum.dart';

/// {@template openai_chat_completion_choice_message_model}
/// This represents the message of the [OpenAIChatCompletionChoiceModel] model of the OpenAI API, which is used and get returned while using the [OpenAIChat] methods.
/// {@endtemplate}
class OpenAIChatCompletionChoiceMessageModel {
/// The [role] of the message.
final String role;
final OpenAIChatMessageRole role;

/// The [content] of the message.
final String content;
Expand All @@ -24,15 +26,16 @@ class OpenAIChatCompletionChoiceMessageModel {
Map<String, dynamic> json,
) {
return OpenAIChatCompletionChoiceMessageModel(
role: json['role'],
role: OpenAIChatMessageRole.values
.firstWhere((role) => role.name == json['role']),
content: json['content'],
);
}

/// This method used to convert the [OpenAIChatCompletionChoiceMessageModel] to a [Map<String, dynamic>] object.
Map<String, dynamic> toMap() {
return {
"role": role,
"role": role.name,
"content": content,
};
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/core/models/image/enum.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
enum OpenAIImageSize { size256, size512, size1024 }

enum OpenAIResponseFormat { url, b64Json }
enum OpenAIImageResponseFormat { url, b64Json }

enum OpenAIAudioResponseFormat { json, text, srt, verbose_json, vtt }

enum OpenAIChatMessageRole { system, user, assistant }
24 changes: 12 additions & 12 deletions lib/src/instance/images/images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class OpenAIImages implements OpenAIImagesBase {
///
///
/// [responseFormat] is the format in which the generated images are returned. Must be one of :
/// - `OpenAIResponseFormat.url`
/// - `OpenAIResponseFormat.b64Json`
/// - `OpenAIImageResponseFormat.url`
/// - `OpenAIImageResponseFormat.b64Json`
///
///
/// [user] is the user ID to associate with the request. This is used to prevent abuse of the API.
Expand All @@ -55,15 +55,15 @@ class OpenAIImages implements OpenAIImagesBase {
/// prompt: 'create an image about the sea',
/// n: 1,
/// size: OpenAIImageSize.size1024,
/// responseFormat: OpenAIResponseFormat.url,
/// responseFormat: OpenAIImageResponseFormat.url,
/// );
///```
@override
Future<OpenAIImageModel> create({
required String prompt,
int? n,
OpenAIImageSize? size,
OpenAIResponseFormat? responseFormat,
OpenAIImageResponseFormat? responseFormat,
String? user,
}) async {
final String generations = "/generations";
Expand Down Expand Up @@ -100,8 +100,8 @@ class OpenAIImages implements OpenAIImagesBase {
///
///
/// [responseFormat] is the format in which the generated images are returned. Must be one of :
/// - `OpenAIResponseFormat.url`
/// - `OpenAIResponseFormat.b64Json`
/// - `OpenAIImageResponseFormat.url`
/// - `OpenAIImageResponseFormat.b64Json`
///
///
///
Expand All @@ -116,7 +116,7 @@ class OpenAIImages implements OpenAIImagesBase {
/// prompt: "mask the image with a dinosaur in the image",
/// n: 1,
/// size: OpenAIImageSize.size1024,
/// responseFormat: OpenAIResponseFormat.url,
/// responseFormat: OpenAIImageResponseFormat.url,
/// );
///```
@override
Expand All @@ -126,7 +126,7 @@ class OpenAIImages implements OpenAIImagesBase {
required String prompt,
int? n,
OpenAIImageSize? size,
OpenAIResponseFormat? responseFormat,
OpenAIImageResponseFormat? responseFormat,
String? user,
}) async {
final String edit = "/edits";
Expand Down Expand Up @@ -163,8 +163,8 @@ class OpenAIImages implements OpenAIImagesBase {
///
///
/// [responseFormat] is the format in which the generated images are returned. Must be one of :
/// - `OpenAIResponseFormat.url`
/// - `OpenAIResponseFormat.b64Json`
/// - `OpenAIImageResponseFormat.url`
/// - `OpenAIImageResponseFormat.b64Json`
///
///
/// [user] is the user ID to associate with the request. This is used to prevent abuse of the API.
Expand All @@ -176,15 +176,15 @@ class OpenAIImages implements OpenAIImagesBase {
/// image: File(/* IMAGE PATH HERE */),
/// n: 1,
/// size: OpenAIImageSize.size1024,
/// responseFormat: OpenAIResponseFormat.url,
/// responseFormat: OpenAIImageResponseFormat.url,
/// );
/// ```
@override
Future<OpenAIImageVariationModel> variation({
required File image,
int? n,
OpenAIImageSize? size,
OpenAIResponseFormat? responseFormat,
OpenAIImageResponseFormat? responseFormat,
String? user,
}) async {
final String variations = "/variations";
Expand Down
30 changes: 16 additions & 14 deletions test/openai_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:dart_openai/openai.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';

@Timeout(Duration(minutes: 2))
void main() async {
final exampleImageFile = await getFileFromUrl(
"https://upload.wikimedia.org/wikipedia/commons/7/7e/Dart-logo.png",
Expand Down Expand Up @@ -34,7 +35,7 @@ void main() async {
}
});
test('with setting a key', () {
OpenAI.apiKey = "YOUR KEY HERE SO THE TESTS CAN RUN";
OpenAI.apiKey = "PUT HERE YOUR API KEY";

expect(OpenAI.instance, isA<OpenAI>());
});
Expand Down Expand Up @@ -125,7 +126,7 @@ void main() async {
messages: [
OpenAIChatCompletionChoiceMessageModel(
content: "Hello, how are you?",
role: "user",
role: OpenAIChatMessageRole.user,
),
],
);
Expand All @@ -148,7 +149,7 @@ void main() async {
messages: [
OpenAIChatCompletionChoiceMessageModel(
content: "Hello, how are you?",
role: "user",
role: OpenAIChatMessageRole.user,
),
],
);
Expand All @@ -160,17 +161,18 @@ void main() async {
});
});
group('edits', () {
test('create', () async {
final OpenAIEditModel edit = await OpenAI.instance.edit.create(
model: "text-davinci-edit-001",
instruction: "remove the word 'made' from the text",
input: "I made something, idk man",
);
expect(edit, isA<OpenAIEditModel>());
expect(edit.choices.first, isA<OpenAIEditModelChoice>());
expect(edit.choices.first.text, isNotNull);
expect(edit.choices.first.text, isA<String>());
});
//! temporary disabled, because the API have on this and throws an unexpected error from OpenAI end.
// test('create', () async {
// final OpenAIEditModel edit = await OpenAI.instance.edit.create(
// model: "text-davinci-edit-001",
// instruction: "remove the word 'made' from the text",
// input: "I made something, idk man",
// );
// expect(edit, isA<OpenAIEditModel>());
// expect(edit.choices.first, isA<OpenAIEditModelChoice>());
// expect(edit.choices.first.text, isNotNull);
// expect(edit.choices.first.text, isA<String>());
// });
});
group('images', () {
test('create', () async {
Expand Down

0 comments on commit 7011bc7

Please sign in to comment.