Anthropic provides access to powerful language models through their API. This document outlines how to use the Anthropic API within the OneSDK framework.
To use the Anthropic API, you first need to initialize the OneSDK with your Anthropic API key:
from llm_onesdk import OneSDK
anthropic_sdk = OneSDK("anthropic", {
"api_key": "your_api_key_here",
"api_url": "https://api.anthropic.com" # Optional: Use this to override the default base URL
})
Alternatively, you can set the API key as an environment variable ANTHROPIC_API_KEY
, and the SDK will automatically use it.
To get a list of available models:
models = anthropic_sdk.list_models()
print(models)
To get information about a specific model:
model_info = anthropic_sdk.get_model("claude-3-opus-20240229")
print(model_info)
To generate text, use the generate
method. You need to specify the model and provide a list of messages:
model = "claude-3-opus-20240229"
messages = [{"role": "user", "content": "Explain quantum computing in simple terms."}]
response = anthropic_sdk.generate(model, messages, max_tokens=1000)
print(response['content'][0]['text'])
You can customize the generation with additional parameters:
response = anthropic_sdk.generate(
model,
messages,
max_tokens=1000,
temperature=0.7,
top_p=0.9,
stop=["END"],
metadata={"user_id": "12345"}
)
For longer responses or to get partial results as they're generated, use the stream_generate
method:
for chunk in anthropic_sdk.stream_generate(model, messages, max_tokens=1000):
print(chunk['delta']['text'], end='', flush=True)
To estimate the number of tokens in your input:
token_count = anthropic_sdk.count_tokens(model, messages)
print(f"Token count: {token_count}")
To use a proxy for API calls:
anthropic_sdk.set_proxy("http://your-proxy-url:port")
The API supports sending images as part of the message content. To include an image:
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image",
"source": {
"type": "path",
"path": "/path/to/your/image.jpg",
"media_type": "image/jpeg"
}
}
]
}
]
response = anthropic_sdk.generate(model, messages)
If you need to use a different API endpoint (e.g., for testing or using a proxy service), you can specify a custom base URL during initialization:
anthropic_sdk = OneSDK("anthropic", {
"api_key": "your_api_key_here",
"api_url": "https://your-custom-endpoint.com"
})
The SDK uses API version "2023-06-01" by default. If you need to use a different version, you can modify the API_VERSION
class variable in the API
class:
from llm_onesdk.providers.anthropic.api import API
API.API_VERSION = "your-desired-version"
Note: Changing the API version may affect the behavior and available features. Consult the official Anthropic API documentation for version-specific information.
The SDK will raise InvokeError
or its subclasses for various error conditions. Always wrap your API calls in try-except blocks:
from llm_onesdk.utils.error_handler import InvokeError, InvokeConnectionError, InvokeRateLimitError, InvokeAuthorizationError, InvokeBadRequestError
try:
response = anthropic_sdk.generate(model, messages)
except InvokeConnectionError as e:
print(f"Connection error: {str(e)}")
except InvokeRateLimitError as e:
print(f"Rate limit exceeded: {str(e)}")
except InvokeAuthorizationError as e:
print(f"Authorization error: {str(e)}")
except InvokeBadRequestError as e:
print(f"Bad request: {str(e)}")
except InvokeError as e:
print(f"An error occurred: {str(e)}")
The SDK uses Python's logging module. To enable debug logging:
import logging
logging.basicConfig(level=logging.DEBUG)
This will print detailed information about API requests and responses, which can be helpful for troubleshooting.
- Use the most appropriate model for your task.
- Implement proper error handling and retries for production applications.
- Be mindful of rate limits and implement appropriate backoff strategies.
- Keep your API key secure and never expose it in client-side code.
- When working with large responses, use the streaming API to improve responsiveness.
- Set a reasonable
max_tokens
value to control the length of generated responses. - Use the
count_tokens
method to estimate costs and stay within token limits. - When processing sensitive data, ensure you're complying with data protection regulations.
- Regularly update the SDK to benefit from the latest features and bug fixes.
- Use environment variables for API keys in production environments.
- The SDK currently does not support fine-tuning or training custom models.
- Some advanced features of the Anthropic API may not be directly accessible through this SDK. Refer to the official Anthropic API documentation for the most up-to-date information.
This SDK uses the Anthropic API version "2023-06-01" by default. Make sure your use cases are compatible with this version, or change it as described in the "Handling Different API Versions" section.
For more detailed information about available models, specific features, and API updates, please refer to the official Anthropic API documentation.