Anthropic SDK for Rust
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.
Features ✨
- Asynchronous request handling 🚀
- Support for streaming API responses 🌊
- Easy configuration for authentication and request parameters 🔧
- Error handling with detailed messages 🛠️💬
- Optional verbose mode for raw response output 📃
Installation
Add the following to your Cargo.toml file:
[]
= "0.1.5"
= "0.15.0"
= "1.0"
= { = "1.0", = ["full"] }
Usage
Basic Usage
For non-streaming responses, you can use the SDK as follows:
// examples/basic_usage.rs
use Client;
use dotenv;
use json;
async
This example demonstrates how to send a request to the Anthropic API and print the response.
Streaming Usage
For streaming responses, the SDK can be used as follows:
// examples/streaming_usage.rs
use Client;
use dotenv;
use json;
use ;
async
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.
Tool Use
For using Anthropic Tool use, the SDK can be used as follows:
// examples/tool_use_usage.rs
use ;
use dotenv;
use json;
async
This example demonstrates how to use tools with the Anthropic API to perform specific tasks, such as getting the weather.
Fields Explanation
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.
Configuration
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.
Feature Requests 📬
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! 🌟🚀