15 releases
new 0.0.15 | Jan 17, 2025 |
---|---|
0.0.14 | Jan 17, 2025 |
#818 in Web programming
764 downloads per month
46KB
672 lines
Anthropic Rust SDK
An unofficial Rust SDK for the Anthropic API.
Features
- Complete Anthropic API support
- Models API (List and Get models)
- Messages API (Create messages and count tokens)
- Message Batches API (Create and List batches)
- Robust async/await implementation using Tokio
- Comprehensive error handling with detailed error types
- Built-in pagination support for list operations
- Token counting utilities for accurate message length estimation
- Type-safe API with full Rust type definitions
- Easy-to-use builder patterns for request construction
Installation
cargo add anthropic-ai-sdk
Quick Start
use anthropic_ai_sdk::clients::AnthropicClient;
use anthropic_ai_sdk::types::message::{
CreateMessageParams, Message, MessageClient, MessageError, RequiredMessageParams, Role,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let anthropic_api_key = std::env::var("ANTHROPIC_API_KEY").unwrap();
let client = AnthropicClient::new::<MessageError>(anthropic_api_key, "2023-06-01").unwrap();
let body = CreateMessageParams::new(RequiredMessageParams {
model: "claude-3-5-sonnet-20240620".to_string(),
messages: vec![Message::new_text(Role::User, "Hello, Claude")],
max_tokens: 1024,
});
match client.create_message(Some(&body)).await {
Ok(message) => {
println!("Successfully created message: {:?}", message.content);
}
Err(e) => {
println!("Error: {}", e);
}
}
Ok(())
}
Examples
Check out the examples directory for more usage examples:
- Modesl
- List Models - How to retrieve a list of available models
- Get a Models - How to get a model
- Messages
- Message - How to create a message
- Count Message Tokens - How to count tokens in a message
- Message Batch
- Create a Message Batch - How to create a message batch
- List Message Batches - How to retrieve a list of message batches
API Coverage
- Models
- List Models
- Get a Model
- Messages
- Messages
- Count Message Tokens
- Message Batches
- Retrieve a Message Batch
- Retrieve Message Batch Results
- List Message Batches
- Cancel a Message Batch
- Delete a Message Batch
- Admin API
- Organization Member Management
- Get User
- List Users
- Update User
- Remove User
- Organization Invites
- Get Invite
- List Invites
- Create Invite
- Delete Invite
- Workspace Management
- Get Workspace
- List Workspaces
- Update Workspace
- Create Workspace
- Archive Workspace
- Workspace Member Management
- Get Workspace Member
- List Workspace Member
- Get Workspace Member
- Update Workspace Member
- Delete Workspace Member
- API Keys
- Get API Key
- List API Keys
- Update API Keys
- Organization Member Management
Development
Prerequisites
- Rust 1.81 or later
- An Anthropic API key
Running Tests
cargo test
Running Examples
Set your API key
export ANTHROPIC_API_KEY="your-api-key"
Run an example
cd examples/models/list-models
cargo run
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Security
If you discover a security vulnerability within this package, please send an e-mail to the maintainers. All security vulnerabilities will be promptly addressed.
Support
For support questions, please use the GitHub Issues.
Dependencies
~8–19MB
~255K SLoC