Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Documentation for AnthropicService Python Module
This Python module provides an asynchronous interface to interact with the Anthropic API, specifically targeting the
chat/completions
endpoint. It is designed to encapsulate the process of making API requests, handling rate limits, and processing responses.Requirements
aiohttp
library for asynchronous HTTP requestsConfiguration
Before using the
AnthropicService
, you need to obtain an API key from Anthropic. Once acquired, you can set it as an environment variableANTHROPIC_API_KEY
or pass it directly to theAnthropicService
constructor.Module Components
AnthropicService Class
This class provides methods to interact with the Anthropic API.
base_url
(str): The base URL for the Anthropic API.available_endpoints
(list): A list of available API endpoints.schema
(dict): The schema defining the structure for API requests. Needs to be defined according to Anthropic's API documentation.key_scheme
(str): Environment variable key name for the API key.token_encoding_name
(str): Encoding scheme used for rate limiting tokens.__init__(api_key=None, schema=None, token_encoding_name="cl100k_base", **kwargs)
: Initializes the service with an API key, request schema, and token encoding name.async init_endpoint(endpoint)
: Initializes an API endpoint, setting up any required rate limiters or other configurations.async serve(input_, endpoint="chat/completions", method="post", **kwargs)
: High-level method to serve requests. It initializes endpoints as needed and directs to the appropriate service method based on the endpoint.async serve_chat(messages, **kwargs)
: Handles requests to thechat/completions
endpoint.async call_api(payload, endpoint, method)
: Makes the API call. Implementers need to fill in this method withaiohttp
or another HTTP client library to perform the actual network request.create_payload(messages, schema, **kwargs)
: Generates the payload for the API request based on input messages and the provided schema.Usage Example
Notes
'your_api_key_here'
with your actual Anthropic API key.call_api
method needs to be implemented to perform HTTP requests.Error Handling
The module raises a
ValueError
if an unsupported endpoint is requested. Implementers should add try-except blocks to handle network errors, API rate limiting, and other exceptions that might occur during API calls.Extending the Module
To support more endpoints or add features:
available_endpoints
with new endpoints.serve_chat
for other endpoints.Remember to review Anthropic's API documentation for the latest endpoints, request formats, and best practices.