Skip to content

Commit

Permalink
[ Edit, Fix ] fixes for the chat completion new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anasfik committed Feb 21, 2024
1 parent c3f5fe2 commit e4b3eb5
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
13 changes: 8 additions & 5 deletions example/lib/chat_completion_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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); //
Expand Down
8 changes: 3 additions & 5 deletions example/lib/chat_stream_example_with_function_call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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.",
),
],
),
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions example/lib/create_audio_speech.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down
2 changes: 1 addition & 1 deletion example/lib/create_audio_transcription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Future<void> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class OpenAIChatCompletionChoiceMessageContentItemModel {
String imageUrl,
) {
return OpenAIChatCompletionChoiceMessageContentItemModel._(
type: 'image',
type: 'image_url',
imageUrl: imageUrl,
);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/core/networking/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -277,6 +279,7 @@ abstract class OpenAINetworkingClient {
response.bodyBytes,
mode: FileMode.write,
);

OpenAILogger.fileContentWrittenSuccessfully(fileName);

return onFileResponse(file);
Expand Down
3 changes: 2 additions & 1 deletion lib/src/instance/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e4b3eb5

Please sign in to comment.