mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
31 lines
775 B
Python
31 lines
775 B
Python
"""OpenAI client infrastructure"""
|
|
from openai import AsyncOpenAI
|
|
from config import settings
|
|
|
|
# Global OpenAI client instance
|
|
_openai_client: AsyncOpenAI | None = None
|
|
|
|
|
|
async def init_openai():
|
|
"""Initialize OpenAI client"""
|
|
global _openai_client
|
|
_openai_client = AsyncOpenAI(
|
|
api_key=settings.OPENROUTER_API_KEY,
|
|
base_url=settings.OPENROUTER_BASE_URL
|
|
)
|
|
|
|
|
|
async def close_openai():
|
|
"""Close OpenAI client"""
|
|
global _openai_client
|
|
if _openai_client:
|
|
await _openai_client.close()
|
|
_openai_client = None
|
|
|
|
|
|
def get_openai() -> AsyncOpenAI:
|
|
"""Get OpenAI client instance (dependency injection)"""
|
|
if _openai_client is None:
|
|
raise RuntimeError("OpenAI client not initialized")
|
|
return _openai_client
|
|
|