use cloudllm::client_wrapper::{ClientWrapper, Message, Role};
use cloudllm::clients::openai::{Model, OpenAIClient};
use cloudllm::LLMSession;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_default_env()
.filter_level(log::LevelFilter::Info)
.init();
println!("\n{}", "=".repeat(80));
println!(" OpenAI Web Search Example");
println!(" Demonstrating Responses API with Tool Calling");
println!("{}\n", "=".repeat(80));
let openai_key = std::env::var("OPEN_AI_SECRET").expect("OPEN_AI_SECRET must be set");
let client = OpenAIClient::new_with_model_enum(&openai_key, Model::GPT5);
println!("✓ OpenAI client initialized with gpt-5");
println!("✓ Model: {}\n", client.model_name());
println!("{}", "=".repeat(80));
println!("Example 1: Basic Web Search");
println!("{}\n", "=".repeat(80));
let messages_1 = vec![
Message {
role: Role::System,
content: Arc::from("You are a helpful research assistant. Search the web for current information and provide a comprehensive answer."),
tool_calls: vec![],
},
Message {
role: Role::User,
content: Arc::from("What are the latest developments in artificial intelligence this week? Provide a brief summary of the top 3 recent developments."),
tool_calls: vec![],
},
];
println!("Sending request with web_search tool (context: high)...\n");
let response_1 = client.send_message(&messages_1, None).await?;
println!("Response:\n{}\n", response_1.content);
if let Some(usage) = client.get_last_usage().await {
println!(
"Tokens - Input: {}, Output: {}, Total: {}\n",
usage.input_tokens, usage.output_tokens, usage.total_tokens
);
}
println!("{}", "=".repeat(80));
println!("Example 2: Web Search with Geographic Filtering");
println!("{}\n", "=".repeat(80));
let messages_2 = vec![
Message {
role: Role::System,
content: Arc::from(
"You are a local events coordinator. Search for current events and news.",
),
tool_calls: vec![],
},
Message {
role: Role::User,
content: Arc::from(
"What are the major tech events happening in San Francisco this month?",
),
tool_calls: vec![],
},
];
println!("Sending request with geographic filtering (SF, CA)...\n");
let response_2 = client.send_message(&messages_2, None).await?;
println!("Response:\n{}\n", response_2.content);
if let Some(usage) = client.get_last_usage().await {
println!(
"Tokens - Input: {}, Output: {}, Total: {}\n",
usage.input_tokens, usage.output_tokens, usage.total_tokens
);
}
println!("{}", "=".repeat(80));
println!("Example 3: Web Search with LLMSession");
println!("{}\n", "=".repeat(80));
let session_client = Arc::new(OpenAIClient::new_with_model_enum(&openai_key, Model::GPT5));
let mut session = LLMSession::new(
session_client,
"You are a helpful assistant with access to real-time web information.".to_string(),
8192, );
println!("Starting multi-turn conversation with web search capabilities...\n");
let response = session
.send_message(
Role::User,
"What is the current stock price of Tesla and what were the major news stories about Tesla this week?".to_string(),
None,
)
.await?;
println!("Assistant: {}\n", response.content);
let usage_1 = session.token_usage();
println!(
"Session tokens - Input: {}, Output: {}, Total: {}\n",
usage_1.input_tokens, usage_1.output_tokens, usage_1.total_tokens
);
let follow_up = session
.send_message(
Role::User,
"Based on that information, what do you think might happen to Tesla's stock price in the next quarter?".to_string(),
None,
)
.await?;
println!("Assistant: {}\n", follow_up.content);
let usage_2 = session.token_usage();
println!(
"Session cumulative tokens - Input: {}, Output: {}, Total: {}\n",
usage_2.input_tokens, usage_2.output_tokens, usage_2.total_tokens
);
println!("{}", "=".repeat(80));
println!("Summary");
println!("{}\n", "=".repeat(80));
println!("✓ Web search tool working correctly");
println!("✓ Geographic filtering functional");
println!("✓ LLMSession multi-turn context maintained");
println!("\nAll OpenAI Responses API examples completed successfully!");
Ok(())
}