-
Notifications
You must be signed in to change notification settings - Fork 12
/
create_a_message.rs
70 lines (60 loc) · 1.78 KB
/
create_a_message.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
//! This example demonstrates how to use the `create_a_message` API.
//!
//! ```shell
//! $ cargo run --example create_a_message -- -p <prompt> -m <message>
//! ```
//!
//! e.g.
//! ```shell
//! $ cargo run --example create_a_message -- -p "You are a excellent AI assistant." -m "Where is the capital of Japan?"
//! ```
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)]
message: 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.
let model = ClaudeModel::Claude3Haiku20240307;
let messages = vec![Message::user(
arguments.message,
)];
let max_tokens = MaxTokens::new(1024, model)?;
let system_prompt = SystemPrompt::new(arguments.prompt);
let request_body = MessagesRequestBody {
model,
messages,
max_tokens,
system: Some(system_prompt),
..Default::default()
};
// 3. Call the API.
let response = client
.create_a_message(request_body)
.await?;
println!("Result:\n{}", response);
// 4. Use the text content.
println!(
"Content: {}",
response
.content
.flatten_into_text()?
);
Ok(())
}