use rai_sdk::{ClientBuilder, GenerationConfig, Model, schemars::JsonSchema};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
struct Recipe {
name: String,
ingredients: Vec<String>,
steps: Vec<String>,
prep_time_minutes: u32,
difficulty: String,
}
#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::new().model(Model::gpt4o_mini()).build()?;
let config = GenerationConfig::default()
.with_temperature(0.2)
.with_json_schema_for::<Recipe>()?;
let request = client
.request()
.prompt("Give me a recipe for a simple and tasty chocolate cake.")
.config(config);
println!("Sending request to OpenAI (expecting structured JSON)...");
let response = request.generate().await?;
let output_text = response.text();
println!("\nRaw JSON Response:\n{}", output_text);
let recipe: Recipe = serde_json::from_str(&output_text)?;
println!("\nParsed Recipe Object:");
println!("Name: {}", recipe.name);
println!("Prep Time: {} min", recipe.prep_time_minutes);
println!("Difficulty: {}", recipe.difficulty);
println!("Ingredients: {:?}", recipe.ingredients);
Ok(())
}