Spaces:
Running
Running
import anthropic | |
from .base_api import BaseAPI | |
class AnthropicAPI(BaseAPI): | |
"""Anthropic API implementation""" | |
def __init__(self, api_key: str, model_name: str, **kwargs): | |
super().__init__(api_key, model_name, **kwargs) | |
self.client = anthropic.AsyncAnthropic(api_key=api_key) | |
async def generate_response(self, prompt: str, **kwargs) -> str: | |
"""Generate response using Anthropic API""" | |
try: | |
response = await self.client.messages.create( | |
model=self.model_name, | |
max_tokens=kwargs.get('max_tokens', 2048), | |
temperature=kwargs.get('temperature', 0.0), | |
messages=[{"role": "user", "content": prompt}] | |
) | |
return response.content[0].text | |
except Exception as e: | |
raise Exception(f"Anthropic API error: {str(e)}") | |
def get_model_info(self) -> dict: | |
"""Get model information""" | |
return { | |
"provider": "Anthropic", | |
"model": self.model_name, | |
"api_version": "2023-06-01" | |
} |