Skip to content

Commit bfc0ec2

Browse files
authored
Merge pull request #88 from dongri/assistant-beta-v2
Assistant beta v2
2 parents 3e3844a + a0bde8f commit bfc0ec2

File tree

6 files changed

+130
-23
lines changed

6 files changed

+130
-23
lines changed

examples/assistant.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use openai_api_rs::v1::api::Client;
22
use openai_api_rs::v1::assistant::AssistantRequest;
3-
use openai_api_rs::v1::common::GPT4_1106_PREVIEW;
3+
use openai_api_rs::v1::common::GPT4_O;
44
use openai_api_rs::v1::message::{CreateMessageRequest, MessageRole};
55
use openai_api_rs::v1::run::CreateRunRequest;
66
use openai_api_rs::v1::thread::CreateThreadRequest;
@@ -13,31 +13,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1313
let mut tools = HashMap::new();
1414
tools.insert("type".to_string(), "code_interpreter".to_string());
1515

16-
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
16+
let req = AssistantRequest::new(GPT4_O.to_string());
1717
let req = req
1818
.clone()
1919
.description("this is a test assistant".to_string());
2020
let req = req.clone().instructions("You are a personal math tutor. When asked a question, write and run Python code to answer the question.".to_string());
2121
let req = req.clone().tools(vec![tools]);
22-
println!("{:?}", req);
22+
println!("AssistantRequest: {:?}", req);
2323

2424
let result = client.create_assistant(req)?;
25-
println!("{:?}", result.id);
25+
println!("Create Assistant Result ID: {:?}", result.id);
2626

2727
let thread_req = CreateThreadRequest::new();
2828
let thread_result = client.create_thread(thread_req)?;
29-
println!("{:?}", thread_result.id.clone());
29+
println!("Create Thread Result ID: {:?}", thread_result.id.clone());
3030

3131
let message_req = CreateMessageRequest::new(
3232
MessageRole::user,
3333
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
3434
);
3535

3636
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
37-
println!("{:?}", message_result.id.clone());
37+
println!("Create Message Result ID: {:?}", message_result.id.clone());
3838

3939
let run_req = CreateRunRequest::new(result.id);
4040
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
41+
println!("Create Run Result ID: {:?}", run_result.id.clone());
4142

