forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_example.rs
More file actions
71 lines (67 loc) · 2.97 KB
/
openai_example.rs
File metadata and controls
71 lines (67 loc) · 2.97 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
// Import required modules from the LLM library for OpenAI 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 OpenAI API key from environment variable or use test key as fallback
let api_key = std::env::var("OPENAI_API_KEY").unwrap_or("sk-TESTKEY".into());
// Initialize and configure the LLM client
let llm = LLMBuilder::new()
.backend(LLMBackend::OpenAI) // Use OpenAI as the LLM provider
.api_key(api_key) // Set the API key
.model("gpt-4.1-nano") // Use GPT-4.1 Nano model
.max_tokens(512) // Limit response length
.temperature(0.7) // Control response randomness (0.0-1.0)
.build()
.expect("Failed to build LLM (OpenAI)");
// 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(response) => {
// Print the response text
if let Some(text) = response.text() {
println!("Response: {text}");
}
// Print usage information
if let Some(usage) = response.usage() {
println!("\nUsage Information:");
println!(" Prompt tokens: {}", usage.prompt_tokens);
println!(" Completion tokens: {}", usage.completion_tokens);
println!(" Total tokens: {}", usage.total_tokens);
if let Some(completion_details) = &usage.completion_tokens_details {
if let Some(reasoning_tokens) = completion_details.reasoning_tokens {
println!(" Reasoning tokens: {reasoning_tokens}");
}
if let Some(audio_tokens) = completion_details.audio_tokens {
println!(" Audio tokens: {audio_tokens}");
}
}
if let Some(prompt_details) = &usage.prompt_tokens_details {
if let Some(cached_tokens) = prompt_details.cached_tokens {
println!(" Cached tokens: {cached_tokens}");
}
if let Some(audio_tokens) = prompt_details.audio_tokens {
println!(" Audio tokens (prompt): {audio_tokens}");
}
}
} else {
println!("No usage information available");
}
}
Err(e) => eprintln!("Chat error: {e}"),
}
Ok(())
}