Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Draft for Anthropic service #233

Merged
merged 3 commits into from
Feb 6, 2024
Merged

Conversation

karandeeps18
Copy link
Contributor

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

  • Python 3.7+
  • aiohttp library for asynchronous HTTP requests
  • Access to Anthropic API and a valid API key

Configuration

Before using the AnthropicService, you need to obtain an API key from Anthropic. Once acquired, you can set it as an environment variable ANTHROPIC_API_KEY or pass it directly to the AnthropicService constructor.

Module Components

AnthropicService Class

This class provides methods to interact with the Anthropic API.

  1. Attributes
  • 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.
  1. Methods
  • __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 the chat/completions endpoint.
  • async call_api(payload, endpoint, method): Makes the API call. Implementers need to fill in this method with aiohttp 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

import asyncio

async def main():
    anthropic_service = AnthropicService(api_key='your_api_key_here')
    prompt = "Hello, Claude. I would like to know about your capabilities."
    response = await anthropic_service.serve(prompt, endpoint="chat/completions")
    print(response)

if __name__ == '__main__':
    asyncio.run(main())

Notes

  • Replace 'your_api_key_here' with your actual Anthropic API key.
  • The call_api method needs to be implemented to perform HTTP requests.
  • This module is designed to be extended with additional functionality, such as more endpoints or sophisticated error handling.

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:

  • Update available_endpoints with new endpoints.
  • Extend the schema to include new request formats.
  • Implement additional methods similar to serve_chat for other endpoints.

Remember to review Anthropic's API documentation for the latest endpoints, request formats, and best practices.

@ohdearquant ohdearquant merged commit 70e20c0 into lion-agi:main Feb 6, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants