use ai_lib_rust::types::tool::{FunctionDefinition, ToolDefinition};
use ai_lib_rust::{AiClient, Message};
fn web_search_tool() -> ToolDefinition {
ToolDefinition {
tool_type: "function".to_string(),
function: FunctionDefinition {
name: "web_search".to_string(),
description: Some("Search the web for up-to-date information.".to_string()),
parameters: Some(serde_json::json!({
"type": "object",
"properties": {
"query": { "type": "string", "description": "Search query" },
"top_k": { "type": "integer", "minimum": 1, "maximum": 5, "default": 3 }
},
"required": ["query"]
})),
},
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
if std::env::var("DEEPSEEK_API_KEY").is_err() {
eprintln!("Error: DEEPSEEK_API_KEY environment variable is not set.");
eprintln!(
"Run with: DEEPSEEK_API_KEY=your_key cargo run --example deepseek_tool_call_stream"
);
std::process::exit(1);
}
let client = AiClient::new("deepseek/deepseek-chat").await?;
let messages = vec![
Message::system("You are a helpful assistant. You MUST call the provided tool before answering."),
Message::user("Call the web_search tool EXACTLY ONCE with JSON arguments {\"query\":\"latest stable Rust version\",\"top_k\":3}. Do not answer in natural language."),
];
let tool_choice = serde_json::json!({
"type": "function",
"function": { "name": "web_search" }
});
let resp = client
.chat()
.messages(messages)
.temperature(0.0)
.max_tokens(256)
.tools(vec![web_search_tool()])
.tool_choice(tool_choice)
.execute()
.await?;
println!("\n\n--- Content ---\n{}", resp.content);
println!("\n--- Tool calls ---\n{:#?}", resp.tool_calls);
println!("\n--- Usage ---\n{:#?}", resp.usage);
Ok(())
}