#api-client #anthropic #client-send

anthropic_client

与Anthropic API交互的库

6 个版本 (1 个稳定版本)

1.0.0 2024年4月15日
0.1.4 2024年3月12日

#1493 in 网页编程

MIT 许可协议

8KB
98

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

使用方法

以下是如何使用anthropic_client crate 向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);
        }
    }
}

图像示例

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);
        }
    }
}

在运行代码之前,请确保设置ANTHROPIC_API_KEY环境变量为您的Anthropic API密钥。确保设置ANTHROPIC_API_URL环境变量为您的Anthropic API URL。确保在运行代码之前设置ANTHROPIC_API_VERSION环境变量为您的Anthropic API版本。

ANTHROPIC_API_URL=https://api.anthropic.com/v1/messages
ANTHROPIC_API_VERSION=2023-06-01
ANTHROPIC_API_KEY=xxxxxxxx

许可协议

本项目采用MIT许可协议。有关详细信息,请参阅LICENSE文件。

依赖项

~6–17MB
~252K SLoC