chain_example/
chain_example.rs1use llm::builder::{LLMBackend, LLMBuilder};
11use llm::chain::LLMRegistry;
12use llm::chain::MultiChainStepMode;
13use prompt_store::{PromptStore, RunError, RunOutput};
14
15#[tokio::main]
16async fn main() -> Result<(), RunError> {
17 let password = std::env::var("PROMPT_STORE_PASSWORD")
20 .expect("PROMPT_STORE_PASSWORD must be set for this example.");
21 let store = PromptStore::with_password(&password)?;
22
23 let openai_llm = LLMBuilder::new()
25 .backend(LLMBackend::OpenAI)
26 .api_key(std::env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set"))
27 .model("gpt-4o-mini")
28 .max_tokens(1000)
29 .build()
30 .unwrap();
31
32 let anthropic_llm = LLMBuilder::new()
33 .backend(LLMBackend::Anthropic)
34 .api_key(std::env::var("ANTHROPIC_API_KEY").expect("ANTHROPIC_API_KEY must be set"))
35 .model("claude-3-5-sonnet-20240620")
36 .max_tokens(1000)
37 .build()
38 .unwrap();
39
40 let mut registry = LLMRegistry::new();
41 registry.insert("openai_fast", openai_llm);
42 registry.insert("anthropic_strong", anthropic_llm);
43
44 let user_question = "How does photosynthesis work at the molecular level?";
46
47 println!("Executing prompt chain for: \"{}\"", user_question);
48
49 let outputs = store
50 .chain(®istry) .step("analyse", "9k6zezem")
54 .with_mode(MultiChainStepMode::Chat)
55 .with_provider("openai_fast")
56 .step("suggestions", "uetgwnq1")
59 .with_mode(MultiChainStepMode::Chat)
60 .with_provider("anthropic_strong")
61 .step("final_response", "dkeodfyp")
64 .with_mode(MultiChainStepMode::Chat)
65 .with_provider("anthropic_strong")
66 .step_raw(
67 "raw",
68 "Synthesize the following: {{final_response}} in 2 sentences.",
69 )
70 .with_mode(MultiChainStepMode::Chat)
71 .with_provider("anthropic_strong")
72 .vars([("query", user_question)])
74 .run()
75 .await?;
76
77 if let RunOutput::Chain(map) = outputs {
79 println!("\n--- Chain Execution Complete ---");
80 println!(
81 "\n[✅] Final Answer (from 'final_response' step):\n{}",
82 map.get("final_response").unwrap_or(&"N/A".to_string())
83 );
84 println!("\n--- Intermediate Steps ---");
85 println!(
86 "\n[1] Analysis ('analyse'):\n{}",
87 map.get("analyse").unwrap_or(&"N/A".to_string())
88 );
89 println!(
90 "\n[2] Suggestions ('suggestions'):\n{}",
91 map.get("suggestions").unwrap_or(&"N/A".to_string())
92 );
93 println!(
94 "\n[3] Raw ('raw'):\n{}",
95 map.get("raw").unwrap_or(&"N/A".to_string())
96 );
97 }
98
99 Ok(())
100}