Crates.io | rusty-anthropic |
lib.rs | rusty-anthropic |
version | 0.1.1 |
source | src |
created_at | 2024-08-08 14:19:57.222243 |
updated_at | 2024-09-21 12:45:24.880452 |
description | A Rust SDK for interacting with the Anthropic API, supporting Vision and more. |
homepage | |
repository | https://github.com/pleaseful/rusty-anthropic |
max_upload_size | |
id | 1329601 |
size | 20,337 |
A Rust client for interacting with the Anthropic API, featuring examples for messages and text completions. This client utilizes the dotenv
crate to manage environment variables securely.
dotenv
crateTo use the Rusty Anthropics Client, follow these steps:
Cargo.toml
:[dependencies]
rusty-anthropic = "0.1.1"
reqwest = { version = "0.12.5", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
dotenv = "0.15.0"
.env
file in the root directory of your project to store your API key:API_KEY=your_api_key_here
The client uses the dotenv
crate to load environment variables. Ensure that you have a .env
file in the root directory of your project with the following content:
API_KEY=your_api_key_here
This file should be included in your .gitignore
to prevent your API key from being committed to version control.
Below is an example showing how to use the Messages API and the Text Completions API.
main.rs
and add the following content:use rusty_anthropic::anthropic_api::client::AnthropicClient;
use rusty_anthropic::anthropic_api::messages::{MessagesApi, MessageRequest};
use rusty_anthropic::request_client::RequestClient;
use serde_json::json;
use dotenv::dotenv;
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load environment variables from .env file
dotenv().ok();
// Retrieve the API key from the environment variable
let api_key = env::var("API_KEY").expect("API_KEY environment variable not set");
// Initialize the RequestClient with your API key
let request_client = RequestClient::new(api_key);
// Create an Anthropic client instance
let base_url = "https://api.anthropic.com/v1";
let anthropic_client = AnthropicClient::new(&request_client, base_url);
// Create a Messages API instance
let messages_api = MessagesApi::new(&anthropic_client);
// Create a message request
let model = "claude-3-5-sonnet-20240620".to_string();
let messages = vec![json!({"role": "user", "content": "Hello, Claude"})];
let request = MessageRequest::new(model, messages)
.max_tokens(1024)
.temperature(1.0);
// Send the request and get the response
let response_result = messages_api.create(request).await;
// Handle and print the response as JSON
match response_result {
Ok(response) => {
println!("{}", response.to_string());
},
Err(e) => {
let error_response = json!({
"error": e.to_string()
});
println!("{}", error_response.to_string());
}
}
Ok(())
}
Please visit this link for the documentation (WIP): here
This project is licensed under the terms of the MIT license. See the LICENSE file for details.