use serde_json::json;
use std::env;
use valyu::{AnswerRequest, ValyuClient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv().ok();
let api_key = env::var("VALYU_API_KEY")
.expect("VALYU_API_KEY must be set in .env file");
let client = ValyuClient::new(api_key);
let schema = json!({
"type": "object",
"properties": {
"summary": {
"type": "string",
"description": "A concise summary of the topic"
},
"key_points": {
"type": "array",
"items": {"type": "string"},
"description": "List of key points or findings"
},
"timeline": {
"type": "array",
"items": {
"type": "object",
"properties": {
"date": {"type": "string"},
"event": {"type": "string"}
}
},
"description": "Timeline of important events"
},
"impact": {
"type": "string",
"description": "Overall impact and significance"
}
},
"required": ["summary", "key_points"]
});
let query = "What is artificial intelligence?";
println!("❓ Question: {}\n", query);
let request = AnswerRequest::new(query)
.with_search_type("web")
.with_structured_output(schema);
println!("🔍 Generating structured answer...\n");
let response = client.answer(&request).await?;
println!("✅ Answer received!\n");
if let Some(data_type) = &response.data_type {
println!("📋 Data type: {}", data_type);
}
if let Some(contents) = &response.contents {
println!("\n📊 Structured Answer:");
println!("{}", "=".repeat(80));
println!("{}", serde_json::to_string_pretty(contents)?);
println!("{}", "=".repeat(80));
}
if let Some(sources) = &response.search_results {
println!("\n📚 Sources used: {}", sources.len());
}
if let Some(cost) = &response.cost {
if let Some(total) = cost.total_dollars {
println!("\n💰 Total cost: ${:.4}", total);
}
}
Ok(())
}