From e4b3eb531c97cafdb3dd12d6794a7a4944f29eb2 Mon Sep 17 00:00:00 2001 From: Anas Fikhi Date: Wed, 21 Feb 2024 22:54:33 +0100 Subject: [PATCH] [ Edit, Fix ] fixes for the chat completion new changes --- example/lib/chat_completion_example.dart | 13 ++++++++----- .../lib/chat_stream_example_with_function_call.dart | 8 +++----- example/lib/create_audio_speech.dart | 4 ++-- example/lib/create_audio_transcription.dart | 2 +- .../choices/sub_models/sub_models/content.dart | 2 +- lib/src/core/networking/client.dart | 5 ++++- lib/src/instance/chat/chat.dart | 3 ++- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/example/lib/chat_completion_example.dart b/example/lib/chat_completion_example.dart index 4d648a0d..de7ad4b9 100644 --- a/example/lib/chat_completion_example.dart +++ b/example/lib/chat_completion_example.dart @@ -22,9 +22,9 @@ void main() async { ), //! image url contents are allowed only for models with image support - OpenAIChatCompletionChoiceMessageContentItemModel.imageUrl( - "https://placehold.co/600x400", - ), + // OpenAIChatCompletionChoiceMessageContentItemModel.imageUrl( + // "https://placehold.co/600x400", + // ), ], role: OpenAIChatMessageRole.user, ); @@ -41,8 +41,11 @@ void main() async { messages: requestMessages, temperature: 0.2, maxTokens: 500, - toolChoice: "auto", - tools: [/* tools if you have any */], + + // uncomment and set your own properties if you want to use tool choices feature.. + + // toolChoice: "auto", + // tools: [], ); print(chatCompletion.choices.first.message); // diff --git a/example/lib/chat_stream_example_with_function_call.dart b/example/lib/chat_stream_example_with_function_call.dart index 61d7a3b3..f72f9e0c 100644 --- a/example/lib/chat_stream_example_with_function_call.dart +++ b/example/lib/chat_stream_example_with_function_call.dart @@ -7,8 +7,6 @@ import 'env/env.dart'; void main() async { OpenAI.apiKey = Env.apiKey; - final ourMockMethodName = "fastestCarInTheWorldInTheYear"; - final chatStream = OpenAI.instance.chat.createStream( model: "gpt-3.5-turbo", messages: [ @@ -25,11 +23,11 @@ void main() async { OpenAIToolModel( type: "function", function: OpenAIFunctionModel.withParameters( - name: ourMockMethodName, + name: "fastestCarInTheWorldInTheYear", parameters: [ OpenAIFunctionProperty.integer( name: "year", - description: "The year to get the fastest car in", + description: "The year to get the fastest car in the world for.", ), ], ), @@ -52,7 +50,7 @@ void main() async { stringBuf.write(args); } }, onDone: () { - if (functionNameMapper.containsKey(ourMockMethodName)) { + if (functionNameMapper.containsKey("fastestCarInTheWorldInTheYear")) { final fullResponse = stringBuf.toString(); print(fullResponse); diff --git a/example/lib/create_audio_speech.dart b/example/lib/create_audio_speech.dart index 3f1192ef..2e34d2dc 100644 --- a/example/lib/create_audio_speech.dart +++ b/example/lib/create_audio_speech.dart @@ -9,9 +9,9 @@ void main() async { // The speech request. File speechFile = await OpenAI.instance.audio.createSpeech( model: "tts-1", - input: "Say my name is ", + input: "it is what it is.", voice: "nova", - responseFormat: OpenAIAudioSpeechResponseFormat.mp3, + responseFormat: OpenAIAudioSpeechResponseFormat.opus, outputDirectory: await Directory("speechOutput").create(), outputFileName: DateTime.now().microsecondsSinceEpoch.toString(), ); diff --git a/example/lib/create_audio_transcription.dart b/example/lib/create_audio_transcription.dart index ed80d116..f2f66e80 100644 --- a/example/lib/create_audio_transcription.dart +++ b/example/lib/create_audio_transcription.dart @@ -17,7 +17,7 @@ Future main() async { 'https://www.cbvoiceovers.com/wp-content/uploads/2017/05/Commercial-showreel.mp3', ), model: "whisper-1", - responseFormat: OpenAIAudioResponseFormat.srt, + responseFormat: OpenAIAudioResponseFormat.text, ); // print the transcription. diff --git a/lib/src/core/models/chat/sub_models/choices/sub_models/sub_models/content.dart b/lib/src/core/models/chat/sub_models/choices/sub_models/sub_models/content.dart index 37ab2f9b..24512a0e 100644 --- a/lib/src/core/models/chat/sub_models/choices/sub_models/sub_models/content.dart +++ b/lib/src/core/models/chat/sub_models/choices/sub_models/sub_models/content.dart @@ -46,7 +46,7 @@ class OpenAIChatCompletionChoiceMessageContentItemModel { String imageUrl, ) { return OpenAIChatCompletionChoiceMessageContentItemModel._( - type: 'image', + type: 'image_url', imageUrl: imageUrl, ); } diff --git a/lib/src/core/networking/client.dart b/lib/src/core/networking/client.dart index 9ba1d426..60439415 100644 --- a/lib/src/core/networking/client.dart +++ b/lib/src/core/networking/client.dart @@ -255,8 +255,10 @@ abstract class OpenAINetworkingClient { OpenAILogger.requestFinishedSuccessfully(); + final fileTypeHeader = "content-type"; + final fileExtensionFromBodyResponseFormat = - response.headers["response_format"] ?? "mp3"; + response.headers[fileTypeHeader]?.split("/")?.last ?? "mp3"; final fileName = outputFileName + "." + fileExtensionFromBodyResponseFormat; @@ -277,6 +279,7 @@ abstract class OpenAINetworkingClient { response.bodyBytes, mode: FileMode.write, ); + OpenAILogger.fileContentWrittenSuccessfully(fileName); return onFileResponse(file); diff --git a/lib/src/instance/chat/chat.dart b/lib/src/instance/chat/chat.dart index 7a564cd4..c4acfd8c 100644 --- a/lib/src/instance/chat/chat.dart +++ b/lib/src/instance/chat/chat.dart @@ -91,7 +91,8 @@ interface class OpenAIChat implements OpenAIChatBase { "messages": messages.map((message) => message.toMap()).toList(), if (tools != null) "tools": tools.map((tool) => tool.toMap()).toList(growable: false), - if (toolChoice != null) "tool_choice": toolChoice.value, + if (toolChoice != null) + "tool_choice": toolChoice is String ? toolChoice : toolChoice.value, if (temperature != null) "temperature": temperature, if (topP != null) "top_p": topP, if (n != null) "n": n,