Skip to content

Commit

Permalink
feat: add assistant version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
redevrx committed May 26, 2024
1 parent 23e2cf6 commit 2e8b7a9
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 9 deletions.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,5 @@ packages:
source: hosted
version: "1.0.4"
sdks:
dart: ">=3.4.0 <4.0.0"
dart: ">=3.4.1 <4.0.0"
flutter: ">=3.19.0"
4 changes: 2 additions & 2 deletions example_app/openai_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ packages:
path: "../.."
relative: true
source: path
version: "3.0.7"
version: "3.0.8"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -565,5 +565,5 @@ packages:
source: hosted
version: "1.0.4"
sdks:
dart: ">=3.4.0 <4.0.0"
dart: ">=3.4.1 <4.0.0"
flutter: ">=3.19.2"
97 changes: 96 additions & 1 deletion lib/src/assistants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,85 @@ import 'package:chat_gpt_sdk/src/utils/constants.dart';

import 'client/client.dart';

class AssistantsV2 {
final OpenAIClient _client;
AssistantsV2(this._client);

Map<String, String> get getHeader => headersAssistantsV2;

///Create an assistant with a model and instructions.
///[create]
Future<AssistantData> create({
required Assistant assistant,
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
_client.apiUrl + kAssistants,
assistant.toJsonV2(),
headers: headersAssistants,
onSuccess: AssistantData.fromJson,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
);
}

///Returns a list of assistants.
///[list]
Future<List<AssistantData>> list() {
return _client.get(
_client.apiUrl + kAssistants,
headers: headersAssistants,
onSuccess: (it) => it['data'] == null
? []
: List<AssistantData>.from(
it['data'].map((x) => AssistantData.fromJson(x)),
),
onCancel: (_) => null,
);
}

///Retrieves an assistant.
///[retrieves]
Future<AssistantData> retrieves({
required String assistantId,
}) {
return _client.get(
_client.apiUrl + kAssistants + "/$assistantId",
headers: headersAssistants,
onSuccess: AssistantData.fromJson,
onCancel: (_) => null,
);
}

///Modifies an assistant.
/// [modifies]
Future<AssistantData> modifies({
required String assistantId,
required Assistant assistant,
void Function(CancelData cancelData)? onCancel,
}) {
return _client.post(
_client.apiUrl + kAssistants + "/$assistantId",
assistant.toJson(),
headers: headersAssistants,
onSuccess: AssistantData.fromJson,
onCancel: (it) => onCancel != null ? onCancel(it) : null,
);
}

///Delete an assistant.
///[delete]
Future<DeleteAssistant> delete({
required String assistantId,
}) {
return _client.delete(
_client.apiUrl + kAssistants + "/$assistantId",
headers: headersAssistants,
onSuccess: DeleteAssistant.fromJson,
onCancel: (_) => null,
);
}
}

class Assistants {
final OpenAIClient _client;
Assistants(this._client);
Expand All @@ -20,6 +99,15 @@ class Assistants {
headersAssistants.addAll(mHeader);
}

///Assistants Version2
///We have changed the way that tools and files work in the Assistants API
/// between the v1 and v2 versions of the beta.
/// Both versions of the beta continue to be accessible via the API today,
/// but we recommend migrating to the newest version of our APIs as soon as feasible.
/// We will deprecate v1 of the beta by the end of 2024.
AssistantsV2 get v2 => AssistantsV2(_client);

@Deprecated('Using Assistants Version 2')
///Create an assistant with a model and instructions.
///[create]
Future<AssistantData> create({
Expand All @@ -35,7 +123,8 @@ class Assistants {
);
}

///

@Deprecated('Using Assistants Version 2')
/// [assistantId]
/// The ID of the assistant for which to create a File.
/// [fileId]
Expand All @@ -59,6 +148,7 @@ class Assistants {
);
}

@Deprecated('Using Assistants Version 2')
///Returns a list of assistants.
///[list]
Future<List<AssistantData>> list() {
Expand All @@ -74,6 +164,7 @@ class Assistants {
);
}

@Deprecated('Using Assistants Version 2')
///Returns a list of assistant files.
///[listFile]
///[assistantId]
Expand All @@ -89,6 +180,7 @@ class Assistants {
);
}

@Deprecated('Using Assistants Version 2')
///Retrieves an assistant.
///[retrieves]
Future<AssistantData> retrieves({
Expand All @@ -102,6 +194,7 @@ class Assistants {
);
}

@Deprecated('Using Assistants Version 2')
///Retrieves an AssistantFile.
/// [retrievesFile]
Future<AssistantFileData> retrievesFile({
Expand All @@ -116,6 +209,7 @@ class Assistants {
);
}

@Deprecated('Using Assistants Version 2')
///Modifies an assistant.
/// [modifies]
Future<AssistantData> modifies({
Expand All @@ -132,6 +226,7 @@ class Assistants {
);
}

@Deprecated('Using Assistants Version 2')
///Delete an assistant.
///[delete]
Future<DeleteAssistant> delete({
Expand Down
34 changes: 34 additions & 0 deletions lib/src/model/assistant/request/assistant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Assistant {
/// [metadata]
final Map? metadata;

final Map<String,dynamic>? fileSearch;

Assistant({
required this.model,
this.name,
Expand All @@ -55,6 +57,7 @@ class Assistant {
this.tools,
this.fileIds,
this.metadata,
this.fileSearch,
});

Map<String, dynamic> toJson() => Map.of({
Expand All @@ -66,4 +69,35 @@ class Assistant {
'file_ids': fileIds ?? [],
'metadata': metadata ?? {},
});


Map<String, dynamic> toJsonV2(){
final data = Map.of({
"model": model.model,
'name': name,
'description': description,
'instructions': instructions,
'tools': tools,
'tool_resources':{
'file_search':fileSearch,
'code_interpreter':{
"file_ids":fileIds ?? [],
},
},
});

if(fileSearch == null){
(data['tool_resources'] as Map?)?.remove('file_search');
}

if(fileIds?.isEmpty == true){
(data['tool_resources'] as Map?)?.remove('code_interpreter');
}

if(fileIds?.isEmpty == true && fileSearch == null){
data.remove('tool_resources');
}

return data;
}
}
18 changes: 14 additions & 4 deletions lib/src/model/assistant/response/assistant_data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:chat_gpt_sdk/src/model/assistant/response/tool.dart';

class AssistantData {
AssistantData({
required this.instructions,
Expand All @@ -11,6 +9,9 @@ class AssistantData {
required this.id,
required this.tools,
required this.object,
this.description,
this.topP,
this.temperature,
});

String instructions;
Expand All @@ -20,8 +21,11 @@ class AssistantData {
int createdAt;
String model;
String id;
List<Tool> tools;
List<dynamic> tools;
String object;
final String? description;
final double? topP;
final double? temperature;

factory AssistantData.fromJson(Map<String, dynamic> json) => AssistantData(
instructions: json["instructions"] ?? '',
Expand All @@ -35,8 +39,11 @@ class AssistantData {
id: json["id"] ?? '',
tools: json["tools"] == null
? []
: List<Tool>.from(json["tools"].map((x) => Tool.fromJson(x))),
: List<dynamic>.from(json["tools"].map((x) => x)),
object: json["object"],
description: json['description'],
topP: json['top_p'],
temperature: json['temperature'],
);

Map<String, dynamic> toJson() => {
Expand All @@ -49,5 +56,8 @@ class AssistantData {
"id": id,
"tools": tools.map((x) => x.toJson()),
"object": object,
'description': description,
'top_p': topP,
'temperature':temperature,
};
}
1 change: 1 addition & 0 deletions lib/src/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Map<String, String> kHeader(
}

Map<String, String> headersAssistants = {'OpenAI-Beta': 'assistants=v1'};
Map<String, String> headersAssistantsV2 = {'OpenAI-Beta': 'assistants=v2'};

///key data
const kTokenKey = 'token';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ homepage: https://www.facebook.com/REDEVRX
repository: https://github.com/redevRx/Flutter-ChatGPT

environment:
sdk: '>=3.4.0 <4.0.0'
sdk: '>=3.4.1 <4.0.0'

dependencies:
dio: ^5.4.3+1
Expand Down

0 comments on commit 2e8b7a9

Please sign in to comment.