Skip to content

Commit

Permalink
feat: runs
Browse files Browse the repository at this point in the history
  • Loading branch information
redevrx committed Feb 1, 2024
1 parent 7cfb889 commit 8f354d6
Show file tree
Hide file tree
Showing 15 changed files with 566 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,14 @@
- Retrieve message
- Retrieve message file
- Modify message
- Run
- Create a run.
- Create a thread and run it in one request.
- List runs
- List run steps
- Retrieves a run.
- Retrieve run step
- Modifies a run.
- Submit tool outputs to run
- Cancel a run

7 changes: 7 additions & 0 deletions lib/chat_gpt_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ export 'src/model/assistant/response/tool.dart';
export 'src/model/thread/request/thread_request.dart';
export 'src/model/thread/response/thread_delete_response.dart';
export 'src/model/thread/response/thread_response.dart';
export 'src/model/message/request/create_message.dart';
export 'src/model/message/response/content.dart';
export 'src/model/message/response/create_message_response.dart';
export 'src/model/message/response/list_message_file.dart';
export 'src/model/message/response/list_message_file_data.dart';
export 'src/model/message/response/message_data.dart';
export 'src/model/message/response/text.dart';
2 changes: 1 addition & 1 deletion lib/src/messages.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';
import 'package:chat_gpt_sdk/src/client/client.dart';
import 'package:chat_gpt_sdk/src/model/message/request/create_message.dart';
import 'package:chat_gpt_sdk/src/model/message/response/create_message_response.dart';
import 'package:chat_gpt_sdk/src/model/message/response/list_message_file.dart';
import 'package:chat_gpt_sdk/src/model/message/response/list_message_file_data.dart';
import 'package:chat_gpt_sdk/src/model/message/response/message_data.dart';
import 'package:chat_gpt_sdk/src/utils/constants.dart';

