use secretary::llm_providers::openai::OpenAILLM;
use secretary::traits::{AsyncGenerateData, Task};
use serde::{Deserialize, Serialize};
use tokio;
#[derive(Task, Serialize, Deserialize, Debug)]
struct Details {
#[task(instruction = "Extract the price as a float")]
pub price: f64,
#[task(instruction = "Extract the product category or type")]
pub category: String,
#[task(instruction = "Extract the brand name if mentioned")]
pub brand: Option<String>,
}
#[derive(Task, Serialize, Deserialize, Debug)]
pub struct Feature {
#[task(instruction = "description of the feature")]
pub description: String,
}
#[derive(Task, Serialize, Deserialize, Debug)]
struct ProductExtraction {
#[task(instruction = "Extract the product name or title")]
pub name: String,
#[task(instruction = "Extract key features or description")]
pub description: Vec<Feature>,
#[task(instruction = "Determine if the product is in stock (true/false)")]
pub in_stock: bool,
pub details: Details,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
println!("Secretary Async Example - Product Information Extraction");
println!("{}", "=".repeat(60));
let task = ProductExtraction::new();
let additional_instructions = vec![
"Be precise with numerical values".to_string(),
"Use 'Unknown' for missing information".to_string(),
"Ensure boolean values are accurate".to_string(),
];
let product_text = "
Apple MacBook Pro 16-inch - $2,499
The latest MacBook Pro features the powerful M3 Pro chip,
16GB unified memory, and 512GB SSD storage. Perfect for
professional video editing and software development.
Category: Laptop Computer
Status: In Stock
Brand: Apple
";
println!("Input text:");
println!("{}", product_text);
println!();
println!("Generated System Prompt:");
println!("{}", task.get_system_prompt());
println!();
println!("Setting up async LLM call (requires API credentials):");
let llm = OpenAILLM::new(
&std::env::var("SECRETARY_OPENAI_API_BASE").unwrap(),
&std::env::var("SECRETARY_OPENAI_API_KEY").unwrap(),
&std::env::var("SECRETARY_OPENAI_MODEL").unwrap(),
)?;
println!("Making async request to LLM...");
let result: ProductExtraction = llm
.async_generate_data(&task, product_text, &additional_instructions)
.await?;
println!("Generated Data Structure: {:#?}", result);
println!();
println!("Example completed successfully!");
Ok(())
}