Skip to main content

Completions

Struct Completions 

Source
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>

Source

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?
examples/use_mistral_tools.rs (lines 44-49)
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
Hide additional examples
examples/use_completions_vertex.rs (line 35)
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}
examples/use_xai_tools.rs (line 54)
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}
examples/use_anthropic_tools.rs (lines 70-75)
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}
examples/use_google_tools.rs (line 52)
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}
examples/use_openai_responses.rs (line 83)
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}
Source

pub fn debug(self) -> Self

This function turns on debug mode which will info! the prompt to log when executing it.

Source

pub fn function_calling(self, function_call: bool) -> Self

This function turns on/off function calling mode when interacting with OpenAI API.

Source

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

Source

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

Source

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?
examples/use_completions_vertex.rs (line 35)
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
Hide additional examples
examples/use_google_tools.rs (line 69)
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}
examples/use_openai_responses.rs (line 84)
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}
examples/use_completions.rs (line 64)
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}
Source

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?
examples/use_mistral_tools.rs (line 50)
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
Hide additional examples
examples/use_xai_tools.rs (line 55)
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}
examples/use_anthropic_tools.rs (line 76)
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}
examples/use_google_tools.rs (line 53)
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}
examples/use_openai_responses.rs (line 85)
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}
Source

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?
examples/use_google_tools.rs (line 54)
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}
Source

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?
examples/use_anthropic_tools.rs (line 135)
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
Hide additional examples
examples/use_openai_responses.rs (line 136)
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}
Source

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.

Source

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?
examples/use_mistral_tools.rs (lines 53-54)
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
Hide additional examples
examples/use_completions_vertex.rs (line 38)
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}
examples/use_xai_tools.rs (lines 58-59)
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}
examples/use_anthropic_tools.rs (lines 79-80)
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}
examples/use_google_tools.rs (lines 57-58)
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}
examples/use_openai_responses.rs (line 88)
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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more