parallel_example/
parallel_example.rs1use llm::builder::{LLMBackend, LLMBuilder};
13use llm::chain::LLMRegistry;
14use prompt_store::{PromptStore, RunError, RunOutput};
15
16#[tokio::main]
17async fn main() -> Result<(), RunError> {
18 let store = PromptStore::init()?;
19
20 let openai_llm = LLMBuilder::new()
21 .backend(LLMBackend::OpenAI)
22 .api_key(std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set"))
23 .model("gpt-4o-mini")
24 .build()
25 .unwrap();
26
27 let mut registry = LLMRegistry::new();
28 registry.insert("openai", openai_llm);
29
30 let user_query = "Rust is a systems programming language focused on safety, speed, and concurrency. It achieves these goals without a garbage collector, using a unique ownership model with a borrow checker.";
31
32 println!("--- Running Advanced Chain ---");
33
34 let outputs = store
35 .chain(®istry)
36 .step("topic", "Extract Topic")
38 .with_provider("openai")
39 .parallel(|group| {
41 group
42 .step("summary", "Summarizer")
43 .step("keywords", "Keyword Extractor")
45 .with_provider("failing_provider")
46 })
47 .with_provider("openai") .on_error_stored("Basic Keyword Extractor")
50 .with_provider("openai")
51 .step_if("tweet", "Generate Tweet", |ctx| {
53 ctx.get("summary")
54 .map_or(false, |s| s.to_lowercase().contains("safety"))
55 })
56 .with_provider("openai")
57 .vars([("query", user_query)])
58 .run()
59 .await?;
60
61 if let RunOutput::Chain(map) = outputs {
62 println!("\n--- Chain Execution Complete ---");
63 println!("\n[1] Topic: {}", map.get("topic").unwrap_or(&"N/A".into()));
64 println!(
65 "\n[2a] Summary: {}",
66 map.get("summary").unwrap_or(&"N/A".into())
67 );
68 println!(
69 "\n[2b] Keywords (used fallback): {}",
70 map.get("keywords").unwrap_or(&"N/A".into())
71 );
72
73 if let Some(tweet) = map.get("tweet") {
74 println!("\n[3] Conditional Tweet: {}", tweet);
75 } else {
76 println!("\n[3] Conditional Tweet: SKIPPED (condition not met)");
77 }
78 }
79
80 Ok(())
81}