From b970797fde251797d446e1de6e0caa7015598f5d Mon Sep 17 00:00:00 2001 From: Nigel Grange Date: Thu, 7 Nov 2024 20:04:07 +0000 Subject: [PATCH] Added json_schema support to ResponseFormat --- example/pubspec.lock | 2 +- example_app/openai_app/pubspec.lock | 2 +- lib/chat_gpt_sdk.dart | 1 + .../model/chat_complete/request/json_schema.dart | 16 ++++++++++++++++ .../chat_complete/request/response_format.dart | 10 +++++++++- 5 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 lib/src/model/chat_complete/request/json_schema.dart diff --git a/example/pubspec.lock b/example/pubspec.lock index 10e5f4f..80bf69e 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -31,7 +31,7 @@ packages: path: ".." relative: true source: path - version: "3.1.1" + version: "3.1.2" clock: dependency: transitive description: diff --git a/example_app/openai_app/pubspec.lock b/example_app/openai_app/pubspec.lock index a3dd9ae..3c6b4e5 100644 --- a/example_app/openai_app/pubspec.lock +++ b/example_app/openai_app/pubspec.lock @@ -55,7 +55,7 @@ packages: path: "../.." relative: true source: path - version: "3.1.1" + version: "3.1.2" clock: dependency: transitive description: diff --git a/lib/chat_gpt_sdk.dart b/lib/chat_gpt_sdk.dart index 8ecade8..16627e0 100644 --- a/lib/chat_gpt_sdk.dart +++ b/lib/chat_gpt_sdk.dart @@ -43,6 +43,7 @@ export 'src/model/chat_complete/response/chat_response_sse.dart'; export 'src/model/chat_complete/enum/role.dart'; export 'src/model/chat_complete/enum/function_call.dart'; export 'src/model/chat_complete/request/messages.dart'; +export 'src/model/chat_complete/request/json_schema.dart'; export 'src/model/chat_complete/request/function_data.dart'; export 'src/model/chat_complete/request/response_format.dart'; export 'src/model/gen_image/enum/generate_image_model.dart'; diff --git a/lib/src/model/chat_complete/request/json_schema.dart b/lib/src/model/chat_complete/request/json_schema.dart new file mode 100644 index 0000000..2cd9f12 --- /dev/null +++ b/lib/src/model/chat_complete/request/json_schema.dart @@ -0,0 +1,16 @@ +class JsonSchema { + final String name; + final Map schema; + final bool strict = true; + + JsonSchema({required this.name, required this.schema}); + + Map toJson() { + final data = {}; + data['name'] = name; + data['schema'] = schema; + data['strict'] = strict; + + return data; + } +} diff --git a/lib/src/model/chat_complete/request/response_format.dart b/lib/src/model/chat_complete/request/response_format.dart index 9e4916e..a029c5a 100644 --- a/lib/src/model/chat_complete/request/response_format.dart +++ b/lib/src/model/chat_complete/request/response_format.dart @@ -1,11 +1,19 @@ +import 'package:chat_gpt_sdk/src/model/chat_complete/request/json_schema.dart'; + class ResponseFormat { final String type; + final JsonSchema? json_schema; + + ResponseFormat({required this.type, this.json_schema}); - ResponseFormat({required this.type}); + ResponseFormat.jsonSchema({required this.json_schema}) : type = 'json_schema'; Map toJson() { final data = {}; data['type'] = type; + if (json_schema != null) { + data['json_schema'] = json_schema!.toJson(); + } return data; }