Crates.io | anthropic-sdk |
lib.rs | anthropic-sdk |
version | 0.1.5 |
source | src |
created_at | 2024-03-29 14:19:50.258107 |
updated_at | 2024-07-23 17:35:17.587989 |
description | Anthropic Rust SDK. |
homepage | https://github.com/mixpeal/anthropic-sdk |
repository | https://github.com/mixpeal/anthropic-sdk |
max_upload_size | |
id | 1190162 |
size | 69,619 |
The Anthropic SDK for Rust provides a simple and efficient way to interact with the Anthropic API, allowing you to send requests and process responses asynchronously. This SDK supports both streaming and non-streaming responses, making it versatile for different use cases.
Add the following to your Cargo.toml
file:
[dependencies]
anthropic_sdk = "0.1.5"
dotenv = "0.15.0"
serde_json = "1.0"
tokio = { version = "1.0", features = ["full"] }
For non-streaming responses, you can use the SDK as follows:
// examples/basic_usage.rs
use anthropic_sdk::Client;
use dotenv::dotenv;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
let request = Client::new()
.version("2023-06-01")
.auth(secret_key.as_str())
.model("claude-3-opus-20240229")
.messages(&json!([
{"role": "user", "content": "Write me a poem about bravery"}
]))
.max_tokens(1024)
// .verbose(true) // Uncomment to return the response as it is from Anthropic
.build()?;
if let Err(error) = request
.execute(|text| async move { println!("{text}") })
.await
{
eprintln!("Error: {error}");
}
Ok(())
}
This example demonstrates how to send a request to the Anthropic API and print the response.
For streaming responses, the SDK can be used as follows:
// examples/streaming_usage.rs
use anthropic_sdk::Client;
use dotenv::dotenv;
use serde_json::json;
use std::sync::{Arc, Mutex};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
let request = Client::new()
.auth(secret_key.as_str())
.model("claude-3-opus-20240229")
.messages(&json!([
{"role": "user", "content": "Write me a poem about bravery"}
]))
.system("Make it sound like Edgar Allan Poe")
.temperature(0.1)
.max_tokens(1024)
.stream(true)
.build()?;
let message = Arc::new(Mutex::new(String::new()));
let message_clone = message.clone();
if let Err(error) = request
.execute(move |text| {
let message_clone = message_clone.clone();
async move {
println!("{text}");
{
let mut message = message_clone.lock().unwrap();
*message = format!("{}{}", *message, text);
}
}
})
.await
{
eprintln!("Error: {error}");
}
let final_message = message.lock().unwrap();
println!("Message: {}", *final_message); // or get the whole message at the end
Ok(())
}
In this example, the response is processed as it streams in. Each chunk of text is printed as soon as it's received, and the entire message is also available at the end.
For using Anthropic Tool use
, the SDK can be used as follows:
// examples/tool_use_usage.rs
use anthropic_sdk::{Client, ToolChoice};
use dotenv::dotenv;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
let request = Client::new()
.auth(secret_key.as_str())
.model("claude-3-opus-20240229")
.beta("tools-2024-04-04") // add the beta header
.tools(&json!([
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
]))
.tool_choice(ToolChoice::Auto)
.messages(&json!([
{
"role": "user",
"content": "What is the weather like in San Francisco?"
}
]))
.max_tokens(1024)
.build()?;
if let Err(error) = request
.execute(|text| async move { println!("{text}") })
.await
{
eprintln!("Error: {error}");
}
Ok(())
}
This example demonstrates how to use tools with the Anthropic API to perform specific tasks, such as getting the weather.
version
: (Optional) Specifies the version of the API to use.auth
: Sets the authentication token for the API.model
: Defines the model to use for generating responses.messages
: Contains the input messages for the API to process.max_tokens
: (Optional) Limits the number of tokens in the response.stream
: (Optional) Enables streaming mode for receiving responses in real-time.temperature
: (Optional) Adjusts the randomness of the response generation.system
: (Optional) Provides additional context or instructions for the response.tools
: (Optional) Specifies tools to use for specialized tasks.tool_choice
: (Optional) Specifies the tool to use when multiple tools are available.verbose
: (Optional) When set to true, returns the raw response from the API.metadata
: (Optional) Includes additional information about the request.stop_sequences
: (Optional) Specifies sequences where the API should stop generating further tokens.top_k
: (Optional) Limits the model to only sample from the top K most likely next tokens.top_p
: (Optional) Uses nucleus sampling to limit the model to a cumulative probability.Before running the examples, make sure to set your Anthropic API key in an .env
file at the root of your project:
ANTHROPIC_API_KEY=your_api_key_here
Alternatively, you can set the ANTHROPIC_API_KEY
environment variable in your system.
If you have any ideas or requests for new features, we'd love to hear from you! Please send your suggestions to hello@mixpeal.com, and we'll be sure to consider them for future updates. Your input is invaluable in helping us improve! 🌟🚀