Struct openai_api_rs::v1::api::Client
source · pub struct Client {
pub api_endpoint: String,
pub api_key: String,
pub organization: Option<String>,
}
Fields§
§api_endpoint: String
§api_key: String
§organization: Option<String>
Implementations§
source§impl Client
impl Client
sourcepub fn new(api_key: String) -> Self
pub fn new(api_key: String) -> Self
Examples found in repository?
examples/embedding.rs (line 6)
5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = EmbeddingRequest::new(
"text-embedding-ada-002".to_string(),
"story time".to_string(),
);
let result = client.embedding(req)?;
println!("{:?}", result.data);
Ok(())
}
More examples
examples/text_to_speech.rs (line 6)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = AudioSpeechRequest::new(
TTS_1.to_string(),
String::from("Money is not problem, Problem is no money"),
audio::VOICE_ALLOY.to_string(),
String::from("problem.mp3"),
);
let result = client.audio_speech(req)?;
println!("{:?}", result);
Ok(())
}
examples/completion.rs (line 6)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = CompletionRequest::new(
completion::GPT3_TEXT_DAVINCI_003.to_string(),
String::from("What is Bitcoin?"),
)
.max_tokens(3000)
.temperature(0.9)
.top_p(1.0)
.stop(vec![String::from(" Human:"), String::from(" AI:")])
.presence_penalty(0.6)
.frequency_penalty(0.0);
let result = client.completion(req)?;
println!("{:}", result.choices[0].text);
Ok(())
}
examples/chat_completion.rs (line 7)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest::new(
GPT4.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
name: None,
}],
);
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}
examples/vision.rs (line 7)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest::new(
GPT4_VISION_PREVIEW.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::ImageUrl(vec![
chat_completion::ImageUrl {
r#type: chat_completion::ContentType::text,
text: Some(String::from("What’s in this image?")),
image_url: None,
},
chat_completion::ImageUrl {
r#type: chat_completion::ContentType::image_url,
text: None,
image_url: Some(chat_completion::ImageUrlType {
url: String::from(
"https://upload.wikimedia.org/wikipedia/commons/5/50/Bitcoin.png",
),
}),
},
]),
name: None,
}],
);
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}
examples/assistant.rs (line 11)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut tools = HashMap::new();
tools.insert("type".to_string(), "code_interpreter".to_string());
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
let req = req
.clone()
.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().tools(vec![tools]);
println!("{:?}", req);
let result = client.create_assistant(req)?;
println!("{:?}", result.id);
let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
println!("{:?}", thread_result.id.clone());
let message_req = CreateMessageRequest::new(
MessageRole::user,
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
);
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
println!("{:?}", message_result.id.clone());
let run_req = CreateRunRequest::new(result.id);
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.unwrap();
if run_result.status == "completed" {
break;
} else {
println!("waiting...");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
"{:?}: {:?} {:?}",
data.role, content.text.value, content.text.annotations
);
}
}
Ok(())
}
Additional examples can be found in:
pub fn new_with_endpoint(api_endpoint: String, api_key: String) -> Self
pub fn new_with_organization(api_key: String, organization: String) -> Self
pub fn build_request(&self, request: Request, is_beta: bool) -> Request
pub fn post<T: Serialize>( &self, path: &str, params: &T ) -> Result<Response, APIError>
pub fn get(&self, path: &str) -> Result<Response, APIError>
pub fn delete(&self, path: &str) -> Result<Response, APIError>
sourcepub fn completion(
&self,
req: CompletionRequest
) -> Result<CompletionResponse, APIError>
pub fn completion( &self, req: CompletionRequest ) -> Result<CompletionResponse, APIError>
Examples found in repository?
examples/completion.rs (line 19)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = CompletionRequest::new(
completion::GPT3_TEXT_DAVINCI_003.to_string(),
String::from("What is Bitcoin?"),
)
.max_tokens(3000)
.temperature(0.9)
.top_p(1.0)
.stop(vec![String::from(" Human:"), String::from(" AI:")])
.presence_penalty(0.6)
.frequency_penalty(0.0);
let result = client.completion(req)?;
println!("{:}", result.choices[0].text);
Ok(())
}
pub fn edit(&self, req: EditRequest) -> Result<EditResponse, APIError>
pub fn image_generation( &self, req: ImageGenerationRequest ) -> Result<ImageGenerationResponse, APIError>
pub fn image_edit( &self, req: ImageEditRequest ) -> Result<ImageEditResponse, APIError>
pub fn image_variation( &self, req: ImageVariationRequest ) -> Result<ImageVariationResponse, APIError>
sourcepub fn embedding(
&self,
req: EmbeddingRequest
) -> Result<EmbeddingResponse, APIError>
pub fn embedding( &self, req: EmbeddingRequest ) -> Result<EmbeddingResponse, APIError>
Examples found in repository?
examples/embedding.rs (line 13)
5 6 7 8 9 10 11 12 13 14 15 16 17
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = EmbeddingRequest::new(
"text-embedding-ada-002".to_string(),
"story time".to_string(),
);
let result = client.embedding(req)?;
println!("{:?}", result.data);
Ok(())
}
pub fn file_list(&self) -> Result<FileListResponse, APIError>
pub fn file_upload( &self, req: FileUploadRequest ) -> Result<FileUploadResponse, APIError>
pub fn file_delete( &self, req: FileDeleteRequest ) -> Result<FileDeleteResponse, APIError>
pub fn file_retrieve( &self, req: FileRetrieveRequest ) -> Result<FileRetrieveResponse, APIError>
pub fn file_retrieve_content( &self, req: FileRetrieveContentRequest ) -> Result<FileRetrieveContentResponse, APIError>
sourcepub fn chat_completion(
&self,
req: ChatCompletionRequest
) -> Result<ChatCompletionResponse, APIError>
pub fn chat_completion( &self, req: ChatCompletionRequest ) -> Result<ChatCompletionResponse, APIError>
Examples found in repository?
examples/chat_completion.rs (line 18)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest::new(
GPT4.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is bitcoin?")),
name: None,
}],
);
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}
More examples
examples/vision.rs (line 33)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = ChatCompletionRequest::new(
GPT4_VISION_PREVIEW.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::ImageUrl(vec![
chat_completion::ImageUrl {
r#type: chat_completion::ContentType::text,
text: Some(String::from("What’s in this image?")),
image_url: None,
},
chat_completion::ImageUrl {
r#type: chat_completion::ContentType::image_url,
text: None,
image_url: Some(chat_completion::ImageUrlType {
url: String::from(
"https://upload.wikimedia.org/wikipedia/commons/5/50/Bitcoin.png",
),
}),
},
]),
name: None,
}],
);
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
Ok(())
}
examples/function_call.rs (line 56)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut properties = HashMap::new();
properties.insert(
"coin".to_string(),
Box::new(chat_completion::JSONSchemaDefine {
schema_type: Some(chat_completion::JSONSchemaType::String),
description: Some("The cryptocurrency to get the price of".to_string()),
..Default::default()
}),
);
let req = ChatCompletionRequest::new(
GPT3_5_TURBO_0613.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
name: None,
}],
)
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}])
.tool_choice(chat_completion::ToolChoiceType::Auto);
// debug request json
// let serialized = serde_json::to_string(&req).unwrap();
// println!("{}", serialized);
let result = client.chat_completion(req)?;
match result.choices[0].finish_reason {
None => {
println!("No finish_reason");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::stop) => {
println!("Stop");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Serialize, Deserialize)]
struct Currency {
coin: String,
}
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let name = tool_call.function.name.clone().unwrap();
let arguments = tool_call.function.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
if name == "get_coin_price" {
let price = get_coin_price(&coin);
println!("{} price: {}", coin, price);
}
}
}
Some(chat_completion::FinishReason::content_filter) => {
println!("ContentFilter");
}
Some(chat_completion::FinishReason::null) => {
println!("Null");
}
}
Ok(())
}
examples/function_call_role.rs (line 51)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut properties = HashMap::new();
properties.insert(
"coin".to_string(),
Box::new(chat_completion::JSONSchemaDefine {
schema_type: Some(chat_completion::JSONSchemaType::String),
description: Some("The cryptocurrency to get the price of".to_string()),
..Default::default()
}),
);
let req = ChatCompletionRequest::new(
GPT3_5_TURBO_0613.to_string(),
vec![chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from("What is the price of Ethereum?")),
name: None,
}],
)
.tools(vec![chat_completion::Tool {
r#type: chat_completion::ToolType::Function,
function: chat_completion::Function {
name: String::from("get_coin_price"),
description: Some(String::from("Get the price of a cryptocurrency")),
parameters: chat_completion::FunctionParameters {
schema_type: chat_completion::JSONSchemaType::Object,
properties: Some(properties),
required: Some(vec![String::from("coin")]),
},
},
}]);
let result = client.chat_completion(req)?;
match result.choices[0].finish_reason {
None => {
println!("No finish_reason");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::stop) => {
println!("Stop");
println!("{:?}", result.choices[0].message.content);
}
Some(chat_completion::FinishReason::length) => {
println!("Length");
}
Some(chat_completion::FinishReason::tool_calls) => {
println!("ToolCalls");
#[derive(Serialize, Deserialize)]
struct Currency {
coin: String,
}
let tool_calls = result.choices[0].message.tool_calls.as_ref().unwrap();
for tool_call in tool_calls {
let function_call = &tool_call.function;
let arguments = function_call.arguments.clone().unwrap();
let c: Currency = serde_json::from_str(&arguments)?;
let coin = c.coin;
println!("coin: {}", coin);
let price = get_coin_price(&coin);
println!("price: {}", price);
let req = ChatCompletionRequest::new(
GPT3_5_TURBO_0613.to_string(),
vec![
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::user,
content: chat_completion::Content::Text(String::from(
"What is the price of Ethereum?",
)),
name: None,
},
chat_completion::ChatCompletionMessage {
role: chat_completion::MessageRole::function,
content: chat_completion::Content::Text({
let price = get_coin_price(&coin);
format!("{{\"price\": {}}}", price)
}),
name: Some(String::from("get_coin_price")),
},
],
);
let result = client.chat_completion(req)?;
println!("{:?}", result.choices[0].message.content);
}
}
Some(chat_completion::FinishReason::content_filter) => {
println!("ContentFilter");
}
Some(chat_completion::FinishReason::null) => {
println!("Null");
}
}
Ok(())
}
pub fn audio_transcription( &self, req: AudioTranscriptionRequest ) -> Result<AudioTranscriptionResponse, APIError>
pub fn audio_translation( &self, req: AudioTranslationRequest ) -> Result<AudioTranslationResponse, APIError>
sourcepub fn audio_speech(
&self,
req: AudioSpeechRequest
) -> Result<AudioSpeechResponse, APIError>
pub fn audio_speech( &self, req: AudioSpeechRequest ) -> Result<AudioSpeechResponse, APIError>
Examples found in repository?
examples/text_to_speech.rs (line 15)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let req = AudioSpeechRequest::new(
TTS_1.to_string(),
String::from("Money is not problem, Problem is no money"),
audio::VOICE_ALLOY.to_string(),
String::from("problem.mp3"),
);
let result = client.audio_speech(req)?;
println!("{:?}", result);
Ok(())
}
pub fn create_fine_tune( &self, req: CreateFineTuneRequest ) -> Result<CreateFineTuneResponse, APIError>
pub fn list_fine_tune(&self) -> Result<ListFineTuneResponse, APIError>
pub fn retrieve_fine_tune( &self, req: RetrieveFineTuneRequest ) -> Result<RetrieveFineTuneResponse, APIError>
pub fn cancel_fine_tune( &self, req: CancelFineTuneRequest ) -> Result<CancelFineTuneResponse, APIError>
pub fn list_fine_tune_events( &self, req: ListFineTuneEventsRequest ) -> Result<ListFineTuneEventsResponse, APIError>
pub fn delete_fine_tune( &self, req: DeleteFineTuneModelRequest ) -> Result<DeleteFineTuneModelResponse, APIError>
pub fn create_moderation( &self, req: CreateModerationRequest ) -> Result<CreateModerationResponse, APIError>
sourcepub fn create_assistant(
&self,
req: AssistantRequest
) -> Result<AssistantObject, APIError>
pub fn create_assistant( &self, req: AssistantRequest ) -> Result<AssistantObject, APIError>
Examples found in repository?
examples/assistant.rs (line 24)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut tools = HashMap::new();
tools.insert("type".to_string(), "code_interpreter".to_string());
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
let req = req
.clone()
.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().tools(vec![tools]);
println!("{:?}", req);
let result = client.create_assistant(req)?;
println!("{:?}", result.id);
let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
println!("{:?}", thread_result.id.clone());
let message_req = CreateMessageRequest::new(
MessageRole::user,
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
);
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
println!("{:?}", message_result.id.clone());
let run_req = CreateRunRequest::new(result.id);
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.unwrap();
if run_result.status == "completed" {
break;
} else {
println!("waiting...");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
"{:?}: {:?} {:?}",
data.role, content.text.value, content.text.annotations
);
}
}
Ok(())
}
pub fn retrieve_assistant( &self, assistant_id: String ) -> Result<AssistantObject, APIError>
pub fn modify_assistant( &self, assistant_id: String, req: AssistantRequest ) -> Result<AssistantObject, APIError>
pub fn delete_assistant( &self, assistant_id: String ) -> Result<DeletionStatus, APIError>
pub fn list_assistant( &self, limit: Option<i64>, order: Option<String>, after: Option<String>, before: Option<String> ) -> Result<ListAssistant, APIError>
pub fn create_assistant_file( &self, assistant_id: String, req: AssistantFileRequest ) -> Result<AssistantFileObject, APIError>
pub fn retrieve_assistant_file( &self, assistant_id: String, file_id: String ) -> Result<AssistantFileObject, APIError>
pub fn delete_assistant_file( &self, assistant_id: String, file_id: String ) -> Result<DeletionStatus, APIError>
pub fn list_assistant_file( &self, assistant_id: String, limit: Option<i64>, order: Option<String>, after: Option<String>, before: Option<String> ) -> Result<ListAssistantFile, APIError>
sourcepub fn create_thread(
&self,
req: CreateThreadRequest
) -> Result<ThreadObject, APIError>
pub fn create_thread( &self, req: CreateThreadRequest ) -> Result<ThreadObject, APIError>
Examples found in repository?
examples/assistant.rs (line 28)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut tools = HashMap::new();
tools.insert("type".to_string(), "code_interpreter".to_string());
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
let req = req
.clone()
.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().tools(vec![tools]);
println!("{:?}", req);
let result = client.create_assistant(req)?;
println!("{:?}", result.id);
let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
println!("{:?}", thread_result.id.clone());
let message_req = CreateMessageRequest::new(
MessageRole::user,
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
);
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
println!("{:?}", message_result.id.clone());
let run_req = CreateRunRequest::new(result.id);
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.unwrap();
if run_result.status == "completed" {
break;
} else {
println!("waiting...");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
"{:?}: {:?} {:?}",
data.role, content.text.value, content.text.annotations
);
}
}
Ok(())
}
pub fn retrieve_thread( &self, thread_id: String ) -> Result<ThreadObject, APIError>
pub fn modify_thread( &self, thread_id: String, req: ModifyThreadRequest ) -> Result<ThreadObject, APIError>
pub fn delete_thread( &self, thread_id: String ) -> Result<DeletionStatus, APIError>
sourcepub fn create_message(
&self,
thread_id: String,
req: CreateMessageRequest
) -> Result<MessageObject, APIError>
pub fn create_message( &self, thread_id: String, req: CreateMessageRequest ) -> Result<MessageObject, APIError>
Examples found in repository?
examples/assistant.rs (line 36)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut tools = HashMap::new();
tools.insert("type".to_string(), "code_interpreter".to_string());
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
let req = req
.clone()
.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().tools(vec![tools]);
println!("{:?}", req);
let result = client.create_assistant(req)?;
println!("{:?}", result.id);
let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
println!("{:?}", thread_result.id.clone());
let message_req = CreateMessageRequest::new(
MessageRole::user,
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
);
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
println!("{:?}", message_result.id.clone());
let run_req = CreateRunRequest::new(result.id);
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.unwrap();
if run_result.status == "completed" {
break;
} else {
println!("waiting...");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
"{:?}: {:?} {:?}",
data.role, content.text.value, content.text.annotations
);
}
}
Ok(())
}
pub fn retrieve_message( &self, thread_id: String, message_id: String ) -> Result<MessageObject, APIError>
pub fn modify_message( &self, thread_id: String, message_id: String, req: ModifyMessageRequest ) -> Result<MessageObject, APIError>
sourcepub fn list_messages(&self, thread_id: String) -> Result<ListMessage, APIError>
pub fn list_messages(&self, thread_id: String) -> Result<ListMessage, APIError>
Examples found in repository?
examples/assistant.rs (line 54)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut tools = HashMap::new();
tools.insert("type".to_string(), "code_interpreter".to_string());
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
let req = req
.clone()
.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().tools(vec![tools]);
println!("{:?}", req);
let result = client.create_assistant(req)?;
println!("{:?}", result.id);
let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
println!("{:?}", thread_result.id.clone());
let message_req = CreateMessageRequest::new(
MessageRole::user,
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
);
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
println!("{:?}", message_result.id.clone());
let run_req = CreateRunRequest::new(result.id);
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.unwrap();
if run_result.status == "completed" {
break;
} else {
println!("waiting...");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
"{:?}: {:?} {:?}",
data.role, content.text.value, content.text.annotations
);
}
}
Ok(())
}
pub fn retrieve_message_file( &self, thread_id: String, message_id: String, file_id: String ) -> Result<MessageFileObject, APIError>
pub fn list_message_file( &self, thread_id: String, message_id: String, limit: Option<i64>, order: Option<String>, after: Option<String>, before: Option<String> ) -> Result<ListMessageFile, APIError>
sourcepub fn create_run(
&self,
thread_id: String,
req: CreateRunRequest
) -> Result<RunObject, APIError>
pub fn create_run( &self, thread_id: String, req: CreateRunRequest ) -> Result<RunObject, APIError>
Examples found in repository?
examples/assistant.rs (line 40)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut tools = HashMap::new();
tools.insert("type".to_string(), "code_interpreter".to_string());
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
let req = req
.clone()
.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().tools(vec![tools]);
println!("{:?}", req);
let result = client.create_assistant(req)?;
println!("{:?}", result.id);
let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
println!("{:?}", thread_result.id.clone());
let message_req = CreateMessageRequest::new(
MessageRole::user,
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
);
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
println!("{:?}", message_result.id.clone());
let run_req = CreateRunRequest::new(result.id);
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.unwrap();
if run_result.status == "completed" {
break;
} else {
println!("waiting...");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
"{:?}: {:?} {:?}",
data.role, content.text.value, content.text.annotations
);
}
}
Ok(())
}
sourcepub fn retrieve_run(
&self,
thread_id: String,
run_id: String
) -> Result<RunObject, APIError>
pub fn retrieve_run( &self, thread_id: String, run_id: String ) -> Result<RunObject, APIError>
Examples found in repository?
examples/assistant.rs (line 44)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(env::var("OPENAI_API_KEY").unwrap().to_string());
let mut tools = HashMap::new();
tools.insert("type".to_string(), "code_interpreter".to_string());
let req = AssistantRequest::new(GPT4_1106_PREVIEW.to_string());
let req = req
.clone()
.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().tools(vec![tools]);
println!("{:?}", req);
let result = client.create_assistant(req)?;
println!("{:?}", result.id);
let thread_req = CreateThreadRequest::new();
let thread_result = client.create_thread(thread_req)?;
println!("{:?}", thread_result.id.clone());
let message_req = CreateMessageRequest::new(
MessageRole::user,
"`I need to solve the equation 3x + 11 = 14. Can you help me?".to_string(),
);
let message_result = client.create_message(thread_result.id.clone(), message_req)?;
println!("{:?}", message_result.id.clone());
let run_req = CreateRunRequest::new(result.id);
let run_result = client.create_run(thread_result.id.clone(), run_req)?;
loop {
let run_result = client
.retrieve_run(thread_result.id.clone(), run_result.id.clone())
.unwrap();
if run_result.status == "completed" {
break;
} else {
println!("waiting...");
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
let list_message_result = client.list_messages(thread_result.id.clone()).unwrap();
for data in list_message_result.data {
for content in data.content {
println!(
"{:?}: {:?} {:?}",
data.role, content.text.value, content.text.annotations
);
}
}
Ok(())
}
pub fn modify_run( &self, thread_id: String, run_id: String, req: ModifyRunRequest ) -> Result<RunObject, APIError>
pub fn list_run( &self, thread_id: String, limit: Option<i64>, order: Option<String>, after: Option<String>, before: Option<String> ) -> Result<ListRun, APIError>
pub fn cancel_run( &self, thread_id: String, run_id: String ) -> Result<RunObject, APIError>
pub fn create_thread_and_run( &self, req: CreateThreadAndRunRequest ) -> Result<RunObject, APIError>
pub fn retrieve_run_step( &self, thread_id: String, run_id: String, step_id: String ) -> Result<RunStepObject, APIError>
pub fn list_run_step( &self, thread_id: String, run_id: String, limit: Option<i64>, order: Option<String>, after: Option<String>, before: Option<String> ) -> Result<ListRunStep, APIError>
Auto Trait Implementations§
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnwindSafe for Client
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more