Skip to content

Commit

Permalink
feat: threads
Browse files Browse the repository at this point in the history
  • Loading branch information
redevrx committed Jan 31, 2024
1 parent 845e687 commit 177539c
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,10 @@
- Fix create transcribes error
- Create speech
- Embedding
- Add new Model
- Add new Model
- Thread
- Create a thread.
- Retrieves a thread.
- Modifies a thread.
- Delete a thread.

2 changes: 1 addition & 1 deletion lib/src/model/file/request/upload_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UploadFile {

Future<FormData> getForm() async {
return FormData.fromMap({
'file': File(file.path).existsSync()
'file': (await File(file.path).exists())
? await MultipartFile.fromFile(file.path, filename: file.name)
: null,
'purpose': purpose,
Expand Down
18 changes: 18 additions & 0 deletions lib/src/model/thread/request/thread_request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ThreadRequest {
final List<Map<String, dynamic>>? messages;

///Set of 16 key-value pairs that can be attached to an object.
/// This can be useful for storing additional information about the
/// object in a structured format. Keys can be a maximum of 64
/// characters long and values can be a maxium of 512 characters long.
/// [metadata]
final Map<String, dynamic>? metadata;

ThreadRequest({this.messages, this.metadata});

Map<String, dynamic> toJson() => Map.of({
'messages': messages,
'metadata': metadata,
})
..removeWhere((_, value) => value == null);
}
24 changes: 24 additions & 0 deletions lib/src/model/thread/response/thread_delete_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class ThreadDeleteResponse {
ThreadDeleteResponse({
required this.deleted,
required this.id,
required this.object,
});

bool deleted;
String id;
String object;

factory ThreadDeleteResponse.fromJson(Map<String, dynamic> json) =>
ThreadDeleteResponse(
deleted: json["deleted"] ?? false,
id: json["id"] ?? '',
object: json["object"] ?? '',
);

Map<String, dynamic> toJson() => {
"deleted": deleted,
"id": id,
"object": object,
};
}
27 changes: 27 additions & 0 deletions lib/src/model/thread/response/thread_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ThreadResponse {
ThreadResponse({
required this.metadata,
required this.createdAt,
required this.id,
required this.object,
});

Map<String, dynamic> metadata;
int createdAt;
String id;
String object;

factory ThreadResponse.fromJson(Map<String, dynamic> json) => ThreadResponse(
metadata: json["metadata"] ?? {},
createdAt: json["created_at"] ?? 0,
id: json["id"] ?? '',
object: json["object"] ?? '',
);

Map<String, dynamic> toJson() => {
"metadata": metadata,
"created_at": createdAt,
"id": id,
"object": object,
};
}
3 changes: 3 additions & 0 deletions lib/src/openai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:chat_gpt_sdk/src/model/gen_image/response/gen_img_response.dart'
import 'package:chat_gpt_sdk/src/model/openai_engine/engine_model.dart';
import 'package:chat_gpt_sdk/src/model/openai_model/openai_model.dart';
import 'package:chat_gpt_sdk/src/moderation.dart';
import 'package:chat_gpt_sdk/src/threads.dart';
import 'package:chat_gpt_sdk/src/utils/constants.dart';
import 'package:chat_gpt_sdk/src/utils/token_builder.dart';
import 'package:dio/dio.dart';
Expand Down Expand Up @@ -232,4 +233,6 @@ class OpenAI implements IOpenAI {

///Assistants
Assistants get assistant => Assistants(_client);

Threads get threads => Threads(client: _client);
}
70 changes: 70 additions & 0 deletions lib/src/threads.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import 'package:chat_gpt_sdk/src/client/openai_client.dart';
import 'package:chat_gpt_sdk/src/model/thread/request/thread_request.dart';
import 'package:chat_gpt_sdk/src/model/thread/response/thread_delete_response.dart';
import 'package:chat_gpt_sdk/src/model/thread/response/thread_response.dart';
import 'package:chat_gpt_sdk/src/utils/constants.dart';

class Threads {
final OpenAIClient _client;

Threads({required OpenAIClient client}) : _client = client;

Map<String, String> _headers = {'OpenAI-Beta': 'assistants=v1'};
Map<String, String> get getHeader => _headers;

void addHeader(Map<String, String> mHeader) {
if (mHeader == {}) return;

_headers.addAll(mHeader);
}

///Create a thread.
/// [createThread]
Future<ThreadResponse> createThread({
ThreadRequest? request,
}) {
return _client.post(
_client.apiUrl + kThread,
request == null ? {} : request.toJson(),
headers: _headers,
onSuccess: ThreadResponse.fromJson,
onCancel: (cancelData) => null,
);
}

///The ID of the thread to retrieve.[threadId]
/// [retrieveThread]
Future<ThreadResponse> retrieveThread({
required String threadId,
}) {
return _client.get(
_client.apiUrl + kThread + "/$threadId",
headers: _headers,
onSuccess: ThreadResponse.fromJson,
onCancel: (cancelData) => null,
);
}

Future<ThreadResponse> modifyThread({
required String threadId,
required Map<String, dynamic> metadata,
}) {
return _client.post(
_client.apiUrl + kThread + "/$threadId",
metadata,
headers: _headers,
onSuccess: ThreadResponse.fromJson,
onCancel: (cancelData) => null,
);
}

Future<ThreadDeleteResponse> deleteThread({
required String threadId,
}) {
return _client.delete(
_client.apiUrl + kThread + "/$threadId",
onSuccess: ThreadDeleteResponse.fromJson,
onCancel: (cancelData) => null,
);
}
}
3 changes: 3 additions & 0 deletions lib/src/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const kFile = 'files';
const kFineTune = 'fine-tunes';
const kFineTuneJob = '$kFineTune/jobs';

///threads
const kThread = 'threads';

/// fine tune model
const kFineTuneModel = 'models';

Expand Down

0 comments on commit 177539c

Please sign in to comment.