-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcreate_a_message_with_builder.rs
73 lines (62 loc) · 1.83 KB
/
create_a_message_with_builder.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
//! This example demonstrates how to use the `create_a_message` API with builder pattern.
//!
//! ```shell
//! $ cargo run --example create_a_message_with_builder -- -p <prompt> -m <message>
//! ```
//!
//! e.g.
//! ```shell
//! $ cargo run --example create_a_message_with_builder -- -p "You are a excellent AI assistant." -m "Where is the capital of Japan?"
//! ```
use std::time::Duration;
use clap::Parser;
use clust::messages::ClaudeModel;
use clust::messages::Message;
use clust::messages::MessagesRequestBuilder;
use clust::messages::SystemPrompt;
use clust::ApiKey;
use clust::ClientBuilder;
#[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 builder pattern.
let client = ClientBuilder::new(ApiKey::from_env()?)
.version(clust::Version::V2023_06_01) // Custom API version
.client(
reqwest::ClientBuilder::new()
.timeout(Duration::from_secs(30))
.build()?,
) // Custom reqwest client
.build();
// 2. Create a request body with builder pattern.
let request_body = MessagesRequestBuilder::new_with_max_tokens(
ClaudeModel::Claude3Haiku20240307,
1024,
)?
.messages(vec![Message::user(
arguments.message,
)])
.system(SystemPrompt::new(arguments.prompt))
.build();
// 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(())
}