-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ Add ] support for log probs chat completion creation
- Loading branch information
Showing
7 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"cSpell.words": ["Epoches", "openai"], | ||
"cSpell.words": ["Epoches", "openai", "Probs"], | ||
"editor.acceptSuggestionOnEnter": "off" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'package:dart_openai/dart_openai.dart'; | ||
|
||
import 'env/env.dart'; | ||
|
||
void main() async { | ||
// Set the OpenAI API key from the .env file. | ||
OpenAI.apiKey = Env.apiKey; | ||
|
||
final systemMessage = OpenAIChatCompletionChoiceMessageModel( | ||
content: [ | ||
OpenAIChatCompletionChoiceMessageContentItemModel.text( | ||
"return any message you are given as JSON.", | ||
), | ||
], | ||
role: OpenAIChatMessageRole.assistant, | ||
); | ||
|
||
final userMessage = OpenAIChatCompletionChoiceMessageModel( | ||
content: [ | ||
OpenAIChatCompletionChoiceMessageContentItemModel.text( | ||
"Hello, I am a chatbot created by OpenAI. How are you today?", | ||
), | ||
], | ||
role: OpenAIChatMessageRole.user, | ||
name: "anas", | ||
); | ||
|
||
final requestMessages = [ | ||
systemMessage, | ||
userMessage, | ||
]; | ||
|
||
OpenAIChatCompletionModel chatCompletion = await OpenAI.instance.chat.create( | ||
model: "gpt-3.5-turbo-1106", | ||
responseFormat: {"type": "json_object"}, | ||
seed: 6, | ||
messages: requestMessages, | ||
temperature: 0.2, | ||
maxTokens: 500, | ||
logprobs: true, | ||
topLogprobs: 2, | ||
); | ||
|
||
print(chatCompletion.choices.first.logprobs?.content.first.bytes); // | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
lib/src/core/models/chat/sub_models/choices/sub_models/log_probs/log_probs.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// ignore_for_file: public_member_api_docs, sort_constructors_first | ||
import 'sub_models/content.dart'; | ||
|
||
class OpenAIChatCompletionChoiceLogProbsModel { | ||
OpenAIChatCompletionChoiceLogProbsModel({ | ||
required this.content, | ||
}); | ||
|
||
final List<OpenAIChatCompletionChoiceLogProbsContentModel> content; | ||
|
||
factory OpenAIChatCompletionChoiceLogProbsModel.fromMap( | ||
Map<String, dynamic> json, | ||
) { | ||
return OpenAIChatCompletionChoiceLogProbsModel( | ||
content: json["content"] != null | ||
? List<OpenAIChatCompletionChoiceLogProbsContentModel>.from( | ||
json["content"].map( | ||
(x) => | ||
OpenAIChatCompletionChoiceLogProbsContentModel.fromMap(x), | ||
), | ||
) | ||
: [], | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
"content": content.map((x) => x.toMap()).toList(), | ||
}; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
lib/src/core/models/chat/sub_models/choices/sub_models/log_probs/sub_models/content.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'top_prob.dart'; | ||
|
||
class OpenAIChatCompletionChoiceLogProbsContentModel { | ||
final String? token; | ||
|
||
final double? logprob; | ||
|
||
final List<int>? bytes; | ||
|
||
final List<OpenAIChatCompletionChoiceTopLogProbsContentModel>? topLogprobs; | ||
|
||
OpenAIChatCompletionChoiceLogProbsContentModel({ | ||
this.token, | ||
this.logprob, | ||
this.bytes, | ||
this.topLogprobs, | ||
}); | ||
|
||
factory OpenAIChatCompletionChoiceLogProbsContentModel.fromMap( | ||
Map<String, dynamic> map, | ||
) { | ||
return OpenAIChatCompletionChoiceLogProbsContentModel( | ||
token: map['token'], | ||
logprob: map['logprob'], | ||
bytes: List<int>.from(map['bytes']), | ||
topLogprobs: List<OpenAIChatCompletionChoiceTopLogProbsContentModel>.from( | ||
map['top_logprobs']?.map( | ||
(x) => OpenAIChatCompletionChoiceTopLogProbsContentModel.fromMap(x), | ||
), | ||
), | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'token': token, | ||
'logprob': logprob, | ||
'bytes': bytes, | ||
}; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/src/core/models/chat/sub_models/choices/sub_models/log_probs/sub_models/top_prob.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import 'content.dart'; | ||
|
||
class OpenAIChatCompletionChoiceTopLogProbsContentModel | ||
extends OpenAIChatCompletionChoiceLogProbsContentModel { | ||
OpenAIChatCompletionChoiceTopLogProbsContentModel({ | ||
super.token, | ||
super.logprob, | ||
super.bytes, | ||
}); | ||
|
||
factory OpenAIChatCompletionChoiceTopLogProbsContentModel.fromMap( | ||
Map<String, dynamic> map, | ||
) { | ||
return OpenAIChatCompletionChoiceTopLogProbsContentModel( | ||
token: map['token'], | ||
logprob: map['logprob'], | ||
bytes: List<int>.from(map['bytes']), | ||
); | ||
} | ||
|
||
Map<String, dynamic> toMap() { | ||
return { | ||
'token': token, | ||
'logprob': logprob, | ||
'bytes': bytes, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters