Skip to content

Commit

Permalink
Merge pull request #148 from lollipopkit/main
Browse files Browse the repository at this point in the history
[Edit] fix chat completion & opt.
  • Loading branch information
anasfik authored Feb 21, 2024
2 parents 7c4712a + 292f515 commit d471644
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ class OpenAIChatCompletionChoiceMessageContentItemModel {
other.imageUrl == imageUrl;
}

//! TODO: make a dynamic toString method for different types of content items.
@override
String toString() {
return 'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type, text: $text, imageUrl: $imageUrl)';
}
String toString() => switch (type) {
'text' =>
'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type, text: $text)',
'image' =>
'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type, imageUrl: $imageUrl)',
_ => 'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type)',
};
}
5 changes: 3 additions & 2 deletions lib/src/core/models/fine_tune/sub_models/hyper_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ final class OpenAIFineTuneHyperParamsModel {
promptLossWeight.hashCode;
}

/// {@template openai_fine_tune_hyper_params_model}
/// {@macro openai_fine_tune_hyper_params_model}
const OpenAIFineTuneHyperParamsModel({
required this.batchSize,
required this.learningRateMultiplier,
required this.nEpochs,
required this.promptLossWeight,
});

/// {@template openai_fine_tune_hyper_params_model}
/// {@template openai_fine_tune_hyper_params_model_fromMap}
/// This method is used to convert a [Map<String, dynamic>] object to a [OpenAIFineTuneHyperParamsModel] object.
/// {@endtemplate}
factory OpenAIFineTuneHyperParamsModel.fromMap(Map<String, dynamic> json) {
return OpenAIFineTuneHyperParamsModel(
batchSize: json['batch_size'],
Expand Down
10 changes: 5 additions & 5 deletions lib/src/core/networking/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,11 @@ abstract class OpenAINetworkingClient {

var resultBody;

resultBody = responseBody.canBeParsedToJson
? decodeToMap(responseBody)
: responseMapAdapter != null
? responseMapAdapter(responseBody)
: responseBody;
resultBody = switch ((responseBody.canBeParsedToJson, responseMapAdapter)) {
(true, _) => decodeToMap(responseBody),
(_, null) => responseBody,
(_, final func) => func!(responseBody),
};

OpenAILogger.decodedSuccessfully();
if (doesErrorExists(resultBody)) {
Expand Down
5 changes: 4 additions & 1 deletion lib/src/core/utils/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import '../constants/strings.dart';
@immutable
@internal
abstract final class OpenAILogger {
/// The valid min length of an api key.
static const int _kValidApiKeyLength = 10;

/// {@template openai_logger_is_active}
/// Wether the to show operations flow logger is active or not.
/// {@endtemplate}
Expand Down Expand Up @@ -87,7 +90,7 @@ abstract final class OpenAILogger {
static isValidApiKey(String key) {
return key.isNotEmpty &&
key.startsWith("sk-") &&
key.length > 10; //magic number
key.length > _kValidApiKeyLength;
}

/// Logs that an baseUrl key is being set, if the logger is active.
Expand Down
8 changes: 6 additions & 2 deletions test/openai_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,12 @@ void main() async {
File jsonLFileExample() {
final file = File("example.jsonl");
file.writeAsStringSync(
jsonEncode(
{"prompt": "<prompt text>", "completion": "<ideal generated text>"}),
jsonEncode({
"prompt": "<prompt text>",
"completion": "<ideal generated text>",
}),
);

return file;
}

Expand All @@ -606,5 +609,6 @@ Future<File> getFileFromUrl(
final uniqueImageName = DateTime.now().microsecondsSinceEpoch;
final file = File("$uniqueImageName.$fileExtension");
await file.writeAsBytes(response.bodyBytes);

return file;
}

0 comments on commit d471644

Please sign in to comment.