forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepseek_example.rs
More file actions
43 lines (38 loc) · 1.61 KB
/
deepseek_example.rs
File metadata and controls
43 lines (38 loc) · 1.61 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
// Import required modules from the LLM library for DeepSeek integration
use llm::{
builder::{LLMBackend, LLMBuilder}, // Builder pattern components
chat::ChatMessage, // Chat-related structures
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get DeepSeek API key from environment variable or use test key as fallback
let api_key = std::env::var("DEEPSEEK_API_KEY").unwrap_or("sk-TESTKEY".into());
// Initialize and configure the LLM client
let llm = LLMBuilder::new()
.backend(LLMBackend::DeepSeek) // Use DeepSeek as the LLM provider
.system("You are a helpful assistant and you response only with words begin with deepseek_")
.api_key(api_key) // Set the API key
.model("deepseek-reasoner") // Use DeepSeek Chat model
.timeout_seconds(1200)
.temperature(0.7) // Control response randomness (0.0-1.0)
.build()
.expect("Failed to build LLM (DeepSeek)");
// Prepare conversation history with example messages
let messages = vec![
ChatMessage::user()
.content("Tell me that you love cats")
.build(),
ChatMessage::assistant()
.content("I am an assistant, I cannot love cats but I can love dogs")
.build(),
ChatMessage::user()
.content("Tell me that you love dogs in 2000 chars")
.build(),
];
// Send chat request and handle the response
match llm.chat(&messages).await {
Ok(text) => println!("Chat response:\n{text}"),
Err(e) => eprintln!("Chat error: {e}"),
}
Ok(())
}