4243
loop {
4344
let run_result = client

src/v1/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Client {
9393
request = request.with_header("openai-organization", organization);
9494
}
9595
if is_beta {
96-
request = request.with_header("OpenAI-Beta", "assistants=v1");
96+
request = request.with_header("OpenAI-Beta", "assistants=v2");
9797
}
9898
if let Some(proxy) = &self.proxy {
9999
request = request.with_proxy(minreq::Proxy::new(proxy).unwrap());

src/v1/assistant.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct AssistantRequest {
1515
#[serde(skip_serializing_if = "Option::is_none")]
1616
pub tools: Option<Vec<HashMap<String, String>>>,
1717
#[serde(skip_serializing_if = "Option::is_none")]
18-
pub file_ids: Option<Vec<String>>,
18+
pub tool_resources: Option<ToolResource>,
1919
#[serde(skip_serializing_if = "Option::is_none")]
2020
pub metadata: Option<HashMap<String, String>>,
2121
}
@@ -28,7 +28,7 @@ impl AssistantRequest {
2828
description: None,
2929
instructions: None,
3030
tools: None,
31-
file_ids: None,
31+
tool_resources: None,
3232
metadata: None,
3333
}
3434
}
@@ -40,7 +40,7 @@ impl_builder_methods!(
4040
description: String,
4141
instructions: String,
4242
tools: Vec<HashMap<String, String>>,
43-
file_ids: Vec<String>,
43+
tool_resources: ToolResource,
4444
metadata: HashMap<String, String>
4545
);
4646

@@ -57,11 +57,44 @@ pub struct AssistantObject {
5757
#[serde(skip_serializing_if = "Option::is_none")]
5858
pub instructions: Option<String>,
5959
pub tools: Vec<HashMap<String, String>>,
60-
pub file_ids: Vec<String>,
61-
pub metadata: HashMap<String, String>,
60+
#[serde(skip_serializing_if = "Option::is_none")]
61+
pub tool_resources: Option<ToolResource>,
62+
pub metadata: Option<HashMap<String, String>>,
6263
pub headers: Option<HashMap<String, String>>,
6364
}
6465

66+
#[derive(Debug, Deserialize, Serialize, Clone)]
67+
pub struct ToolResource {
68+
#[serde(skip_serializing_if = "Option::is_none")]
69+
pub code_interpreter: Option<CodeInterpreter>,
70+
#[serde(skip_serializing_if = "Option::is_none")]
71+
pub file_search: Option<FileSearch>,
72+
}
73+
74+
#[derive(Debug, Deserialize, Serialize, Clone)]
75+
pub struct CodeInterpreter {
76+
#[serde(skip_serializing_if = "Option::is_none")]
77+
pub file_ids: Option<Vec<String>>,
78+
}
79+
80+
#[derive(Debug, Deserialize, Serialize, Clone)]
81+
pub struct FileSearch {
82+
#[serde(skip_serializing_if = "Option::is_none")]
83+
pub vector_store_ids: Option<Vec<String>>,
84+
#[serde(skip_serializing_if = "Option::is_none")]
85+
pub vector_stores: Option<VectorStores>,
86+
}
87+
88+
#[derive(Debug, Deserialize, Serialize, Clone)]
89+
pub struct VectorStores {
90+
#[serde(skip_serializing_if = "Option::is_none")]
91+
pub file_ids: Option<Vec<String>>,
92+
#[serde(skip_serializing_if = "Option::is_none")]
93+
pub chunking_strategy: Option<String>,
94+
#[serde(skip_serializing_if = "Option::is_none")]
95+
pub metadata: Option<HashMap<String, String>>,
96+
}
97+
6598
#[derive(Debug, Deserialize, Serialize)]
6699
pub struct DeletionStatus {
67100
pub id: String,

src/v1/message.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct CreateMessageRequest {
88
pub role: MessageRole,
99
pub content: String,
1010
#[serde(skip_serializing_if = "Option::is_none")]
11-
pub file_ids: Option<Vec<String>>,
11+
pub attachments: Option<Vec<Attachment>>,
1212
#[serde(skip_serializing_if = "Option::is_none")]
1313
pub metadata: Option<HashMap<String, String>>,
1414
}
@@ -18,15 +18,15 @@ impl CreateMessageRequest {
1818
Self {
1919
role,
2020
content,
21-
file_ids: None,
21+
attachments: None,
2222
metadata: None,
2323
}
2424
}
2525
}
2626

2727
impl_builder_methods!(
2828
CreateMessageRequest,
29-
file_ids: Vec<String>,
29+
attachments: Vec<Attachment>,
3030
metadata: HashMap<String, String>
3131
);
3232

@@ -65,11 +65,23 @@ pub struct MessageObject {
6565
pub assistant_id: Option<String>,
6666
#[serde(skip_serializing_if = "Option::is_none")]
6767
pub run_id: Option<String>,
68-
pub file_ids: Vec<String>,
69-
pub metadata: HashMap<String, String>,
68+
#[serde(skip_serializing_if = "Option::is_none")]
69+
pub attachments: Option<Vec<Attachment>>,
70+
pub metadata: Option<HashMap<String, String>>,
7071
pub headers: Option<HashMap<String, String>>,
7172
}
7273

74+
#[derive(Serialize, Deserialize, Debug, Clone)]
75+
pub struct Attachment {
76+
pub file_id: Option<String>,
77+
pub tools: Vec<Tool>,
78+
}
79+
80+
#[derive(Serialize, Deserialize, Debug, Clone)]
81+
pub struct Tool {
82+
pub r#type: String,
83+
}
84+
7385
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
7486
#[allow(non_camel_case_types)]
7587
pub enum MessageRole {

src/v1/run.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ pub struct RunObject {
8585
pub model: String,
8686
pub instructions: Option<String>,
8787
pub tools: Vec<HashMap<String, String>>,
88-
pub file_ids: Vec<String>,
8988
pub metadata: HashMap<String, String>,
9089
pub headers: Option<HashMap<String, String>>,
9190
}

src/v1/thread.rs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,40 @@ pub struct CreateThreadRequest {
88
#[serde(skip_serializing_if = "Option::is_none")]
99
pub messages: Option<Vec<Message>>,
1010
#[serde(skip_serializing_if = "Option::is_none")]
11+
pub tool_resources: Option<ToolResource>,
12+
#[serde(skip_serializing_if = "Option::is_none")]
13+
pub metadata: Option<HashMap<String, String>>,
14+
}
15+
16+
#[derive(Debug, Deserialize, Serialize, Clone)]
17+
pub struct ToolResource {
18+
pub code_interpreter: Option<CodeInterpreter>,
19+
pub file_search: Option<FileSearch>,
20+
}
21+
22+
#[derive(Debug, Deserialize, Serialize, Clone)]
23+
pub struct CodeInterpreter {
24+
pub file_ids: Option<Vec<String>>,
25+
}
26+
27+
#[derive(Debug, Deserialize, Serialize, Clone)]
28+
pub struct FileSearch {
29+
pub vector_store_ids: Option<Vec<String>>,
30+
pub vector_stores: Option<VectorStores>,
31+
}
32+
33+
#[derive(Debug, Deserialize, Serialize, Clone)]
34+
pub struct VectorStores {
35+
pub file_ids: Option<Vec<String>>,
36+
pub chunking_strategy: Option<String>,
1137
pub metadata: Option<HashMap<String, String>>,
1238
}
1339

1440
impl CreateThreadRequest {
1541
pub fn new() -> Self {
1642
Self {
1743
messages: None,
44+
tool_resources: None,
1845
metadata: None,
1946
}
2047
}
@@ -27,9 +54,9 @@ impl Default for CreateThreadRequest {
2754
}
2855

2956
impl_builder_methods!(
30-
CreateThreadRequest,
31-
messages: Vec<Message>,
32-
metadata: HashMap<String, String>
57+
CreateThreadRequest,
58+
messages: Vec<Message>,
59+
tool_resources: ToolResource
3360
);
3461

3562
#[derive(Debug, Deserialize, Serialize)]
@@ -38,17 +65,52 @@ pub struct ThreadObject {
3865
pub object: String,
3966
pub created_at: i64,
4067
pub metadata: HashMap<String, String>,
68+
#[serde(skip_serializing_if = "Option::is_none")]
69+
pub tool_resources: Option<ToolResource>,
70+
#[serde(skip_serializing_if = "Option::is_none")]
4171
pub headers: Option<HashMap<String, String>>,
4272
}
4373

4474
#[derive(Debug, Deserialize, Serialize, Clone)]
4575
pub struct Message {
76+
pub id: String,
77+
pub object: String,
78+
pub created_at: i64,
79+
pub thread_id: String,
4680
pub role: MessageRole,
47-
pub content: String,
81+
pub content: Vec<Content>,
82+
#[serde(skip_serializing_if = "Option::is_none")]
83+
pub assistant_id: Option<String>,
4884
#[serde(skip_serializing_if = "Option::is_none")]
49-
pub file_ids: Option<String>,
85+
pub run_id: Option<String>,
5086
#[serde(skip_serializing_if = "Option::is_none")]
87+
pub attachments: Option<Vec<Attachment>>,
5188
pub metadata: Option<HashMap<String, String>>,
89+
pub headers: Option<HashMap<String, String>>,
90+
}
91+
92+
#[derive(Debug, Deserialize, Serialize, Clone)]
93+
pub struct Content {
94+
#[serde(rename = "type")]
95+
pub content_type: String,
96+
pub text: ContentText,
97+
}
98+
99+
#[derive(Debug, Deserialize, Serialize, Clone)]
100+
pub struct ContentText {
101+
pub value: String,
102+
pub annotations: Vec<String>,
103+
}
104+
105+
#[derive(Serialize, Deserialize, Debug, Clone)]
106+
pub struct Attachment {
107+
pub file_id: String,
108+
pub tools: Vec<Tool>,
109+
}
110+
111+
#[derive(Serialize, Deserialize, Debug, Clone)]
112+
pub struct Tool {
113+
pub r#type: String,
52114
}
53115

54116
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]

0 commit comments

Comments
 (0)