pub struct Completions<T: LLMModel> { /* private fields */ }Expand description
Completions APIs take a list of messages as input and return a model-generated message as output. Although the Completions format is designed to make multi-turn conversations easy, it’s just as useful for single-turn tasks without any conversation.
Implementations§
Source§impl<T: LLMModel> Completions<T>
impl<T: LLMModel> Completions<T>
Sourcepub fn new(
model: T,
api_key: &str,
max_tokens: Option<usize>,
temperature: Option<u32>,
) -> Self
pub fn new( model: T, api_key: &str, max_tokens: Option<usize>, temperature: Option<u32>, ) -> Self
Constructor for the Completions API
Examples found in repository?
36async fn main() -> Result<()> {
37 env_logger::init();
38
39 let mistral_api_key: String =
40 std::env::var("MISTRAL_API_KEY").expect("MISTRAL_API_KEY not set");
41
42 // Example 1: Web search example
43 let web_search_tool = LLMTools::MistralWebSearch(MistralWebSearchConfig::new());
44 let mistral_responses = Completions::new(
45 MistralModels::MistralMedium3_1,
46 &mistral_api_key,
47 None,
48 None,
49 )
50 .add_tool(web_search_tool);
51
52 match mistral_responses
53 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
54 For each news item, provide the title, url, and a short description.")
55 .await
56 {
57 Ok(response) => println!("AI news articles:\n{:#?}", response),
58 Err(e) => eprintln!("Error: {:?}", e),
59 }
60
61 // Example 2: Code interpreter example
62 let code_interpreter_tool =
63 LLMTools::MistralCodeInterpreter(MistralCodeInterpreterConfig::new());
64 let mistral_responses = Completions::new(
65 MistralModels::MistralMedium3_1,
66 &mistral_api_key,
67 None,
68 None,
69 )
70 .add_tool(code_interpreter_tool);
71
72 match mistral_responses
73 .get_answer::<CodeInterpreterResponse>(
74 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
75 )
76 .await
77 {
78 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
79 Err(e) => eprintln!("Error: {:?}", e),
80 }
81
82 Ok(())
83}More examples
20async fn main() -> Result<()> {
21 env_logger::init();
22
23 // Get Vertex API authentication token
24 let google_token_str = get_vertex_token().await?;
25
26 // Example context and instructions
27 let instructions =
28 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
29
30 // Get answer using Google GeminiPro via Vertex AI
31 let model = GoogleModels::Gemini2_5FlashLite;
32
33 // **Pre-requisite**: GeminiPro request through Vertex AI require `GOOGLE_PROJECT_ID` environment variable defined
34 let gemini_completion =
35 Completions::new(model, &google_token_str, None, None).version("google-vertex");
36
37 match gemini_completion
38 .get_answer::<TranslationResponse>(instructions)
39 .await
40 {
41 Ok(response) => println!("Vertex Gemini response: {:#?}", response),
42 Err(e) => eprintln!("Error: {:?}", e),
43 }
44
45 // Get answer using a fine-tuned model
46
47 // Using a fine-tuned model requires addressing the endpoint directly
48 // Replace env variable with the endpoint ID of the fine-tuned model
49 let fine_tuned_endpoint_id: String =
50 std::env::var("GOOGLE_VERTEX_ENDPOINT_ID").expect("GOOGLE_VERTEX_ENDPOINT_ID not set");
51 let model = GoogleModels::endpoint(&fine_tuned_endpoint_id);
52
53 let gemini_completion =
54 Completions::new(model, &google_token_str, None, None).version("google-vertex");
55
56 match gemini_completion
57 .get_answer::<TranslationResponse>(instructions)
58 .await
59 {
60 Ok(response) => println!("Vertex Gemini response: {:#?}", response),
61 Err(e) => eprintln!("Error: {:?}", e),
62 }
63
64 Ok(())
65}42async fn main() -> Result<()> {
43 env_logger::init();
44
45 let xai_api_key: String = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
46
47 // Example 1: Web search example with domain filters
48 let web_search_config = XAIWebSearchConfig::new()
49 .add_allowed_domains(&["techcrunch.com".to_string(), "wired.com".to_string()])
50 .with_enable_image_understanding(true);
51
52 let web_search_tool = LLMTools::XAIWebSearch(web_search_config);
53 let xai_responses =
54 Completions::new(XAIModels::Grok4_1FastNonReasoning, &xai_api_key, None, None)
55 .add_tool(web_search_tool);
56
57 match xai_responses
58 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
59 For each news item, provide the title, url, and a short description.")
60 .await
61 {
62 Ok(response) => println!("AI news articles:\n{:#?}", response),
63 Err(e) => eprintln!("Error: {:?}", e),
64 }
65
66 // Example 2: X Search example with date range and handle filters
67 let x_search_config = XAIXSearchConfig::new()
68 .from_date("2025-01-01".to_string())
69 .to_date("2025-12-31".to_string())
70 .add_allowed_x_handles(&["@elonmusk".to_string(), "@OpenAI".to_string()])
71 .enable_image_understanding(true)
72 .enable_video_understanding(true);
73
74 let x_search_tool = LLMTools::XAIXSearch(x_search_config);
75 let xai_responses_x =
76 Completions::new(XAIModels::Grok4_1FastReasoning, &xai_api_key, None, None)
77 .add_tool(x_search_tool);
78
79 match xai_responses_x
80 .get_answer::<XPosts>(
81 "Find up to 5 recent posts about AI and machine learning from the specified accounts.
82 For each post, provide the author handle, content, and if available, the URL and date.",
83 )
84 .await
85 {
86 Ok(response) => println!("X posts:\n{:#?}", response),
87 Err(e) => eprintln!("Error: {:?}", e),
88 }
89
90 Ok(())
91}62async fn main() -> Result<()> {
63 env_logger::init();
64
65 let anthropic_api_key: String =
66 std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY not set");
67
68 // Example 1: Web search example
69 let web_search_tool = LLMTools::AnthropicWebSearch(AnthropicWebSearchConfig::new());
70 let anthropic_responses = Completions::new(
71 AnthropicModels::ClaudeOpus4_7,
72 &anthropic_api_key,
73 None,
74 None,
75 )
76 .add_tool(web_search_tool);
77
78 match anthropic_responses
79 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
80 For each news item, provide the title, url, and a short description.")
81 .await
82 {
83 Ok(response) => println!("AI news articles:\n{:#?}", response),
84 Err(e) => eprintln!("Error: {:?}", e),
85 }
86
87 // Example 2: Code interpreter example
88
89 let code_interpreter_tool =
90 LLMTools::AnthropicCodeExecution(AnthropicCodeExecutionConfig::new());
91 let anthropic_responses = Completions::new(
92 AnthropicModels::ClaudeOpus4_7,
93 &anthropic_api_key,
94 None,
95 None,
96 )
97 .add_tool(code_interpreter_tool);
98
99 match anthropic_responses
100 .get_answer::<CodeInterpreterResponse>(
101 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
102 )
103 .await
104 {
105 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
106 Err(e) => eprintln!("Error: {:?}", e),
107 }
108
109 // Example 3: File search example
110
111 // Read the concert file and upload it to Anthropic
112 let path = Path::new("metallica.pdf");
113 let bytes = std::fs::read(path)?;
114 let file_name = path
115 .file_name()
116 .and_then(OsStr::to_str)
117 .map(|s| s.to_string())
118 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
119
120 let anthropic_file = AnthropicFile::new(None, &anthropic_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123
124 // Extract concert information using Anthropic API with file search tool
125 let file_search_tool = LLMTools::AnthropicFileSearch(AnthropicFileSearchConfig::new(
126 anthropic_file.id.clone().unwrap_or_default(),
127 ));
128
129 let anthropic_responses = Completions::new(
130 AnthropicModels::ClaudeSonnet4_6,
131 &anthropic_api_key,
132 None,
133 None,
134 )
135 .set_context("bands_genres", &BANDS_GENRES)?
136 .add_tool(file_search_tool);
137
138 match anthropic_responses
139 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
140 The response should include the genre of the music the 'band' represents.
141 The mapping of bands to genres was provided in 'bands_genres' list.")
142 .await
143 {
144 Ok(response) => println!("Concert Info:\n{:#?}", response),
145 Err(e) => eprintln!("Error: {:?}", e),
146 }
147
148 // Cleanup
149 anthropic_file.delete().await?;
150
151 Ok(())
152}39async fn main() -> Result<()> {
40 env_logger::init();
41
42 let google_api_key: String =
43 std::env::var("GOOGLE_AI_STUDIO_API_KEY").expect("GOOGLE_AI_STUDIO_API_KEY not set");
44 let vertex_token = get_vertex_token().await?;
45
46 // Example 1A: Web search example (with Studio API)
47 let web_search_config =
48 GeminiWebSearchConfig::new().add_source("https://www.artificialintelligence-news.com/");
49
50 let web_search_tool = LLMTools::GeminiWebSearch(web_search_config);
51 let google_responses =
52 Completions::new(GoogleModels::Gemini3Flash, &google_api_key, None, None)
53 .add_tool(web_search_tool.clone())
54 .thinking_level(ThinkingLevel::Low);
55
56 match google_responses
57 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
58 For each news item, provide the title, url, and a short description.")
59 .await
60 {
61 Ok(response) => println!("[AI Studio] AI news articles:\n{:#?}", response),
62 Err(e) => eprintln!("[AI Studio] AI news articles error: {:?}", e),
63 }
64
65 // Example 1B: Web search example (with Vertex API)
66 let google_responses_vertex =
67 Completions::new(GoogleModels::Gemini3_1FlashLite, &vertex_token, None, None)
68 .add_tool(web_search_tool)
69 .version("google-vertex");
70
71 match google_responses_vertex
72 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
73 For each news item, provide the title, url, and a short description.")
74 .await
75 {
76 Ok(response) => println!("[Vertex] AI news articles:\n{:#?}", response),
77 Err(e) => eprintln!("[Vertex] AI news articles error: {:?}", e),
78 }
79
80 // Example 2A: Code interpreter example (with Studio API)
81 let code_interpreter_tool = LLMTools::GeminiCodeInterpreter(GeminiCodeInterpreterConfig::new());
82 let google_responses =
83 Completions::new(GoogleModels::Gemini3_1Pro, &google_api_key, None, None)
84 .add_tool(code_interpreter_tool.clone());
85
86 match google_responses
87 .get_answer::<CodeInterpreterResponse>(
88 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
89 )
90 .await
91 {
92 Ok(response) => println!("[AI Studio] Code interpreter response:\n{:#?}", response),
93 Err(e) => eprintln!("[AI Studio] Code interpreter error: {:?}", e),
94 }
95
96 // Example 2B: Code interpreter example (with Vertex API)
97 let google_responses_vertex =
98 Completions::new(GoogleModels::Gemini3_1Pro, &vertex_token, None, None)
99 .add_tool(code_interpreter_tool)
100 .version("google-vertex");
101
102 match google_responses_vertex
103 .get_answer::<CodeInterpreterResponse>(
104 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
105 )
106 .await
107 {
108 Ok(response) => println!("[Vertex] Code interpreter response:\n{:#?}", response),
109 Err(e) => eprintln!("[Vertex] Code interpreter error: {:?}", e),
110 }
111
112 Ok(())
113}72async fn main() -> Result<()> {
73 env_logger::init();
74
75 // Example 1: Basic translation example using reasoning model
76 let instructions =
77 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
78
79 let openai_api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
80
81 let reasoning_tool = LLMTools::OpenAIReasoning(OpenAIReasoningConfig::default());
82
83 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
84 .version("openai_responses")
85 .add_tool(reasoning_tool);
86
87 match openai_responses
88 .get_answer::<TranslationResponse>(instructions)
89 .await
90 {
91 Ok(response) => println!("Translations:\n{:#?}", response),
92 Err(e) => eprintln!("Error: {:?}", e),
93 }
94
95 // Example 2: Web search example
96 let web_search_tool = LLMTools::OpenAIWebSearch(OpenAIWebSearchConfig::new());
97 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
98 .version("openai_responses")
99 .add_tool(web_search_tool);
100
101 match openai_responses
102 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
103 For each news item, provide the title, url, and a short description.")
104 .await
105 {
106 Ok(response) => println!("AI news articles:\n{:#?}", response),
107 Err(e) => eprintln!("Error: {:?}", e),
108 }
109
110 // Example 3: File search example
111
112 // Read the concert file and upload it to OpenAI
113 let path = Path::new("metallica.pdf");
114 let bytes = std::fs::read(path)?;
115 let file_name = path
116 .file_name()
117 .and_then(OsStr::to_str)
118 .map(|s| s.to_string())
119 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
120 let openai_file = OpenAIFile::new(None, &openai_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123 let openai_vector_store = OpenAIVectorStore::new(None, "Concerts", &openai_api_key)
124 .upload(&[openai_file.id.clone().unwrap_or_default()])
125 .await?;
126
127 // Extract concert information using Responses API with file search tool
128 let file_search_tool =
129 LLMTools::OpenAIFileSearch(OpenAIFileSearchConfig::new(vec![openai_vector_store
130 .id
131 .clone()
132 .unwrap_or_default()]));
133
134 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
135 .version("openai_responses")
136 .set_context("bands_genres", &BANDS_GENRES)?
137 .add_tool(file_search_tool);
138
139 match openai_responses
140 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
141 The response should include the genre of the music the 'band' represents.
142 The mapping of bands to genres was provided in 'bands_genres' list.")
143 .await
144 {
145 Ok(response) => println!("Concert Info:\n{:#?}", response),
146 Err(e) => eprintln!("Error: {:?}", e),
147 }
148
149 // Cleanup
150 openai_file.delete().await?;
151 openai_vector_store.delete().await?;
152
153 // Example 4: Code interpreter example
154
155 let code_interpreter_tool = LLMTools::OpenAICodeInterpreter(OpenAICodeInterpreterConfig::new());
156 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
157 .version("openai_responses")
158 .set_context("Code Interpreter", &"You are a personal math tutor. When asked a math question, write and run code to answer the question.".to_string())?
159 .add_tool(code_interpreter_tool);
160
161 match openai_responses
162 .get_answer::<CodeInterpreterResponse>(
163 "I need to solve the equation 3x + 11 = 14. Can you help me?",
164 )
165 .await
166 {
167 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
168 Err(e) => eprintln!("Error: {:?}", e),
169 }
170
171 Ok(())
172}Sourcepub fn debug(self) -> Self
pub fn debug(self) -> Self
This function turns on debug mode which will info! the prompt to log when executing it.
Sourcepub fn function_calling(self, function_call: bool) -> Self
pub fn function_calling(self, function_call: bool) -> Self
This function turns on/off function calling mode when interacting with OpenAI API.
Sourcepub fn temperature(self, temp_target: u32) -> Self
pub fn temperature(self, temp_target: u32) -> Self
This method can be used to define the model temperature used by the Assistant This method accepts % target of the acceptable range for the model
Sourcepub fn temperature_unchecked(self, temp: f32) -> Self
pub fn temperature_unchecked(self, temp: f32) -> Self
This method can be used to define the model temperature used by the Assistant
Using this method the temperature can be set directly without any validation of the range accepted by the model
For a range-safe implementation please consider using OpenAIAssistant::temperature method
Sourcepub fn version(self, version: &str) -> Self
pub fn version(self, version: &str) -> Self
This method can be used to set the version of Completions API to be used This is currently used for OpenAI models which can be run on OpenAI API or Azure API
Examples found in repository?
20async fn main() -> Result<()> {
21 env_logger::init();
22
23 // Get Vertex API authentication token
24 let google_token_str = get_vertex_token().await?;
25
26 // Example context and instructions
27 let instructions =
28 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
29
30 // Get answer using Google GeminiPro via Vertex AI
31 let model = GoogleModels::Gemini2_5FlashLite;
32
33 // **Pre-requisite**: GeminiPro request through Vertex AI require `GOOGLE_PROJECT_ID` environment variable defined
34 let gemini_completion =
35 Completions::new(model, &google_token_str, None, None).version("google-vertex");
36
37 match gemini_completion
38 .get_answer::<TranslationResponse>(instructions)
39 .await
40 {
41 Ok(response) => println!("Vertex Gemini response: {:#?}", response),
42 Err(e) => eprintln!("Error: {:?}", e),
43 }
44
45 // Get answer using a fine-tuned model
46
47 // Using a fine-tuned model requires addressing the endpoint directly
48 // Replace env variable with the endpoint ID of the fine-tuned model
49 let fine_tuned_endpoint_id: String =
50 std::env::var("GOOGLE_VERTEX_ENDPOINT_ID").expect("GOOGLE_VERTEX_ENDPOINT_ID not set");
51 let model = GoogleModels::endpoint(&fine_tuned_endpoint_id);
52
53 let gemini_completion =
54 Completions::new(model, &google_token_str, None, None).version("google-vertex");
55
56 match gemini_completion
57 .get_answer::<TranslationResponse>(instructions)
58 .await
59 {
60 Ok(response) => println!("Vertex Gemini response: {:#?}", response),
61 Err(e) => eprintln!("Error: {:?}", e),
62 }
63
64 Ok(())
65}More examples
39async fn main() -> Result<()> {
40 env_logger::init();
41
42 let google_api_key: String =
43 std::env::var("GOOGLE_AI_STUDIO_API_KEY").expect("GOOGLE_AI_STUDIO_API_KEY not set");
44 let vertex_token = get_vertex_token().await?;
45
46 // Example 1A: Web search example (with Studio API)
47 let web_search_config =
48 GeminiWebSearchConfig::new().add_source("https://www.artificialintelligence-news.com/");
49
50 let web_search_tool = LLMTools::GeminiWebSearch(web_search_config);
51 let google_responses =
52 Completions::new(GoogleModels::Gemini3Flash, &google_api_key, None, None)
53 .add_tool(web_search_tool.clone())
54 .thinking_level(ThinkingLevel::Low);
55
56 match google_responses
57 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
58 For each news item, provide the title, url, and a short description.")
59 .await
60 {
61 Ok(response) => println!("[AI Studio] AI news articles:\n{:#?}", response),
62 Err(e) => eprintln!("[AI Studio] AI news articles error: {:?}", e),
63 }
64
65 // Example 1B: Web search example (with Vertex API)
66 let google_responses_vertex =
67 Completions::new(GoogleModels::Gemini3_1FlashLite, &vertex_token, None, None)
68 .add_tool(web_search_tool)
69 .version("google-vertex");
70
71 match google_responses_vertex
72 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
73 For each news item, provide the title, url, and a short description.")
74 .await
75 {
76 Ok(response) => println!("[Vertex] AI news articles:\n{:#?}", response),
77 Err(e) => eprintln!("[Vertex] AI news articles error: {:?}", e),
78 }
79
80 // Example 2A: Code interpreter example (with Studio API)
81 let code_interpreter_tool = LLMTools::GeminiCodeInterpreter(GeminiCodeInterpreterConfig::new());
82 let google_responses =
83 Completions::new(GoogleModels::Gemini3_1Pro, &google_api_key, None, None)
84 .add_tool(code_interpreter_tool.clone());
85
86 match google_responses
87 .get_answer::<CodeInterpreterResponse>(
88 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
89 )
90 .await
91 {
92 Ok(response) => println!("[AI Studio] Code interpreter response:\n{:#?}", response),
93 Err(e) => eprintln!("[AI Studio] Code interpreter error: {:?}", e),
94 }
95
96 // Example 2B: Code interpreter example (with Vertex API)
97 let google_responses_vertex =
98 Completions::new(GoogleModels::Gemini3_1Pro, &vertex_token, None, None)
99 .add_tool(code_interpreter_tool)
100 .version("google-vertex");
101
102 match google_responses_vertex
103 .get_answer::<CodeInterpreterResponse>(
104 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
105 )
106 .await
107 {
108 Ok(response) => println!("[Vertex] Code interpreter response:\n{:#?}", response),
109 Err(e) => eprintln!("[Vertex] Code interpreter error: {:?}", e),
110 }
111
112 Ok(())
113}72async fn main() -> Result<()> {
73 env_logger::init();
74
75 // Example 1: Basic translation example using reasoning model
76 let instructions =
77 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
78
79 let openai_api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
80
81 let reasoning_tool = LLMTools::OpenAIReasoning(OpenAIReasoningConfig::default());
82
83 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
84 .version("openai_responses")
85 .add_tool(reasoning_tool);
86
87 match openai_responses
88 .get_answer::<TranslationResponse>(instructions)
89 .await
90 {
91 Ok(response) => println!("Translations:\n{:#?}", response),
92 Err(e) => eprintln!("Error: {:?}", e),
93 }
94
95 // Example 2: Web search example
96 let web_search_tool = LLMTools::OpenAIWebSearch(OpenAIWebSearchConfig::new());
97 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
98 .version("openai_responses")
99 .add_tool(web_search_tool);
100
101 match openai_responses
102 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
103 For each news item, provide the title, url, and a short description.")
104 .await
105 {
106 Ok(response) => println!("AI news articles:\n{:#?}", response),
107 Err(e) => eprintln!("Error: {:?}", e),
108 }
109
110 // Example 3: File search example
111
112 // Read the concert file and upload it to OpenAI
113 let path = Path::new("metallica.pdf");
114 let bytes = std::fs::read(path)?;
115 let file_name = path
116 .file_name()
117 .and_then(OsStr::to_str)
118 .map(|s| s.to_string())
119 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
120 let openai_file = OpenAIFile::new(None, &openai_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123 let openai_vector_store = OpenAIVectorStore::new(None, "Concerts", &openai_api_key)
124 .upload(&[openai_file.id.clone().unwrap_or_default()])
125 .await?;
126
127 // Extract concert information using Responses API with file search tool
128 let file_search_tool =
129 LLMTools::OpenAIFileSearch(OpenAIFileSearchConfig::new(vec![openai_vector_store
130 .id
131 .clone()
132 .unwrap_or_default()]));
133
134 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
135 .version("openai_responses")
136 .set_context("bands_genres", &BANDS_GENRES)?
137 .add_tool(file_search_tool);
138
139 match openai_responses
140 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
141 The response should include the genre of the music the 'band' represents.
142 The mapping of bands to genres was provided in 'bands_genres' list.")
143 .await
144 {
145 Ok(response) => println!("Concert Info:\n{:#?}", response),
146 Err(e) => eprintln!("Error: {:?}", e),
147 }
148
149 // Cleanup
150 openai_file.delete().await?;
151 openai_vector_store.delete().await?;
152
153 // Example 4: Code interpreter example
154
155 let code_interpreter_tool = LLMTools::OpenAICodeInterpreter(OpenAICodeInterpreterConfig::new());
156 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
157 .version("openai_responses")
158 .set_context("Code Interpreter", &"You are a personal math tutor. When asked a math question, write and run code to answer the question.".to_string())?
159 .add_tool(code_interpreter_tool);
160
161 match openai_responses
162 .get_answer::<CodeInterpreterResponse>(
163 "I need to solve the equation 3x + 11 = 14. Can you help me?",
164 )
165 .await
166 {
167 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
168 Err(e) => eprintln!("Error: {:?}", e),
169 }
170
171 Ok(())
172}22async fn main() {
23 env_logger::init();
24
25 // Example context and instructions
26 let instructions =
27 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
28
29 // Get answer using AWS Bedrock Converse
30 // AWS Bedrock SDK requires `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables to be defined and matching your AWS account
31 let model = AwsBedrockModels::try_from_str("amazon.nova-lite-v1:0")
32 .unwrap_or(AwsBedrockModels::NovaLite); // Choose the model
33 println!("AWS Bedrock model: {:#?}", model.as_str());
34
35 let aws_completion = Completions::new(model, "", None, None);
36
37 match aws_completion
38 .get_answer::<TranslationResponse>(instructions)
39 .await
40 {
41 Ok(response) => println!("AWS Bedrock response: {:#?}", response),
42 Err(e) => eprintln!("Error: {:?}", e),
43 }
44
45 // Get answer using OpenAI Completions API
46 let openai_api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
47 let model = OpenAIModels::try_from_str("gpt-5.4").unwrap_or(OpenAIModels::Gpt5_4); // Choose the model
48 println!("OpenAI model: {:#?}", model.as_str());
49
50 let openai_completion = Completions::new(model, &openai_api_key, None, None);
51
52 match openai_completion
53 .get_answer::<TranslationResponse>(instructions)
54 .await
55 {
56 Ok(response) => println!("OpenAI Completions API response: {:#?}", response),
57 Err(e) => eprintln!("Error: {:?}", e),
58 }
59
60 // Get answer using OpenAI (on Azure)
61 // Ensure `OPENAI_API_URL` is set to your Azure OpenAI resource endpoint
62 let azure_openai_completion =
63 Completions::new(OpenAIModels::Gpt5_2, &openai_api_key, None, None)
64 .version("azure:2024-08-01-preview");
65 match azure_openai_completion
66 .get_answer::<TranslationResponse>(instructions)
67 .await
68 {
69 Ok(response) => println!("Azure OpenAI response: {:#?}", response),
70 Err(e) => eprintln!("Error: {:?}", e),
71 }
72
73 // Get answer using Anthropic
74 let anthropic_api_key: String =
75 std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY not set");
76 let model = AnthropicModels::try_from_str("claude-haiku-4-5")
77 .unwrap_or(AnthropicModels::Claude4_5Haiku); // Choose the model
78 println!("Anthropic model: {:#?}", model.as_str());
79
80 let anthropic_completion = Completions::new(model, &anthropic_api_key, None, None);
81
82 match anthropic_completion
83 .get_answer::<TranslationResponse>(instructions)
84 .await
85 {
86 Ok(response) => println!("Anthropic response: {:#?}", response),
87 Err(e) => eprintln!("Error: {:?}", e),
88 }
89
90 // Get answer using Mistral
91 let mistral_api_key: String =
92 std::env::var("MISTRAL_API_KEY").expect("MISTRAL_API_KEY not set");
93 let model = MistralModels::try_from_str("mistral-medium-latest")
94 .unwrap_or(MistralModels::MistralMedium3_1); // Choose the model
95 println!("Mistral model: {:#?}", model.as_str());
96
97 let mistral_completion = Completions::new(model, &mistral_api_key, None, None);
98
99 match mistral_completion
100 .get_answer::<TranslationResponse>(instructions)
101 .await
102 {
103 Ok(response) => println!("Mistral response: {:#?}", response),
104 Err(e) => eprintln!("Error: {:?}", e),
105 }
106
107 // Get answer using Google Studio
108 let model = GoogleModels::try_from_str("gemini-2.5-flash-lite")
109 .unwrap_or(GoogleModels::Gemini2_5FlashLite); // Choose the model
110 println!("Google Gemini model: {:#?}", model.as_str());
111
112 let google_token_str: String =
113 std::env::var("GOOGLE_AI_STUDIO_API_KEY").expect("GOOGLE_AI_STUDIO_API_KEY not set");
114
115 let gemini_completion =
116 Completions::new(model, &google_token_str, None, None).version("google-studio");
117
118 match gemini_completion
119 .get_answer::<TranslationResponse>(instructions)
120 .await
121 {
122 Ok(response) => println!("Gemini response: {:#?}", response),
123 Err(e) => eprintln!("Error: {:?}", e),
124 }
125
126 // Get answer using Perplexity
127 let model = PerplexityModels::try_from_str("sonar-pro").unwrap_or(PerplexityModels::Sonar); // Choose the model
128 println!("Perplexity model: {:#?}", model.as_str());
129
130 let perplexity_token_str: String =
131 std::env::var("PERPLEXITY_API_KEY").expect("PERPLEXITY_API_KEY not set");
132
133 let perplexity_completion = Completions::new(model, &perplexity_token_str, None, None);
134
135 match perplexity_completion
136 .get_answer::<TranslationResponse>(instructions)
137 .await
138 {
139 Ok(response) => println!("Perplexity response: {:#?}", response),
140 Err(e) => eprintln!("Error: {:?}", e),
141 }
142
143 // Get answer using DeepSeek
144 let model =
145 DeepSeekModels::try_from_str("deepseek-chat").unwrap_or(DeepSeekModels::DeepSeekChat); // Choose the model
146 println!("DeepSeek model: {:#?}", model.as_str());
147
148 let deepseek_token_str: String =
149 std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY not set");
150
151 let deepseek_completion = Completions::new(model, &deepseek_token_str, None, None);
152
153 match deepseek_completion
154 .get_answer::<TranslationResponse>(instructions)
155 .await
156 {
157 Ok(response) => println!("DeepSeek response: {:#?}", response),
158 Err(e) => eprintln!("Error: {:?}", e),
159 }
160
161 // Get answer using xAI Grok
162 let xai_api_key: String = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
163 let model = XAIModels::try_from_str("grok-3-mini").unwrap_or(XAIModels::Grok3Mini); // Choose the model
164 println!("xAI Grok model: {:#?}", model.as_str());
165
166 let xai_completion = Completions::new(model, &xai_api_key, None, None);
167
168 match xai_completion
169 .get_answer::<TranslationResponse>(instructions)
170 .await
171 {
172 Ok(response) => println!("xAI Grok response: {:#?}", response),
173 Err(e) => eprintln!("Error: {:?}", e),
174 }
175}Sourcepub fn add_tool(self, tool: LLMTools) -> Self
pub fn add_tool(self, tool: LLMTools) -> Self
This method can be used to inform the model to use a tool. Different models support different tool implementations.
Examples found in repository?
36async fn main() -> Result<()> {
37 env_logger::init();
38
39 let mistral_api_key: String =
40 std::env::var("MISTRAL_API_KEY").expect("MISTRAL_API_KEY not set");
41
42 // Example 1: Web search example
43 let web_search_tool = LLMTools::MistralWebSearch(MistralWebSearchConfig::new());
44 let mistral_responses = Completions::new(
45 MistralModels::MistralMedium3_1,
46 &mistral_api_key,
47 None,
48 None,
49 )
50 .add_tool(web_search_tool);
51
52 match mistral_responses
53 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
54 For each news item, provide the title, url, and a short description.")
55 .await
56 {
57 Ok(response) => println!("AI news articles:\n{:#?}", response),
58 Err(e) => eprintln!("Error: {:?}", e),
59 }
60
61 // Example 2: Code interpreter example
62 let code_interpreter_tool =
63 LLMTools::MistralCodeInterpreter(MistralCodeInterpreterConfig::new());
64 let mistral_responses = Completions::new(
65 MistralModels::MistralMedium3_1,
66 &mistral_api_key,
67 None,
68 None,
69 )
70 .add_tool(code_interpreter_tool);
71
72 match mistral_responses
73 .get_answer::<CodeInterpreterResponse>(
74 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
75 )
76 .await
77 {
78 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
79 Err(e) => eprintln!("Error: {:?}", e),
80 }
81
82 Ok(())
83}More examples
42async fn main() -> Result<()> {
43 env_logger::init();
44
45 let xai_api_key: String = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
46
47 // Example 1: Web search example with domain filters
48 let web_search_config = XAIWebSearchConfig::new()
49 .add_allowed_domains(&["techcrunch.com".to_string(), "wired.com".to_string()])
50 .with_enable_image_understanding(true);
51
52 let web_search_tool = LLMTools::XAIWebSearch(web_search_config);
53 let xai_responses =
54 Completions::new(XAIModels::Grok4_1FastNonReasoning, &xai_api_key, None, None)
55 .add_tool(web_search_tool);
56
57 match xai_responses
58 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
59 For each news item, provide the title, url, and a short description.")
60 .await
61 {
62 Ok(response) => println!("AI news articles:\n{:#?}", response),
63 Err(e) => eprintln!("Error: {:?}", e),
64 }
65
66 // Example 2: X Search example with date range and handle filters
67 let x_search_config = XAIXSearchConfig::new()
68 .from_date("2025-01-01".to_string())
69 .to_date("2025-12-31".to_string())
70 .add_allowed_x_handles(&["@elonmusk".to_string(), "@OpenAI".to_string()])
71 .enable_image_understanding(true)
72 .enable_video_understanding(true);
73
74 let x_search_tool = LLMTools::XAIXSearch(x_search_config);
75 let xai_responses_x =
76 Completions::new(XAIModels::Grok4_1FastReasoning, &xai_api_key, None, None)
77 .add_tool(x_search_tool);
78
79 match xai_responses_x
80 .get_answer::<XPosts>(
81 "Find up to 5 recent posts about AI and machine learning from the specified accounts.
82 For each post, provide the author handle, content, and if available, the URL and date.",
83 )
84 .await
85 {
86 Ok(response) => println!("X posts:\n{:#?}", response),
87 Err(e) => eprintln!("Error: {:?}", e),
88 }
89
90 Ok(())
91}62async fn main() -> Result<()> {
63 env_logger::init();
64
65 let anthropic_api_key: String =
66 std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY not set");
67
68 // Example 1: Web search example
69 let web_search_tool = LLMTools::AnthropicWebSearch(AnthropicWebSearchConfig::new());
70 let anthropic_responses = Completions::new(
71 AnthropicModels::ClaudeOpus4_7,
72 &anthropic_api_key,
73 None,
74 None,
75 )
76 .add_tool(web_search_tool);
77
78 match anthropic_responses
79 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
80 For each news item, provide the title, url, and a short description.")
81 .await
82 {
83 Ok(response) => println!("AI news articles:\n{:#?}", response),
84 Err(e) => eprintln!("Error: {:?}", e),
85 }
86
87 // Example 2: Code interpreter example
88
89 let code_interpreter_tool =
90 LLMTools::AnthropicCodeExecution(AnthropicCodeExecutionConfig::new());
91 let anthropic_responses = Completions::new(
92 AnthropicModels::ClaudeOpus4_7,
93 &anthropic_api_key,
94 None,
95 None,
96 )
97 .add_tool(code_interpreter_tool);
98
99 match anthropic_responses
100 .get_answer::<CodeInterpreterResponse>(
101 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
102 )
103 .await
104 {
105 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
106 Err(e) => eprintln!("Error: {:?}", e),
107 }
108
109 // Example 3: File search example
110
111 // Read the concert file and upload it to Anthropic
112 let path = Path::new("metallica.pdf");
113 let bytes = std::fs::read(path)?;
114 let file_name = path
115 .file_name()
116 .and_then(OsStr::to_str)
117 .map(|s| s.to_string())
118 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
119
120 let anthropic_file = AnthropicFile::new(None, &anthropic_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123
124 // Extract concert information using Anthropic API with file search tool
125 let file_search_tool = LLMTools::AnthropicFileSearch(AnthropicFileSearchConfig::new(
126 anthropic_file.id.clone().unwrap_or_default(),
127 ));
128
129 let anthropic_responses = Completions::new(
130 AnthropicModels::ClaudeSonnet4_6,
131 &anthropic_api_key,
132 None,
133 None,
134 )
135 .set_context("bands_genres", &BANDS_GENRES)?
136 .add_tool(file_search_tool);
137
138 match anthropic_responses
139 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
140 The response should include the genre of the music the 'band' represents.
141 The mapping of bands to genres was provided in 'bands_genres' list.")
142 .await
143 {
144 Ok(response) => println!("Concert Info:\n{:#?}", response),
145 Err(e) => eprintln!("Error: {:?}", e),
146 }
147
148 // Cleanup
149 anthropic_file.delete().await?;
150
151 Ok(())
152}39async fn main() -> Result<()> {
40 env_logger::init();
41
42 let google_api_key: String =
43 std::env::var("GOOGLE_AI_STUDIO_API_KEY").expect("GOOGLE_AI_STUDIO_API_KEY not set");
44 let vertex_token = get_vertex_token().await?;
45
46 // Example 1A: Web search example (with Studio API)
47 let web_search_config =
48 GeminiWebSearchConfig::new().add_source("https://www.artificialintelligence-news.com/");
49
50 let web_search_tool = LLMTools::GeminiWebSearch(web_search_config);
51 let google_responses =
52 Completions::new(GoogleModels::Gemini3Flash, &google_api_key, None, None)
53 .add_tool(web_search_tool.clone())
54 .thinking_level(ThinkingLevel::Low);
55
56 match google_responses
57 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
58 For each news item, provide the title, url, and a short description.")
59 .await
60 {
61 Ok(response) => println!("[AI Studio] AI news articles:\n{:#?}", response),
62 Err(e) => eprintln!("[AI Studio] AI news articles error: {:?}", e),
63 }
64
65 // Example 1B: Web search example (with Vertex API)
66 let google_responses_vertex =
67 Completions::new(GoogleModels::Gemini3_1FlashLite, &vertex_token, None, None)
68 .add_tool(web_search_tool)
69 .version("google-vertex");
70
71 match google_responses_vertex
72 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
73 For each news item, provide the title, url, and a short description.")
74 .await
75 {
76 Ok(response) => println!("[Vertex] AI news articles:\n{:#?}", response),
77 Err(e) => eprintln!("[Vertex] AI news articles error: {:?}", e),
78 }
79
80 // Example 2A: Code interpreter example (with Studio API)
81 let code_interpreter_tool = LLMTools::GeminiCodeInterpreter(GeminiCodeInterpreterConfig::new());
82 let google_responses =
83 Completions::new(GoogleModels::Gemini3_1Pro, &google_api_key, None, None)
84 .add_tool(code_interpreter_tool.clone());
85
86 match google_responses
87 .get_answer::<CodeInterpreterResponse>(
88 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
89 )
90 .await
91 {
92 Ok(response) => println!("[AI Studio] Code interpreter response:\n{:#?}", response),
93 Err(e) => eprintln!("[AI Studio] Code interpreter error: {:?}", e),
94 }
95
96 // Example 2B: Code interpreter example (with Vertex API)
97 let google_responses_vertex =
98 Completions::new(GoogleModels::Gemini3_1Pro, &vertex_token, None, None)
99 .add_tool(code_interpreter_tool)
100 .version("google-vertex");
101
102 match google_responses_vertex
103 .get_answer::<CodeInterpreterResponse>(
104 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
105 )
106 .await
107 {
108 Ok(response) => println!("[Vertex] Code interpreter response:\n{:#?}", response),
109 Err(e) => eprintln!("[Vertex] Code interpreter error: {:?}", e),
110 }
111
112 Ok(())
113}72async fn main() -> Result<()> {
73 env_logger::init();
74
75 // Example 1: Basic translation example using reasoning model
76 let instructions =
77 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
78
79 let openai_api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
80
81 let reasoning_tool = LLMTools::OpenAIReasoning(OpenAIReasoningConfig::default());
82
83 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
84 .version("openai_responses")
85 .add_tool(reasoning_tool);
86
87 match openai_responses
88 .get_answer::<TranslationResponse>(instructions)
89 .await
90 {
91 Ok(response) => println!("Translations:\n{:#?}", response),
92 Err(e) => eprintln!("Error: {:?}", e),
93 }
94
95 // Example 2: Web search example
96 let web_search_tool = LLMTools::OpenAIWebSearch(OpenAIWebSearchConfig::new());
97 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
98 .version("openai_responses")
99 .add_tool(web_search_tool);
100
101 match openai_responses
102 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
103 For each news item, provide the title, url, and a short description.")
104 .await
105 {
106 Ok(response) => println!("AI news articles:\n{:#?}", response),
107 Err(e) => eprintln!("Error: {:?}", e),
108 }
109
110 // Example 3: File search example
111
112 // Read the concert file and upload it to OpenAI
113 let path = Path::new("metallica.pdf");
114 let bytes = std::fs::read(path)?;
115 let file_name = path
116 .file_name()
117 .and_then(OsStr::to_str)
118 .map(|s| s.to_string())
119 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
120 let openai_file = OpenAIFile::new(None, &openai_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123 let openai_vector_store = OpenAIVectorStore::new(None, "Concerts", &openai_api_key)
124 .upload(&[openai_file.id.clone().unwrap_or_default()])
125 .await?;
126
127 // Extract concert information using Responses API with file search tool
128 let file_search_tool =
129 LLMTools::OpenAIFileSearch(OpenAIFileSearchConfig::new(vec![openai_vector_store
130 .id
131 .clone()
132 .unwrap_or_default()]));
133
134 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
135 .version("openai_responses")
136 .set_context("bands_genres", &BANDS_GENRES)?
137 .add_tool(file_search_tool);
138
139 match openai_responses
140 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
141 The response should include the genre of the music the 'band' represents.
142 The mapping of bands to genres was provided in 'bands_genres' list.")
143 .await
144 {
145 Ok(response) => println!("Concert Info:\n{:#?}", response),
146 Err(e) => eprintln!("Error: {:?}", e),
147 }
148
149 // Cleanup
150 openai_file.delete().await?;
151 openai_vector_store.delete().await?;
152
153 // Example 4: Code interpreter example
154
155 let code_interpreter_tool = LLMTools::OpenAICodeInterpreter(OpenAICodeInterpreterConfig::new());
156 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
157 .version("openai_responses")
158 .set_context("Code Interpreter", &"You are a personal math tutor. When asked a math question, write and run code to answer the question.".to_string())?
159 .add_tool(code_interpreter_tool);
160
161 match openai_responses
162 .get_answer::<CodeInterpreterResponse>(
163 "I need to solve the equation 3x + 11 = 14. Can you help me?",
164 )
165 .await
166 {
167 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
168 Err(e) => eprintln!("Error: {:?}", e),
169 }
170
171 Ok(())
172}Sourcepub fn thinking_level(self, thinking_level: ThinkingLevel) -> Self
pub fn thinking_level(self, thinking_level: ThinkingLevel) -> Self
This method can be used to set the thinking level for the model This is currently used for Gemini 3 models
Examples found in repository?
39async fn main() -> Result<()> {
40 env_logger::init();
41
42 let google_api_key: String =
43 std::env::var("GOOGLE_AI_STUDIO_API_KEY").expect("GOOGLE_AI_STUDIO_API_KEY not set");
44 let vertex_token = get_vertex_token().await?;
45
46 // Example 1A: Web search example (with Studio API)
47 let web_search_config =
48 GeminiWebSearchConfig::new().add_source("https://www.artificialintelligence-news.com/");
49
50 let web_search_tool = LLMTools::GeminiWebSearch(web_search_config);
51 let google_responses =
52 Completions::new(GoogleModels::Gemini3Flash, &google_api_key, None, None)
53 .add_tool(web_search_tool.clone())
54 .thinking_level(ThinkingLevel::Low);
55
56 match google_responses
57 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
58 For each news item, provide the title, url, and a short description.")
59 .await
60 {
61 Ok(response) => println!("[AI Studio] AI news articles:\n{:#?}", response),
62 Err(e) => eprintln!("[AI Studio] AI news articles error: {:?}", e),
63 }
64
65 // Example 1B: Web search example (with Vertex API)
66 let google_responses_vertex =
67 Completions::new(GoogleModels::Gemini3_1FlashLite, &vertex_token, None, None)
68 .add_tool(web_search_tool)
69 .version("google-vertex");
70
71 match google_responses_vertex
72 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
73 For each news item, provide the title, url, and a short description.")
74 .await
75 {
76 Ok(response) => println!("[Vertex] AI news articles:\n{:#?}", response),
77 Err(e) => eprintln!("[Vertex] AI news articles error: {:?}", e),
78 }
79
80 // Example 2A: Code interpreter example (with Studio API)
81 let code_interpreter_tool = LLMTools::GeminiCodeInterpreter(GeminiCodeInterpreterConfig::new());
82 let google_responses =
83 Completions::new(GoogleModels::Gemini3_1Pro, &google_api_key, None, None)
84 .add_tool(code_interpreter_tool.clone());
85
86 match google_responses
87 .get_answer::<CodeInterpreterResponse>(
88 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
89 )
90 .await
91 {
92 Ok(response) => println!("[AI Studio] Code interpreter response:\n{:#?}", response),
93 Err(e) => eprintln!("[AI Studio] Code interpreter error: {:?}", e),
94 }
95
96 // Example 2B: Code interpreter example (with Vertex API)
97 let google_responses_vertex =
98 Completions::new(GoogleModels::Gemini3_1Pro, &vertex_token, None, None)
99 .add_tool(code_interpreter_tool)
100 .version("google-vertex");
101
102 match google_responses_vertex
103 .get_answer::<CodeInterpreterResponse>(
104 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
105 )
106 .await
107 {
108 Ok(response) => println!("[Vertex] Code interpreter response:\n{:#?}", response),
109 Err(e) => eprintln!("[Vertex] Code interpreter error: {:?}", e),
110 }
111
112 Ok(())
113}Sourcepub fn set_context<U: Serialize>(
self,
input_name: &str,
input_data: &U,
) -> Result<Self>
pub fn set_context<U: Serialize>( self, input_name: &str, input_data: &U, ) -> Result<Self>
This method can be used to provide values that will be used as context for the prompt. Using this function you can provide multiple input values by calling it multiple times. New values will be appended with the category name It accepts any instance that implements the Serialize trait.
Examples found in repository?
62async fn main() -> Result<()> {
63 env_logger::init();
64
65 let anthropic_api_key: String =
66 std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY not set");
67
68 // Example 1: Web search example
69 let web_search_tool = LLMTools::AnthropicWebSearch(AnthropicWebSearchConfig::new());
70 let anthropic_responses = Completions::new(
71 AnthropicModels::ClaudeOpus4_7,
72 &anthropic_api_key,
73 None,
74 None,
75 )
76 .add_tool(web_search_tool);
77
78 match anthropic_responses
79 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
80 For each news item, provide the title, url, and a short description.")
81 .await
82 {
83 Ok(response) => println!("AI news articles:\n{:#?}", response),
84 Err(e) => eprintln!("Error: {:?}", e),
85 }
86
87 // Example 2: Code interpreter example
88
89 let code_interpreter_tool =
90 LLMTools::AnthropicCodeExecution(AnthropicCodeExecutionConfig::new());
91 let anthropic_responses = Completions::new(
92 AnthropicModels::ClaudeOpus4_7,
93 &anthropic_api_key,
94 None,
95 None,
96 )
97 .add_tool(code_interpreter_tool);
98
99 match anthropic_responses
100 .get_answer::<CodeInterpreterResponse>(
101 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
102 )
103 .await
104 {
105 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
106 Err(e) => eprintln!("Error: {:?}", e),
107 }
108
109 // Example 3: File search example
110
111 // Read the concert file and upload it to Anthropic
112 let path = Path::new("metallica.pdf");
113 let bytes = std::fs::read(path)?;
114 let file_name = path
115 .file_name()
116 .and_then(OsStr::to_str)
117 .map(|s| s.to_string())
118 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
119
120 let anthropic_file = AnthropicFile::new(None, &anthropic_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123
124 // Extract concert information using Anthropic API with file search tool
125 let file_search_tool = LLMTools::AnthropicFileSearch(AnthropicFileSearchConfig::new(
126 anthropic_file.id.clone().unwrap_or_default(),
127 ));
128
129 let anthropic_responses = Completions::new(
130 AnthropicModels::ClaudeSonnet4_6,
131 &anthropic_api_key,
132 None,
133 None,
134 )
135 .set_context("bands_genres", &BANDS_GENRES)?
136 .add_tool(file_search_tool);
137
138 match anthropic_responses
139 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
140 The response should include the genre of the music the 'band' represents.
141 The mapping of bands to genres was provided in 'bands_genres' list.")
142 .await
143 {
144 Ok(response) => println!("Concert Info:\n{:#?}", response),
145 Err(e) => eprintln!("Error: {:?}", e),
146 }
147
148 // Cleanup
149 anthropic_file.delete().await?;
150
151 Ok(())
152}More examples
72async fn main() -> Result<()> {
73 env_logger::init();
74
75 // Example 1: Basic translation example using reasoning model
76 let instructions =
77 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
78
79 let openai_api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
80
81 let reasoning_tool = LLMTools::OpenAIReasoning(OpenAIReasoningConfig::default());
82
83 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
84 .version("openai_responses")
85 .add_tool(reasoning_tool);
86
87 match openai_responses
88 .get_answer::<TranslationResponse>(instructions)
89 .await
90 {
91 Ok(response) => println!("Translations:\n{:#?}", response),
92 Err(e) => eprintln!("Error: {:?}", e),
93 }
94
95 // Example 2: Web search example
96 let web_search_tool = LLMTools::OpenAIWebSearch(OpenAIWebSearchConfig::new());
97 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
98 .version("openai_responses")
99 .add_tool(web_search_tool);
100
101 match openai_responses
102 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
103 For each news item, provide the title, url, and a short description.")
104 .await
105 {
106 Ok(response) => println!("AI news articles:\n{:#?}", response),
107 Err(e) => eprintln!("Error: {:?}", e),
108 }
109
110 // Example 3: File search example
111
112 // Read the concert file and upload it to OpenAI
113 let path = Path::new("metallica.pdf");
114 let bytes = std::fs::read(path)?;
115 let file_name = path
116 .file_name()
117 .and_then(OsStr::to_str)
118 .map(|s| s.to_string())
119 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
120 let openai_file = OpenAIFile::new(None, &openai_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123 let openai_vector_store = OpenAIVectorStore::new(None, "Concerts", &openai_api_key)
124 .upload(&[openai_file.id.clone().unwrap_or_default()])
125 .await?;
126
127 // Extract concert information using Responses API with file search tool
128 let file_search_tool =
129 LLMTools::OpenAIFileSearch(OpenAIFileSearchConfig::new(vec![openai_vector_store
130 .id
131 .clone()
132 .unwrap_or_default()]));
133
134 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
135 .version("openai_responses")
136 .set_context("bands_genres", &BANDS_GENRES)?
137 .add_tool(file_search_tool);
138
139 match openai_responses
140 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
141 The response should include the genre of the music the 'band' represents.
142 The mapping of bands to genres was provided in 'bands_genres' list.")
143 .await
144 {
145 Ok(response) => println!("Concert Info:\n{:#?}", response),
146 Err(e) => eprintln!("Error: {:?}", e),
147 }
148
149 // Cleanup
150 openai_file.delete().await?;
151 openai_vector_store.delete().await?;
152
153 // Example 4: Code interpreter example
154
155 let code_interpreter_tool = LLMTools::OpenAICodeInterpreter(OpenAICodeInterpreterConfig::new());
156 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
157 .version("openai_responses")
158 .set_context("Code Interpreter", &"You are a personal math tutor. When asked a math question, write and run code to answer the question.".to_string())?
159 .add_tool(code_interpreter_tool);
160
161 match openai_responses
162 .get_answer::<CodeInterpreterResponse>(
163 "I need to solve the equation 3x + 11 = 14. Can you help me?",
164 )
165 .await
166 {
167 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
168 Err(e) => eprintln!("Error: {:?}", e),
169 }
170
171 Ok(())
172}Sourcepub fn check_prompt_tokens<U: JsonSchema + DeserializeOwned>(
&self,
instructions: &str,
) -> Result<usize>
pub fn check_prompt_tokens<U: JsonSchema + DeserializeOwned>( &self, instructions: &str, ) -> Result<usize>
This method is used to check how many tokens would most likely remain for the response This is accomplished by estimating number of tokens needed for system/base instructions, user prompt, and function components including schema definition.
Sourcepub async fn get_answer<U: JsonSchema + DeserializeOwned>(
self,
instructions: &str,
) -> Result<U>
pub async fn get_answer<U: JsonSchema + DeserializeOwned>( self, instructions: &str, ) -> Result<U>
This method is used to submit a prompt to OpenAI and process the response. When calling the function you need to specify the type parameter as the response will match the schema of that type. The prompt in this function is written in a way to instruct OpenAI to behave like a computer function that calculates an output based on provided input and its language model.
Examples found in repository?
36async fn main() -> Result<()> {
37 env_logger::init();
38
39 let mistral_api_key: String =
40 std::env::var("MISTRAL_API_KEY").expect("MISTRAL_API_KEY not set");
41
42 // Example 1: Web search example
43 let web_search_tool = LLMTools::MistralWebSearch(MistralWebSearchConfig::new());
44 let mistral_responses = Completions::new(
45 MistralModels::MistralMedium3_1,
46 &mistral_api_key,
47 None,
48 None,
49 )
50 .add_tool(web_search_tool);
51
52 match mistral_responses
53 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
54 For each news item, provide the title, url, and a short description.")
55 .await
56 {
57 Ok(response) => println!("AI news articles:\n{:#?}", response),
58 Err(e) => eprintln!("Error: {:?}", e),
59 }
60
61 // Example 2: Code interpreter example
62 let code_interpreter_tool =
63 LLMTools::MistralCodeInterpreter(MistralCodeInterpreterConfig::new());
64 let mistral_responses = Completions::new(
65 MistralModels::MistralMedium3_1,
66 &mistral_api_key,
67 None,
68 None,
69 )
70 .add_tool(code_interpreter_tool);
71
72 match mistral_responses
73 .get_answer::<CodeInterpreterResponse>(
74 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
75 )
76 .await
77 {
78 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
79 Err(e) => eprintln!("Error: {:?}", e),
80 }
81
82 Ok(())
83}More examples
20async fn main() -> Result<()> {
21 env_logger::init();
22
23 // Get Vertex API authentication token
24 let google_token_str = get_vertex_token().await?;
25
26 // Example context and instructions
27 let instructions =
28 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
29
30 // Get answer using Google GeminiPro via Vertex AI
31 let model = GoogleModels::Gemini2_5FlashLite;
32
33 // **Pre-requisite**: GeminiPro request through Vertex AI require `GOOGLE_PROJECT_ID` environment variable defined
34 let gemini_completion =
35 Completions::new(model, &google_token_str, None, None).version("google-vertex");
36
37 match gemini_completion
38 .get_answer::<TranslationResponse>(instructions)
39 .await
40 {
41 Ok(response) => println!("Vertex Gemini response: {:#?}", response),
42 Err(e) => eprintln!("Error: {:?}", e),
43 }
44
45 // Get answer using a fine-tuned model
46
47 // Using a fine-tuned model requires addressing the endpoint directly
48 // Replace env variable with the endpoint ID of the fine-tuned model
49 let fine_tuned_endpoint_id: String =
50 std::env::var("GOOGLE_VERTEX_ENDPOINT_ID").expect("GOOGLE_VERTEX_ENDPOINT_ID not set");
51 let model = GoogleModels::endpoint(&fine_tuned_endpoint_id);
52
53 let gemini_completion =
54 Completions::new(model, &google_token_str, None, None).version("google-vertex");
55
56 match gemini_completion
57 .get_answer::<TranslationResponse>(instructions)
58 .await
59 {
60 Ok(response) => println!("Vertex Gemini response: {:#?}", response),
61 Err(e) => eprintln!("Error: {:?}", e),
62 }
63
64 Ok(())
65}42async fn main() -> Result<()> {
43 env_logger::init();
44
45 let xai_api_key: String = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
46
47 // Example 1: Web search example with domain filters
48 let web_search_config = XAIWebSearchConfig::new()
49 .add_allowed_domains(&["techcrunch.com".to_string(), "wired.com".to_string()])
50 .with_enable_image_understanding(true);
51
52 let web_search_tool = LLMTools::XAIWebSearch(web_search_config);
53 let xai_responses =
54 Completions::new(XAIModels::Grok4_1FastNonReasoning, &xai_api_key, None, None)
55 .add_tool(web_search_tool);
56
57 match xai_responses
58 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
59 For each news item, provide the title, url, and a short description.")
60 .await
61 {
62 Ok(response) => println!("AI news articles:\n{:#?}", response),
63 Err(e) => eprintln!("Error: {:?}", e),
64 }
65
66 // Example 2: X Search example with date range and handle filters
67 let x_search_config = XAIXSearchConfig::new()
68 .from_date("2025-01-01".to_string())
69 .to_date("2025-12-31".to_string())
70 .add_allowed_x_handles(&["@elonmusk".to_string(), "@OpenAI".to_string()])
71 .enable_image_understanding(true)
72 .enable_video_understanding(true);
73
74 let x_search_tool = LLMTools::XAIXSearch(x_search_config);
75 let xai_responses_x =
76 Completions::new(XAIModels::Grok4_1FastReasoning, &xai_api_key, None, None)
77 .add_tool(x_search_tool);
78
79 match xai_responses_x
80 .get_answer::<XPosts>(
81 "Find up to 5 recent posts about AI and machine learning from the specified accounts.
82 For each post, provide the author handle, content, and if available, the URL and date.",
83 )
84 .await
85 {
86 Ok(response) => println!("X posts:\n{:#?}", response),
87 Err(e) => eprintln!("Error: {:?}", e),
88 }
89
90 Ok(())
91}62async fn main() -> Result<()> {
63 env_logger::init();
64
65 let anthropic_api_key: String =
66 std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY not set");
67
68 // Example 1: Web search example
69 let web_search_tool = LLMTools::AnthropicWebSearch(AnthropicWebSearchConfig::new());
70 let anthropic_responses = Completions::new(
71 AnthropicModels::ClaudeOpus4_7,
72 &anthropic_api_key,
73 None,
74 None,
75 )
76 .add_tool(web_search_tool);
77
78 match anthropic_responses
79 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
80 For each news item, provide the title, url, and a short description.")
81 .await
82 {
83 Ok(response) => println!("AI news articles:\n{:#?}", response),
84 Err(e) => eprintln!("Error: {:?}", e),
85 }
86
87 // Example 2: Code interpreter example
88
89 let code_interpreter_tool =
90 LLMTools::AnthropicCodeExecution(AnthropicCodeExecutionConfig::new());
91 let anthropic_responses = Completions::new(
92 AnthropicModels::ClaudeOpus4_7,
93 &anthropic_api_key,
94 None,
95 None,
96 )
97 .add_tool(code_interpreter_tool);
98
99 match anthropic_responses
100 .get_answer::<CodeInterpreterResponse>(
101 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
102 )
103 .await
104 {
105 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
106 Err(e) => eprintln!("Error: {:?}", e),
107 }
108
109 // Example 3: File search example
110
111 // Read the concert file and upload it to Anthropic
112 let path = Path::new("metallica.pdf");
113 let bytes = std::fs::read(path)?;
114 let file_name = path
115 .file_name()
116 .and_then(OsStr::to_str)
117 .map(|s| s.to_string())
118 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
119
120 let anthropic_file = AnthropicFile::new(None, &anthropic_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123
124 // Extract concert information using Anthropic API with file search tool
125 let file_search_tool = LLMTools::AnthropicFileSearch(AnthropicFileSearchConfig::new(
126 anthropic_file.id.clone().unwrap_or_default(),
127 ));
128
129 let anthropic_responses = Completions::new(
130 AnthropicModels::ClaudeSonnet4_6,
131 &anthropic_api_key,
132 None,
133 None,
134 )
135 .set_context("bands_genres", &BANDS_GENRES)?
136 .add_tool(file_search_tool);
137
138 match anthropic_responses
139 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
140 The response should include the genre of the music the 'band' represents.
141 The mapping of bands to genres was provided in 'bands_genres' list.")
142 .await
143 {
144 Ok(response) => println!("Concert Info:\n{:#?}", response),
145 Err(e) => eprintln!("Error: {:?}", e),
146 }
147
148 // Cleanup
149 anthropic_file.delete().await?;
150
151 Ok(())
152}39async fn main() -> Result<()> {
40 env_logger::init();
41
42 let google_api_key: String =
43 std::env::var("GOOGLE_AI_STUDIO_API_KEY").expect("GOOGLE_AI_STUDIO_API_KEY not set");
44 let vertex_token = get_vertex_token().await?;
45
46 // Example 1A: Web search example (with Studio API)
47 let web_search_config =
48 GeminiWebSearchConfig::new().add_source("https://www.artificialintelligence-news.com/");
49
50 let web_search_tool = LLMTools::GeminiWebSearch(web_search_config);
51 let google_responses =
52 Completions::new(GoogleModels::Gemini3Flash, &google_api_key, None, None)
53 .add_tool(web_search_tool.clone())
54 .thinking_level(ThinkingLevel::Low);
55
56 match google_responses
57 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
58 For each news item, provide the title, url, and a short description.")
59 .await
60 {
61 Ok(response) => println!("[AI Studio] AI news articles:\n{:#?}", response),
62 Err(e) => eprintln!("[AI Studio] AI news articles error: {:?}", e),
63 }
64
65 // Example 1B: Web search example (with Vertex API)
66 let google_responses_vertex =
67 Completions::new(GoogleModels::Gemini3_1FlashLite, &vertex_token, None, None)
68 .add_tool(web_search_tool)
69 .version("google-vertex");
70
71 match google_responses_vertex
72 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
73 For each news item, provide the title, url, and a short description.")
74 .await
75 {
76 Ok(response) => println!("[Vertex] AI news articles:\n{:#?}", response),
77 Err(e) => eprintln!("[Vertex] AI news articles error: {:?}", e),
78 }
79
80 // Example 2A: Code interpreter example (with Studio API)
81 let code_interpreter_tool = LLMTools::GeminiCodeInterpreter(GeminiCodeInterpreterConfig::new());
82 let google_responses =
83 Completions::new(GoogleModels::Gemini3_1Pro, &google_api_key, None, None)
84 .add_tool(code_interpreter_tool.clone());
85
86 match google_responses
87 .get_answer::<CodeInterpreterResponse>(
88 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
89 )
90 .await
91 {
92 Ok(response) => println!("[AI Studio] Code interpreter response:\n{:#?}", response),
93 Err(e) => eprintln!("[AI Studio] Code interpreter error: {:?}", e),
94 }
95
96 // Example 2B: Code interpreter example (with Vertex API)
97 let google_responses_vertex =
98 Completions::new(GoogleModels::Gemini3_1Pro, &vertex_token, None, None)
99 .add_tool(code_interpreter_tool)
100 .version("google-vertex");
101
102 match google_responses_vertex
103 .get_answer::<CodeInterpreterResponse>(
104 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
105 )
106 .await
107 {
108 Ok(response) => println!("[Vertex] Code interpreter response:\n{:#?}", response),
109 Err(e) => eprintln!("[Vertex] Code interpreter error: {:?}", e),
110 }
111
112 Ok(())
113}72async fn main() -> Result<()> {
73 env_logger::init();
74
75 // Example 1: Basic translation example using reasoning model
76 let instructions =
77 "Translate the following English sentence to all the languages in the response type: Rust is best for working with LLMs";
78
79 let openai_api_key: String = std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set");
80
81 let reasoning_tool = LLMTools::OpenAIReasoning(OpenAIReasoningConfig::default());
82
83 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
84 .version("openai_responses")
85 .add_tool(reasoning_tool);
86
87 match openai_responses
88 .get_answer::<TranslationResponse>(instructions)
89 .await
90 {
91 Ok(response) => println!("Translations:\n{:#?}", response),
92 Err(e) => eprintln!("Error: {:?}", e),
93 }
94
95 // Example 2: Web search example
96 let web_search_tool = LLMTools::OpenAIWebSearch(OpenAIWebSearchConfig::new());
97 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
98 .version("openai_responses")
99 .add_tool(web_search_tool);
100
101 match openai_responses
102 .get_answer::<AINewsArticles>("Find up to 5 most recent news items about Artificial Intelligence, Generative AI, and Large Language Models.
103 For each news item, provide the title, url, and a short description.")
104 .await
105 {
106 Ok(response) => println!("AI news articles:\n{:#?}", response),
107 Err(e) => eprintln!("Error: {:?}", e),
108 }
109
110 // Example 3: File search example
111
112 // Read the concert file and upload it to OpenAI
113 let path = Path::new("metallica.pdf");
114 let bytes = std::fs::read(path)?;
115 let file_name = path
116 .file_name()
117 .and_then(OsStr::to_str)
118 .map(|s| s.to_string())
119 .ok_or_else(|| anyhow!("Failed to extract file name"))?;
120 let openai_file = OpenAIFile::new(None, &openai_api_key)
121 .upload(&file_name, bytes)
122 .await?;
123 let openai_vector_store = OpenAIVectorStore::new(None, "Concerts", &openai_api_key)
124 .upload(&[openai_file.id.clone().unwrap_or_default()])
125 .await?;
126
127 // Extract concert information using Responses API with file search tool
128 let file_search_tool =
129 LLMTools::OpenAIFileSearch(OpenAIFileSearchConfig::new(vec![openai_vector_store
130 .id
131 .clone()
132 .unwrap_or_default()]));
133
134 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Mini, &openai_api_key, None, None)
135 .version("openai_responses")
136 .set_context("bands_genres", &BANDS_GENRES)?
137 .add_tool(file_search_tool);
138
139 match openai_responses
140 .get_answer::<ConcertInfo>("Extract the information requested in the response type from the attached concert information.
141 The response should include the genre of the music the 'band' represents.
142 The mapping of bands to genres was provided in 'bands_genres' list.")
143 .await
144 {
145 Ok(response) => println!("Concert Info:\n{:#?}", response),
146 Err(e) => eprintln!("Error: {:?}", e),
147 }
148
149 // Cleanup
150 openai_file.delete().await?;
151 openai_vector_store.delete().await?;
152
153 // Example 4: Code interpreter example
154
155 let code_interpreter_tool = LLMTools::OpenAICodeInterpreter(OpenAICodeInterpreterConfig::new());
156 let openai_responses = Completions::new(OpenAIModels::Gpt5_4Nano, &openai_api_key, None, None)
157 .version("openai_responses")
158 .set_context("Code Interpreter", &"You are a personal math tutor. When asked a math question, write and run code to answer the question.".to_string())?
159 .add_tool(code_interpreter_tool);
160
161 match openai_responses
162 .get_answer::<CodeInterpreterResponse>(
163 "I need to solve the equation 3x + 11 = 14. Can you help me?",
164 )
165 .await
166 {
167 Ok(response) => println!("Code interpreter response:\n{:#?}", response),
168 Err(e) => eprintln!("Error: {:?}", e),
169 }
170
171 Ok(())
172}Auto Trait Implementations§
impl<T> Freeze for Completions<T>where
T: Freeze,
impl<T> RefUnwindSafe for Completions<T>where
T: RefUnwindSafe,
impl<T> Send for Completions<T>where
T: Send,
impl<T> Sync for Completions<T>where
T: Sync,
impl<T> Unpin for Completions<T>where
T: Unpin,
impl<T> UnsafeUnpin for Completions<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Completions<T>where
T: UnwindSafe,
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more