anthropic_client

Crates.ioanthropic_client
lib.rsanthropic_client
version1.0.0
sourcesrc
created_at2024-03-12 08:06:04.322971
updated_at2024-04-15 16:00:13.277978
descriptionA library for interacting with the Anthropic API
homepage
repositoryhttps://github.com/sivanbil/anthropic_client.git
max_upload_size
id1170284
size10,809
c-role (sivanbil)

documentation

README

# 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"

Usage

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

License

This project is licensed under the MIT License. See the LICENSE file for details.

Commit count: 4

cargo fmt