clust
An unofficial Rust client for the Anthropic/Claude API.
Installation
Run the following Cargo command in your project directory:
cargo add clust
or add the following line to your Cargo.toml:
[]
= "0.9.0"
Supported APIs
- Messages
Feature flags
macros
: Enable theclust::attributse::clust_tool
attribute macro for generatingclust::messages::Tool
orclust::messages::AsyncTool
from a Rust function.
Usages
API key and client
First you need to create a new API client: clust::Client
with your Anthropic API key from environment variable: "
ANTHROPIC_API_KEY"
use Client;
let client = from_env.unwrap;
or specify the API key directly:
use Client;
use ApiKey;
let client = from_api_key;
If you want to customize the client, you can use builder pattern by clust::ClientBuilder
:
use ClientBuilder;
use ApiKey;
use Version;
let client = new
.version
.client
.build;
Models and max tokens
You can specify the model by clust::messages::ClaudeModel
.
use ClaudeModel;
use MessagesRequestBody;
let model = Claude3Sonnet20240229;
let request_body = MessagesRequestBody ;
Because max number of tokens of text generation: clust::messages::MaxTokens
depends on the model,
you need to create clust::messages::MaxTokens
with the model.
use ClaudeModel;
use MaxTokens;
use MessagesRequestBody;
let model = Claude3Sonnet20240229;
let max_tokens = new.unwrap;
let request_body = MessagesRequestBody ;
Prompt
You can specify the system prompt by clust::messages::SystemPrompt
and there is no "system" role in the message.
use SystemPrompt;
use MessagesRequestBody;
let system_prompt = new;
let request_body = MessagesRequestBody ;
Messages and contents
Build messages by a vector of clust::messages::Message
:
use Role;
use Content;
/// The message.
You can create each role message as follows:
use Message;
let message = user;
let message = assistant;
and a content: clust::messages::Content
.
use ContentBlock;
/// The content of the message.
Multiple blocks is a vector of content block: clust::messages::ContentBlock
:
use TextContentBlock;
use ImageContentBlock;
/// The content block of the message.
You can create a content as follows:
use Content;
use ContentBlock;
use TextContentBlock;
use ImageContentBlock;
use ImageContentSource;
use ImageMediaType;
// Single text content
let content = SingleText;
// or use `From` trait
let content = from;
// Multiple content blocks
let content = MultipleBlocks;
// or use `From` trait for `String` or `ImageContentSource`
let content = from;
Request body
The request body is defined by clust::messages::MessagesRequestBody
.
See also MessagesRequestBody
for other options.
use MessagesRequestBody;
use ClaudeModel;
use Message;
use MaxTokens;
use SystemPrompt;
let request_body = MessagesRequestBody ;
You can also use the builder pattern with clust::messages::MessagesRequestBuilder
:
use MessagesRequestBuilder;
use ClaudeModel;
use Message;
use SystemPrompt;
let request_body = new_with_max_tokens.unwrap
.messages
.system
.build;
API calling
Call the API by clust::Client::create_a_message
with the request body.
use Client;
use MessagesRequestBody;
async
Streaming
When you want to stream the response incrementally,
you can use clust::Client::create_a_message_stream
with the stream option: StreamOption::ReturnStream
.
use Client;
use MessagesRequestBody;
use StreamOption;
use StreamExt;
async
Tool use
Support tool use for two methods:
1. Use clust_tool
attribute macro for Rust function
When you define a tool as Rust function with documentation comment like this:
/// Get the current weather in a given location
///
/// ## Arguments
/// - `location` - The city and state, e.g. San Francisco, CA
you can use the clust::clust_macros::clust_tool
attribute macro with macros
feature flag to generate code:
/// Get the current weather in a given location
///
/// ## Arguments
/// - `location` - The city and state, e.g. San Francisco, CA
// <- Generate `clust::messages::Tool` for this function
and create an instance of clust::messages::Tool
that named by ClustTool_{function_name}
from the function:
let tool = ClustTool_get_weather ;
Get the tool definition from clust::messages::Tool
for API request:
let tool_definition = tool.definition;
and call the tool with tool use got from the API response:
let tool_result = tool.call;
See also a tool use example and clust_tool for details.
2. Manually implement clust::messages::Tool
or clust::messages::AsyncTool
You can manually implement clust::messages::Tool
or clust::messages::AsyncTool
for your tool.
Examples
Create a message
An example of creating a message with the API key loaded from the environment variable: ANTHROPIC_API_KEY
ANTHROPIC_API_KEY={your-api-key}
is as follows:
use ClaudeModel;
use MaxTokens;
use Message;
use MessagesRequestBody;
use SystemPrompt;
use Client;
async
Streaming messages with tokio
backend
An example of creating a message stream with the API key loaded from the environment variable: ANTHROPIC_API_KEY
ANTHROPIC_API_KEY={your-api-key}
with tokio-stream is as follows:
use ClaudeModel;
use MaxTokens;
use Message;
use MessagesRequestBody;
use SystemPrompt;
use StreamOption;
use StreamChunk;
use Client;
use StreamExt;
async
}
println!;
Ok
}
Create a message with vision
Conversation
Tool use
See a tool use example.
Other examples
See also the examples directory for more examples.
Changelog
See CHANGELOG.
License
Licensed under either of the Apache License, Version 2.0 or the MIT license at your option.