mirror of
https://github.com/mii443/openai-api-rs.git
synced 2025-08-23 15:48:07 +00:00
@ -1,6 +1,6 @@
|
|||||||
use openai_api_rs::v1::api::Client;
|
use openai_api_rs::v1::api::Client;
|
||||||
use openai_api_rs::v1::assistant::AssistantRequest;
|
use openai_api_rs::v1::assistant::AssistantRequest;
|
||||||
use openai_api_rs::v1::common::GPT4_1106_PREVIEW;
|
use openai_api_rs::v1::common::GPT4_O;
|
||||||
use openai_api_rs::v1::message::{CreateMessageRequest, MessageRole};
|
use openai_api_rs::v1::message::{CreateMessageRequest, MessageRole};
|
||||||
use openai_api_rs::v1::run::CreateRunRequest;
|
use openai_api_rs::v1::run::CreateRunRequest;
|
||||||
use openai_api_rs::v1::thread::CreateThreadRequest;
|
use openai_api_rs::v1::thread::CreateThreadRequest;
|
||||||
@ -13,20 +13,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let mut tools = HashMap::new();
|
let mut tools = HashMap::new();
|
||||||
tools.insert("type".to_string(), "code_interpreter".to_string());
|
tools.insert("type".to_string(), "code_interpreter".to_string());
|
||||||
|
|
||||||
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
|
let req = AssistantRequest::new(GPT4_O.to_string());
|
||||||
let req = req
|
let req = req
|
||||||
.clone()
|
.clone()
|
||||||
.description("this is a test assistant".to_string());
|
.description("this is a test assistant".to_string());
|
||||||
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());
|
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());
|
||||||
let req = req.clone().tools(vec![tools]);
|
let req = req.clone().tools(vec![tools]);
|
||||||
println!("{:?}", req);
|
println!("AssistantRequest: {:?}", req);
|
||||||
|
|
||||||
let result = client.create_assistant(req)?;
|
let result = client.create_assistant(req)?;
|
||||||
println!("{:?}", result.id);
|
println!("Create Assistant Result ID: {:?}", result.id);
|
||||||
|
|
||||||
let thread_req = CreateThreadRequest::new();
|
let thread_req = CreateThreadRequest::new();
|
||||||
let thread_result = client.create_thread(thread_req)?;
|
let thread_result = client.create_thread(thread_req)?;
|
||||||
println!("{:?}", thread_result.id.clone());
|
println!("Create Thread Result ID: {:?}", thread_result.id.clone());
|
||||||
|
|
||||||
let message_req = CreateMessageRequest::new(
|
let message_req = CreateMessageRequest::new(
|
||||||
MessageRole::user,
|
MessageRole::user,
|
||||||
@ -34,10 +34,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
|
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
|
||||||
println!("{:?}", message_result.id.clone());
|
println!("Create Message Result ID: {:?}", message_result.id.clone());
|
||||||
|
|
||||||
let run_req = CreateRunRequest::new(result.id);
|
let run_req = CreateRunRequest::new(result.id);
|
||||||
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
|
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
|
||||||
|
println!("Create Run Result ID: {:?}", run_result.id.clone());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let run_result = client
|
let run_result = client
|
||||||
|
@ -93,7 +93,7 @@ impl Client {
|
|||||||
request = request.with_header("openai-organization", organization);
|
request = request.with_header("openai-organization", organization);
|
||||||
}
|
}
|
||||||
if is_beta {
|
if is_beta {
|
||||||
request = request.with_header("OpenAI-Beta", "assistants=v1");
|
request = request.with_header("OpenAI-Beta", "assistants=v2");
|
||||||
}
|
}
|
||||||
if let Some(proxy) = &self.proxy {
|
if let Some(proxy) = &self.proxy {
|
||||||
request = request.with_proxy(minreq::Proxy::new(proxy).unwrap());
|
request = request.with_proxy(minreq::Proxy::new(proxy).unwrap());
|
||||||
|
@ -15,7 +15,7 @@ pub struct AssistantRequest {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub tools: Option<Vec<HashMap<String, String>>>,
|
pub tools: Option<Vec<HashMap<String, String>>>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub file_ids: Option<Vec<String>>,
|
pub tool_resources: Option<ToolResource>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub metadata: Option<HashMap<String, String>>,
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
@ -28,7 +28,7 @@ impl AssistantRequest {
|
|||||||
description: None,
|
description: None,
|
||||||
instructions: None,
|
instructions: None,
|
||||||
tools: None,
|
tools: None,
|
||||||
file_ids: None,
|
tool_resources: None,
|
||||||
metadata: None,
|
metadata: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ impl_builder_methods!(
|
|||||||
description: String,
|
description: String,
|
||||||
instructions: String,
|
instructions: String,
|
||||||
tools: Vec<HashMap<String, String>>,
|
tools: Vec<HashMap<String, String>>,
|
||||||
file_ids: Vec<String>,
|
tool_resources: ToolResource,
|
||||||
metadata: HashMap<String, String>
|
metadata: HashMap<String, String>
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -57,11 +57,44 @@ pub struct AssistantObject {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub instructions: Option<String>,
|
pub instructions: Option<String>,
|
||||||
pub tools: Vec<HashMap<String, String>>,
|
pub tools: Vec<HashMap<String, String>>,
|
||||||
pub file_ids: Vec<String>,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub metadata: HashMap<String, String>,
|
pub tool_resources: Option<ToolResource>,
|
||||||
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
pub headers: Option<HashMap<String, String>>,
|
pub headers: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct ToolResource {
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub code_interpreter: Option<CodeInterpreter>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub file_search: Option<FileSearch>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct CodeInterpreter {
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub file_ids: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct FileSearch {
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub vector_store_ids: Option<Vec<String>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub vector_stores: Option<VectorStores>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct VectorStores {
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub file_ids: Option<Vec<String>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub chunking_strategy: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
pub struct DeletionStatus {
|
pub struct DeletionStatus {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
@ -8,7 +8,7 @@ pub struct CreateMessageRequest {
|
|||||||
pub role: MessageRole,
|
pub role: MessageRole,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub file_ids: Option<Vec<String>>,
|
pub attachments: Option<Vec<Attachment>>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub metadata: Option<HashMap<String, String>>,
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
@ -18,7 +18,7 @@ impl CreateMessageRequest {
|
|||||||
Self {
|
Self {
|
||||||
role,
|
role,
|
||||||
content,
|
content,
|
||||||
file_ids: None,
|
attachments: None,
|
||||||
metadata: None,
|
metadata: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -26,7 +26,7 @@ impl CreateMessageRequest {
|
|||||||
|
|
||||||
impl_builder_methods!(
|
impl_builder_methods!(
|
||||||
CreateMessageRequest,
|
CreateMessageRequest,
|
||||||
file_ids: Vec<String>,
|
attachments: Vec<Attachment>,
|
||||||
metadata: HashMap<String, String>
|
metadata: HashMap<String, String>
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -65,11 +65,23 @@ pub struct MessageObject {
|
|||||||
pub assistant_id: Option<String>,
|
pub assistant_id: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub run_id: Option<String>,
|
pub run_id: Option<String>,
|
||||||
pub file_ids: Vec<String>,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub metadata: HashMap<String, String>,
|
pub attachments: Option<Vec<Attachment>>,
|
||||||
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
pub headers: Option<HashMap<String, String>>,
|
pub headers: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Attachment {
|
||||||
|
pub file_id: Option<String>,
|
||||||
|
pub tools: Vec<Tool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Tool {
|
||||||
|
pub r#type: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
|
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub enum MessageRole {
|
pub enum MessageRole {
|
||||||
|
@ -85,7 +85,6 @@ pub struct RunObject {
|
|||||||
pub model: String,
|
pub model: String,
|
||||||
pub instructions: Option<String>,
|
pub instructions: Option<String>,
|
||||||
pub tools: Vec<HashMap<String, String>>,
|
pub tools: Vec<HashMap<String, String>>,
|
||||||
pub file_ids: Vec<String>,
|
|
||||||
pub metadata: HashMap<String, String>,
|
pub metadata: HashMap<String, String>,
|
||||||
pub headers: Option<HashMap<String, String>>,
|
pub headers: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,32 @@ pub struct CreateThreadRequest {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub messages: Option<Vec<Message>>,
|
pub messages: Option<Vec<Message>>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub tool_resources: Option<ToolResource>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct ToolResource {
|
||||||
|
pub code_interpreter: Option<CodeInterpreter>,
|
||||||
|
pub file_search: Option<FileSearch>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct CodeInterpreter {
|
||||||
|
pub file_ids: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct FileSearch {
|
||||||
|
pub vector_store_ids: Option<Vec<String>>,
|
||||||
|
pub vector_stores: Option<VectorStores>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct VectorStores {
|
||||||
|
pub file_ids: Option<Vec<String>>,
|
||||||
|
pub chunking_strategy: Option<String>,
|
||||||
pub metadata: Option<HashMap<String, String>>,
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,6 +41,7 @@ impl CreateThreadRequest {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
messages: None,
|
messages: None,
|
||||||
|
tool_resources: None,
|
||||||
metadata: None,
|
metadata: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,7 +56,7 @@ impl Default for CreateThreadRequest {
|
|||||||
impl_builder_methods!(
|
impl_builder_methods!(
|
||||||
CreateThreadRequest,
|
CreateThreadRequest,
|
||||||
messages: Vec<Message>,
|
messages: Vec<Message>,
|
||||||
metadata: HashMap<String, String>
|
tool_resources: ToolResource
|
||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
@ -38,17 +65,52 @@ pub struct ThreadObject {
|
|||||||
pub object: String,
|
pub object: String,
|
||||||
pub created_at: i64,
|
pub created_at: i64,
|
||||||
pub metadata: HashMap<String, String>,
|
pub metadata: HashMap<String, String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub tool_resources: Option<ToolResource>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub headers: Option<HashMap<String, String>>,
|
pub headers: Option<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
|
pub id: String,
|
||||||
|
pub object: String,
|
||||||
|
pub created_at: i64,
|
||||||
|
pub thread_id: String,
|
||||||
pub role: MessageRole,
|
pub role: MessageRole,
|
||||||
pub content: String,
|
pub content: Vec<Content>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub file_ids: Option<String>,
|
pub assistant_id: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub run_id: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub attachments: Option<Vec<Attachment>>,
|
||||||
pub metadata: Option<HashMap<String, String>>,
|
pub metadata: Option<HashMap<String, String>>,
|
||||||
|
pub headers: Option<HashMap<String, String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct Content {
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
pub content_type: String,
|
||||||
|
pub text: ContentText,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
pub struct ContentText {
|
||||||
|
pub value: String,
|
||||||
|
pub annotations: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Attachment {
|
||||||
|
pub file_id: String,
|
||||||
|
pub tools: Vec<Tool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct Tool {
|
||||||
|
pub r#type: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
|
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
|
||||||
|
Reference in New Issue
Block a user