From 292f51519bb7157c6552fecdf60148a4ee61710f Mon Sep 17 00:00:00 2001 From: lollipopkit Date: Fri, 29 Dec 2023 11:33:33 +0800 Subject: [PATCH] [Edit] fix chat completion & opt. --- .../choices/sub_models/sub_models/content.dart | 13 ++++++++----- .../models/fine_tune/sub_models/hyper_params.dart | 5 +++-- lib/src/core/networking/client.dart | 10 +++++----- lib/src/core/utils/logger.dart | 5 ++++- test/openai_test.dart | 8 ++++++-- 5 files changed, 26 insertions(+), 15 deletions(-) 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..21faf199 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, ); } @@ -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)', + }; } diff --git a/lib/src/core/models/fine_tune/sub_models/hyper_params.dart b/lib/src/core/models/fine_tune/sub_models/hyper_params.dart index 0c6806d5..3b43a2b5 100644 --- a/lib/src/core/models/fine_tune/sub_models/hyper_params.dart +++ b/lib/src/core/models/fine_tune/sub_models/hyper_params.dart @@ -37,7 +37,7 @@ 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, @@ -45,8 +45,9 @@ final class OpenAIFineTuneHyperParamsModel { 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] object to a [OpenAIFineTuneHyperParamsModel] object. + /// {@endtemplate} factory OpenAIFineTuneHyperParamsModel.fromMap(Map json) { return OpenAIFineTuneHyperParamsModel( batchSize: json['batch_size'], diff --git a/lib/src/core/networking/client.dart b/lib/src/core/networking/client.dart index 9ba1d426..bd9f908d 100644 --- a/lib/src/core/networking/client.dart +++ b/lib/src/core/networking/client.dart @@ -597,11 +597,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)) { diff --git a/lib/src/core/utils/logger.dart b/lib/src/core/utils/logger.dart index 9de265aa..449d85c6 100644 --- a/lib/src/core/utils/logger.dart +++ b/lib/src/core/utils/logger.dart @@ -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} @@ -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. diff --git a/test/openai_test.dart b/test/openai_test.dart index 6c130a84..a1828b2f 100644 --- a/test/openai_test.dart +++ b/test/openai_test.dart @@ -529,9 +529,12 @@ void main() async { File jsonLFileExample() { final file = File("example.jsonl"); file.writeAsStringSync( - jsonEncode( - {"prompt": "", "completion": ""}), + jsonEncode({ + "prompt": "", + "completion": "", + }), ); + return file; } @@ -543,5 +546,6 @@ Future getFileFromUrl( final uniqueImageName = DateTime.now().microsecondsSinceEpoch; final file = File("$uniqueImageName.$fileExtension"); await file.writeAsBytes(response.bodyBytes); + return file; }