#api-client #anthropic #client-send

anthropic_client

A library for interacting with the Anthropic API

6 releases (1 stable)

1.0.0 Apr 15, 2024
0.1.4 Mar 12, 2024

#815 in Web programming

MIT license

8KB
98 lines

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

Dependencies

~6–17MB
~243K SLoC