Spaces:
Sleeping
Sleeping
| import openai | |
| from .base_api import BaseAPI | |
| import asyncio | |
| class OpenAIAPI(BaseAPI): | |
| """OpenAI API implementation""" | |
| def __init__(self, api_key: str, model_name: str, **kwargs): | |
| super().__init__(api_key, model_name, **kwargs) | |
| self.client = openai.AsyncOpenAI(api_key=api_key) | |
| async def generate_response(self, prompt: str, **kwargs) -> str: | |
| """Generate response using OpenAI API""" | |
| try: | |
| response = await self.client.chat.completions.create( | |
| model=self.model_name, | |
| messages=[{"role": "user", "content": prompt}], | |
| temperature=kwargs.get('temperature', 0.0), | |
| max_tokens=kwargs.get('max_tokens', 2048), | |
| timeout=self.timeout | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| raise Exception(f"OpenAI API error: {str(e)}") | |
| def get_model_info(self) -> dict: | |
| """Get model information""" | |
| return { | |
| "provider": "OpenAI", | |
| "model": self.model_name, | |
| "api_version": "v1" | |
| } |