Crates.io | anthropic_client |
lib.rs | anthropic_client |
version | 1.0.0 |
source | src |
created_at | 2024-03-12 08:06:04.322971 |
updated_at | 2024-04-15 16:00:13.277978 |
description | A library for interacting with the Anthropic API |
homepage | |
repository | https://github.com/sivanbil/anthropic_client.git |
max_upload_size | |
id | 1170284 |
size | 10,809 |
# Anthropic API Client
A Rust library for interacting with the Anthropic API.
## Installation
Add the following to your `Cargo.toml` file:
```toml
[dependencies]
anthropic_client = "1.0.0"
Here's a simple example of how to use the anthropic_client
crate to send a request to the Anthropic API:
use anthropic_client::send_test_request;
fn main() {
let content = "Hello, Anthropic! My name is sivanliao.";
let model = "claude-3-opus-20240229";
let anthropic_client = Anthropic::new();
let messages = vec![
json!({"role": "user", "content": content}),
];
match anthropic_client.messages.create(&anthropic_client, model, 1024, &messages) {
Ok(response) => {
println!("{:?}", response);
// Assert the response or perform other checks
assert!(!response.is_empty());
}
Err(error) => {
// Handle the error case
panic!("Error: {}", error);
}
}
}
Image example
use anthropic_client::send_image_request;
fn main() {
let image_url = "https://imagepphcloud.thepaper.cn/pph/image/300/508/637.jpg";
let image_media_type = "image/jpeg";
let model = "claude-3-opus-20240229";
let anthropic_client = Anthropic::new();
let messages = Anthropic::pack_vision_content(image_url.parse().unwrap(), image_media_type);
match messages {
Ok(pack_messages) => {
match anthropic_client.messages.create(&anthropic_client, model, 1024, &pack_messages) {
Ok(response) => {
println!("{:?}", response);
// Assert the response or perform other checks
assert!(!response.is_empty());
}
Err(error) => {
// Handle the error case
panic!("Error: {}", error);
}
}
}
Err(error) => {
panic!("Error: {}", error);
}
}
}
Make sure to set the ANTHROPIC_API_KEY
environment variable with your Anthropic API key before running the code.
Make sure to set the ANTHROPIC_API_URL
environment variable with your Anthropic API key before running the code.
Make sure to set the ANTHROPIC_API_VERSION
environment variable with your Anthropic API key before running the code.
ANTHROPIC_API_URL=https://api.anthropic.com/v1/messages
ANTHROPIC_API_VERSION=2023-06-01
ANTHROPIC_API_KEY=xxxxxxxx
This project is licensed under the MIT License. See the LICENSE file for details.