basic_usage/
basic_usage.rs1use orchestra_rs::{
2 error::Result, llm::LLM, messages::Message, model::ModelConfig,
3 providers::types::ProviderSource,
4};
5
6#[tokio::main]
12async fn main() -> Result<()> {
13 env_logger::init();
15
16 println!("š¼ Orchestra-rs Basic Usage Example\n");
17
18 simple_prompt().await?;
20
21 chat_with_history().await?;
23
24 custom_configuration().await?;
26
27 using_presets().await?;
29
30 println!("\nā
All examples completed successfully!");
31 Ok(())
32}
33
34async fn simple_prompt() -> Result<()> {
36 println!("š Example 1: Simple Prompt");
37 println!("===============================================================================");
38
39 let llm = LLM::gemini("gemini-2.5-flash");
41
42 let response = llm
44 .prompt("Hello! Can you tell me a fun fact about Rust programming language?")
45 .await?;
46
47 println!("š¤ Response: {}\n", response.text);
48 Ok(())
49}
50
51async fn chat_with_history() -> Result<()> {
53 println!("š¬ Example 2: Chat with History");
54 println!("===============================================================================");
55
56 let llm = LLM::gemini("gemini-2.5-flash");
57
58 let history = vec![
60 Message::human("Hi! I'm learning Rust and I'm confused about ownership."),
61 Message::assistant(
62 "Hello! I'd be happy to help you understand Rust ownership. It's one of Rust's most important concepts. What specific aspect of ownership would you like me to explain?",
63 ),
64 Message::human("What's the difference between moving and borrowing?"),
65 Message::assistant(
66 "Great question! Moving transfers ownership of a value, while borrowing allows temporary access without taking ownership. When you move a value, the original variable can no longer be used. When you borrow, you get a reference that allows you to use the value without owning it.",
67 ),
68 ];
69
70 let response = llm
72 .chat(
73 Message::human("Can you give me a simple code example of both?"),
74 history,
75 )
76 .await?;
77
78 println!("š¤ Response: {}\n", response.text);
79 Ok(())
80}
81
82async fn custom_configuration() -> Result<()> {
84 println!("āļø Example 3: Custom Configuration");
85 println!("===============================================================================");
86
87 let config = ModelConfig::new("gemini-2.5-flash")
89 .with_system_instruction("You are a helpful Rust programming tutor. Always provide practical examples and explain concepts clearly.")
90 .with_temperature(0.7)?
91 .with_top_p(0.9)?;
92
93 let llm =
95 LLM::new(ProviderSource::Gemini, "gemini-2.5-flash".to_string()).with_custom_config(config);
96
97 let response = llm
98 .prompt("Explain Rust's Result type and how to use it")
99 .await?;
100 println!("š¤ Response: {}\n", response.text);
101 Ok(())
102}
103
104async fn using_presets() -> Result<()> {
106 println!("šÆ Example 4: Using Presets");
107 println!("===============================================================================");
108
109 println!("š Conservative preset (focused, deterministic):");
111 let conservative_llm =
112 LLM::conservative(ProviderSource::Gemini, "gemini-2.5-flash".to_string());
113 let conservative_response = conservative_llm
114 .prompt("Write a one-sentence summary of what Rust is.")
115 .await?;
116 println!("Response: {}\n", conservative_response.text);
117
118 println!("šØ Creative preset (diverse, imaginative):");
120 let creative_llm = LLM::creative(ProviderSource::Gemini, "gemini-2.5-flash".to_string());
121 let creative_response = creative_llm
122 .prompt("Write a creative analogy to explain Rust's ownership system.")
123 .await?;
124 println!("Response: {}\n", creative_response.text);
125
126 println!("āļø Balanced preset (moderate creativity):");
128 let balanced_llm = LLM::balanced(ProviderSource::Gemini, "gemini-2.5-flash".to_string());
129 let balanced_response = balanced_llm
130 .prompt("Explain the benefits of using Rust for systems programming.")
131 .await?;
132 println!("Response: {}\n", balanced_response.text);
133
134 Ok(())
135}
136
137#[allow(dead_code)]
139async fn provider_capabilities() -> Result<()> {
140 println!("š Provider Capabilities");
141 println!("===============================================================================");
142
143 let llm = LLM::gemini("gemini-2.5-flash");
144
145 println!("Provider name: {}", llm.provider_name());
146 println!("Supports streaming: {}", llm.supports_streaming());
147 println!("Supports tools: {}", llm.supports_tools());
148 println!("Model name: {}", llm.get_model_name());
149
150 Ok(())
151}