-
Notifications
You must be signed in to change notification settings - Fork 6
/
conversation.rs
104 lines (87 loc) · 2.61 KB
/
conversation.rs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! This example demonstrates how to converse with the assistant.
//!
//! ```shell
//! $ cargo run --example conversation -- -p <prompt> -f <first> -s <second>
//! ```
//!
//! e.g.
//! ```shell
//! $ cargo run --example conversation -- -p "You are a excellent AI assistant." -f "Where is the capital of Japan?" -s "What is the population of the city?"
//! ```
use clust::messages::ClaudeModel;
use clust::messages::MaxTokens;
use clust::messages::Message;
use clust::messages::MessagesRequestBody;
use clust::messages::SystemPrompt;
use clust::Client;
use clap::Parser;
#[derive(Parser)]
struct Arguments {
#[arg(short, long)]
prompt: String,
#[arg(short, long)]
first: String,
#[arg(short, long)]
second: String,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// 0. Parse the command-line arguments.
let arguments = Arguments::parse();
// 1. Create a new API client with the API key loaded from the environment variable: `ANTHROPIC_API_KEY`
let client = Client::from_env()?;
// or specify the API key directly
// let client = Client::from_api_key(clust::ApiKey::new("your-api-key"));
// 2. Create a request body with the first message.
let model = ClaudeModel::Claude3Haiku20240307;
let max_tokens = MaxTokens::new(1024, model)?;
let prompt = SystemPrompt::new(arguments.prompt);
let messages = vec![Message::user(
arguments.first,
)];
let mut request_body = MessagesRequestBody {
model,
messages,
max_tokens,
system: Some(prompt),
..Default::default()
};
// 3. Call the API.
let response = client
.create_a_message(request_body.clone())
.await?;
println!("First result:\n{}", response);
// 4. Use the first text content.
println!(
"First content: {}",
response
.content
.flatten_into_text()?
);
// 5. Store the first assistant message.
request_body
.messages
.push(response.crate_message());
// 6. Add the second user message.
request_body
.messages
.push(Message::user(arguments.second));
// 7. Re-call the API.
let response = client
.create_a_message(request_body.clone())
.await?;
println!("Second result:\n{}", response);
// 8. Use the second text content.
println!(
"Second content: {}",
response
.content
.flatten_into_text()?
);
// 9. Store the second assistant message.
request_body
.messages
.push(response.crate_message());
// Continue the conversation...
Ok(())
}