use futures_util::StreamExt;
use rstructor::{Instructor, LLMClient, OpenAIClient};
use serde::{Deserialize, Serialize};
#[derive(Instructor, Serialize, Deserialize, Debug)]
#[llm(description = "A notable invention")]
struct Invention {
#[llm(description = "Name of the invention")]
name: String,
#[llm(description = "Year it was invented")]
year: i32,
#[llm(description = "Who invented it")]
inventor: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = OpenAIClient::from_env()?;
println!("--- Streaming a list of inventions (one at a time) ---\n");
let mut stream =
client.materialize_iter::<Invention>("List 8 important inventions of the 20th century.");
let mut inventions = Vec::new();
while let Some(item) = stream.next().await {
let invention = item?; println!(
" tentative {}. {} ({}) — {}",
inventions.len() + 1,
invention.name,
invention.year,
invention.inventor
);
inventions.push(invention);
}
println!(
"\nStream completed cleanly; {} items are authoritative.",
inventions.len()
);
Ok(())
}