class Messages {
final OpenAIClient _client;
Expand Down
61 changes: 61 additions & 0 deletions lib/src/model/run/request/create_run.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class CreateRun {
///The ID of the
///<a href='https://platform.openai.com/docs/api-reference/assistants'>assistant</a>
/// to use to execute this run.
///[assistantId]
final String assistantId;

///The ID of the
///<a href='https://platform.openai.com/docs/api-reference/models'>Model</a>
/// to be used to execute this run.
/// If a value is provided here,
/// it will override the model associated with the assistant.
/// If not, the model associated with the assistant will be used.
/// [model]
final String? model;

///Overrides the
///<a href='https://platform.openai.com/docs/api-reference/assistants/createAssistant'>instructions</a>
/// of the assistant.
/// This is useful for modifying the behavior on a per-run basis.
/// [instructions]
final String? instructions;

///Appends additional instructions at the end of the instructions
/// for the run. This is useful for modifying the behavior
/// on a per-run basis without overriding other instructions.
/// [additionalInstructions]
final String? additionalInstructions;

///Override the tools the assistant can use for this run.
/// This is useful for modifying the behavior on a per-run basis.
/// [tools]
final List<Map<String, dynamic>>? tools;

///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;

CreateRun({
required this.assistantId,
this.model,
this.instructions,
this.additionalInstructions,
this.tools,
this.metadata,
});

Map<String, dynamic> toJson() => Map.of({
'assistant_id': assistantId,
'model': model,
'instructions': instructions,
'additional_instructions': additionalInstructions,
'tools': tools,
'metadata': metadata,
})
..removeWhere((_, value) => value == null);
}
64 changes: 64 additions & 0 deletions lib/src/model/run/request/create_thread_and_run.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class CreateThreadAndRun {
///The ID of the
///<a href='https://platform.openai.com/docs/api-reference/assistants'>assistant</a>
/// to use to execute this run.
/// [assistantId]
final String assistantId;

///
/// [thread]
/// ## Example
/// ```dart
/// {
/// "messages": [
/// {"role": "user", "content": "Explain deep learning to a 5 year old."}
/// ]
/// }
/// ```
final Map<String, dynamic>? thread;

///The ID of the
///<a href='https://platform.openai.com/docs/api-reference/models'>Model</a>
/// to be used to execute this run.
/// If a value is provided here, it will override the
/// model associated with the assistant.
/// If not, the model associated with the assistant will be used.
/// [model]
final String? model;

///Override the default system message of the assistant.
/// This is useful for modifying the behavior on a per-run basis.
/// [instructions]
final String? instructions;

///Override the tools the assistant can use for this run.
/// This is useful for modifying the behavior on a per-run basis.
/// [tools]
final List<Map<String, dynamic>>? tools;

///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;

CreateThreadAndRun({
required this.assistantId,
this.thread,
this.model,
this.instructions,
this.tools,
this.metadata,
});

Map<String, dynamic> toJson() => Map.of({
'assistant_id': assistantId,
'thread': thread,
'model': model,
'instructions': instructions,
'tools': tools,
'metadata': metadata,
})
..removeWhere((_, value) => value == null);
}
102 changes: 102 additions & 0 deletions lib/src/model/run/response/create_run_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:chat_gpt_sdk/src/model/assistant/response/tool.dart';
import 'package:chat_gpt_sdk/src/model/run/response/step_detail.dart';
import 'package:chat_gpt_sdk/src/model/run/response/usage.dart';

class CreateRunResponse {
CreateRunResponse({
required this.metadata,
required this.assistantId,
required this.createdAt,
required this.tools,
required this.completedAt,
required this.threadId,
required this.fileIds,
required this.startedAt,
required this.model,
required this.id,
required this.object,
required this.status,
required this.usage,
required this.instructions,
required this.lastError,
required this.failedAt,
required this.cancelledAt,
required this.expiresAt,
required this.type,
required this.stepDetails,
});

Map<String, dynamic> metadata;
String assistantId;
int createdAt;
List<Tool> tools;
int completedAt;
String threadId;
List<String> fileIds;
int startedAt;
String model;
String id;
String object;
String status;
Usage? usage;
String instructions;
Map<String, dynamic> lastError;
int failedAt;
int cancelledAt;
int expiresAt;
String type;
StepDetail? stepDetails;

factory CreateRunResponse.fromJson(Map<String, dynamic> json) =>
CreateRunResponse(
metadata: json["metadata"] ?? {},
assistantId: json["assistant_id"] ?? '',
createdAt: json["created_at"] ?? 0,
tools: json["tools"] == null
? []
: List<Tool>.from(json["tools"].map((x) => Tool.fromJson(x))),
completedAt: json["completed_at"] ?? '',
threadId: json["thread_id"] ?? '',
fileIds: json["file_ids"] == null
? []
: List<String>.from(json["file_ids"].map((x) => x)),
startedAt: json["started_at"] ?? 0,
model: json["model"] ?? '',
id: json["id"] ?? '',
object: json["object"] ?? '',
status: json["status"] ?? '',
usage: json["usage"] == null ? null : Usage.fromJson(json["usage"]),
instructions: json['instructions'] ?? '',
lastError: json['last_error'] ?? {},
failedAt: json['failed_at'] ?? 0,
cancelledAt: json['cancelled_at'] ?? 0,
expiresAt: json['expires_at'] ?? 0,
type: json['type'] ?? '',
stepDetails: json['step_details'] == null
? null
: StepDetail.fromJson(json['step_details']),
);

Map<String, dynamic> toJson() => {
"metadata": metadata,
"assistant_id": assistantId,
"created_at": createdAt,
"usage": usage?.toJson(),
"tools": tools.map((x) => x.toJson()),
"completed_at": completedAt,
"thread_id": threadId,
"file_ids": fileIds,
"started_at": startedAt,
"model": model,
"id": id,
"object": object,
"status": status,
'instructions': instructions,
'last_error': lastError,
'failed_at': failedAt,
'cancelled_at': cancelledAt,
'expires_at': expiresAt,
'type': type,
'step_details': stepDetails?.toJson(),
};
}
64 changes: 64 additions & 0 deletions lib/src/model/run/response/create_thread_and_run_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class CreateThreadAndRunData {
CreateThreadAndRunData({
required this.instructions,
required this.metadata,
required this.assistantId,
required this.createdAt,
required this.tools,
required this.threadId,
required this.expiresAt,
required this.fileIds,
required this.model,
required this.id,
required this.object,
required this.status,
});

String instructions;
Map<String, dynamic> metadata;
String assistantId;
int createdAt;
List<Map<String, dynamic>> tools;
String threadId;
int expiresAt;
List<Map<String, dynamic>> fileIds;
String model;
String id;
String object;
String status;

factory CreateThreadAndRunData.fromJson(Map<String, dynamic> json) =>
CreateThreadAndRunData(
instructions: json["instructions"] ?? '',
metadata: json["metadata"] ?? {},
assistantId: json["assistant_id"] ?? '',
createdAt: json["created_at"] ?? '',
tools: json["tools"] == null
? []
: List<Map<String, dynamic>>.from(json["tools"].map((x) => x)),
threadId: json["thread_id"] ?? '',
expiresAt: json["expires_at"] ?? '',
fileIds: json["file_ids"] == null
? []
: List<Map<String, dynamic>>.from(json["file_ids"].map((x) => x)),
model: json["model"] ?? '',
id: json["id"] ?? '',
object: json["object"] ?? '',
status: json["status"] ?? '',
);

Map<String, dynamic> toJson() => {
"instructions": instructions,
"metadata": metadata,
"assistant_id": assistantId,
"created_at": createdAt,
"tools": tools,
"thread_id": threadId,
"expires_at": expiresAt,
"file_ids": fileIds,
"model": model,
"id": id,
"object": object,
"status": status,
};
}
37 changes: 37 additions & 0 deletions lib/src/model/run/response/list_run.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:chat_gpt_sdk/src/model/run/response/create_run_response.dart';

class ListRun {
ListRun({
required this.firstId,
required this.data,
required this.lastId,
required this.hasMore,
required this.object,
});

String firstId;
List<CreateRunResponse> data;
String lastId;
bool hasMore;
String object;

factory ListRun.fromJson(Map<String, dynamic> json) => ListRun(
firstId: json["first_id"],
data: json["data"] == null
? []
: List<CreateRunResponse>.from(
json["data"].map((x) => CreateRunResponse.fromJson(x)),
),
lastId: json["last_id"],
hasMore: json["has_more"],
object: json["object"],
);

Map<String, dynamic> toJson() => {
"first_id": firstId,
"data": data.map((x) => x.toJson()).toList(),
"last_id": lastId,
"has_more": hasMore,
"object": object,
};
}
16 changes: 16 additions & 0 deletions lib/src/model/run/response/message_creation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class MessageCreation {
MessageCreation({
required this.messageId,
});

String messageId;

factory MessageCreation.fromJson(Map<String, dynamic> json) =>
MessageCreation(
messageId: json["message_id"] ?? '',
);

Map<String, dynamic> toJson() => {
"message_id": messageId,
};
}
Loading

0 comments on commit 8f354d6

Please sign in to comment.