forked from graniet/llm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_example.rs
More file actions
32 lines (28 loc) · 1022 Bytes
/
memory_example.rs
File metadata and controls
32 lines (28 loc) · 1022 Bytes
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
// Memory integration example
use llm::{
builder::{LLMBackend, LLMBuilder},
chat::ChatMessage,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create LLM with automatic memory
let llm = LLMBuilder::new()
.backend(LLMBackend::OpenAI)
.api_key(std::env::var("OPENAI_API_KEY").unwrap_or("sk-TESTKEY".into()))
.model("gpt-3.5-turbo")
.sliding_window_memory(5)
.build()?;
// First conversation
let messages1 = vec![ChatMessage::user().content("My name is Alice").build()];
match llm.chat(&messages1).await {
Ok(response) => println!("Response 1: {response}"),
Err(e) => eprintln!("Error 1: {e}"),
}
// Second conversation - should remember Alice's name
let messages2 = vec![ChatMessage::user().content("What's my name?").build()];
match llm.chat(&messages2).await {
Ok(response) => println!("Response 2: {response}"),
Err(e) => eprintln!("Error 2: {e}"),
}
Ok(())
}