from openai import AsyncOpenAI from app.config import OPENAI_API_KEY, OPENAI_MODEL class OpenAIClient: _client = None def __init__(self): self.api_key = OPENAI_API_KEY self.model = OPENAI_MODEL if OpenAIClient._client is None and self.api_key: OpenAIClient._client = AsyncOpenAI( api_key=self.api_key, timeout=12.0, max_retries=1, ) async def generate_response( self, system_prompt: str, user_message: str, ) -> str: client = OpenAIClient._client or AsyncOpenAI( api_key=self.api_key or "sk-dummy-key-placeholder", timeout=12.0, ) response = await client.chat.completions.create( model=self.model or "gpt-4o-mini", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_message}, ], response_format={"type": "json_object"}, temperature=0.35, max_completion_tokens=140, ) return response.choices[0].message.content