/
create_a_message_with_vision.rs
87 lines (75 loc) · 2.46 KB
/
create_a_message_with_vision.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
//! This example demonstrates how to use the `create_a_message` API with vision.
//!
//! ```shell
//! $ cargo run --example create_a_message_with_vision -- -p <prompt> -m <message> -i <image-path>
//! ```
//!
//! e.g.
//! ```shell
//! $ cargo run --example create_a_message_with_vision -- -p "You are a excellent AI assistant." -m "What animal is in this image?" -i "path/to/image.png"
//! ```
use std::path::PathBuf;
use base64::Engine;
use clap::Parser;
use clust::messages::ClaudeModel;
use clust::messages::ContentBlock;
use clust::messages::ImageContentSource;
use clust::messages::ImageMediaType;
use clust::messages::MaxTokens;
use clust::messages::Message;
use clust::messages::MessagesRequestBody;
use clust::messages::SystemPrompt;
use clust::Client;
#[derive(Parser)]
struct Arguments {
#[arg(short, long)]
prompt: String,
#[arg(short, long)]
message: String,
#[arg(short, long)]
image_path: 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. Read image file and encode it to Base64.
let image_file = tokio::fs::read(&arguments.image_path).await?;
let image_base64 = base64::prelude::BASE64_STANDARD.encode(&image_file);
let image_source = ImageContentSource::base64(
ImageMediaType::from_path(&PathBuf::from(&arguments.image_path))?,
image_base64,
);
// 3. Create a request body.
let model = ClaudeModel::Claude3Sonnet20240229;
let messages = vec![Message::user(vec![
ContentBlock::from(image_source),
ContentBlock::from(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()
};
// 4. Call the API.
let response = client
.create_a_message(request_body)
.await?;
println!("Result:\n{}", response);
// 5. Use the text content.
println!(
"Content: {}",
response
.content
.flatten_into_text()?
);
Ok(())
}