-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathapi_example.rs
More file actions
77 lines (71 loc) · 2.62 KB
/
api_example.rs
File metadata and controls
77 lines (71 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Example demonstrating how to serve multiple LLM backends through a REST API
//!
//! This example shows how to chain multiple LLM providers together to:
//! 1. Use Groq to perform initial calculation
//! 2. Use Claude to provide analysis and commentary
//! 3. Use GPT to improve and format the results
//! 4. Expose the chain through a REST API
//!
//! # Example Request
//! ```json
//! POST http://127.0.0.1:3000/v1/chat/completions
//! {
//! "model": "groq:deepseek-r1-distill-llama-70b",
//! "messages": [
//! {"role": "user", "content": "calcule 1x20"}
//! ],
//! "steps": [
//! {
//! "provider_id": "anthropic",
//! "id": "step1",
//! "template": "Analyze and comment on this calculation: {{initial}}",
//! "temperature": 0.7
//! },
//! {
//! "provider_id": "openai",
//! "id": "step2",
//! "template": "Improve and expand upon this mathematical analysis: {{step1}}",
//! "max_tokens": 500
//! },
//! {
//! "provider_id": "openai",
//! "id": "step3",
//! "template": "Format the following into a clear report:\nCalculation: {{initial}}\nAnalysis: {{step1}}\nExpanded Analysis: {{step2}}"
//! }
//! ]
//! }
//! ```
use llm::{
builder::{LLMBackend, LLMBuilder},
chain::LLMRegistryBuilder,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize OpenAI backend with API key and model settings
let openai_llm = LLMBuilder::new()
.backend(LLMBackend::OpenAI)
.api_key(std::env::var("OPENAI_API_KEY").unwrap_or("sk-OPENAI".into()))
.model("gpt-4")
.build()?;
// Initialize Anthropic backend with API key and model settings
let anthro_llm = LLMBuilder::new()
.backend(LLMBackend::Anthropic)
.api_key(std::env::var("ANTHROPIC_API_KEY").unwrap_or("anthro-key".into()))
.model("claude-3-5-sonnet-20240620")
.build()?;
// Initialize Groq backend with API key and model settings
let groq_llm = LLMBuilder::new()
.backend(LLMBackend::Groq)
.api_key(std::env::var("GROQ_API_KEY").unwrap_or("gsk-YOUR_API_KEY".into()))
.model("deepseek-r1-distill-llama-70b")
.build()?;
// Create registry to manage multiple backends
let registry = LLMRegistryBuilder::new()
.register("openai", openai_llm)
.register("anthropic", anthro_llm)
.register("groq", groq_llm)
.build();
// Start REST API server on localhost port 3000
registry.serve("127.0.0.1:3000").await?;
Ok(())
}