- 4 provider adapters: OpenAI (SDK), Anthropic (SDK), Google (google-genai), Mistral (direct HTTP) - Core router with automatic failover + exponential backoff - Flask blueprint with /api/v1/ai/* endpoints - Auth via token-broker verify endpoint - DB models for ai_providers, ai_model_mapping, ai_router_log - /health endpoint (parallel provider check), /usage stats - 21 unit tests (all passing)
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import logging
|
|
from typing import Optional
|
|
|
|
from .base import AIProvider
|
|
|
|
logger = logging.getLogger("ai_router.openai")
|
|
|
|
|
|
class OpenAIAdapter(AIProvider):
|
|
@property
|
|
def name(self) -> str:
|
|
return "openai"
|
|
|
|
def chat(self, messages: list, model: str, api_key: Optional[str] = None, **kwargs) -> dict:
|
|
from openai import OpenAI
|
|
|
|
key = api_key or self.get_api_key()
|
|
client = OpenAI(api_key=key)
|
|
resp = client.chat.completions.create(
|
|
model=model,
|
|
messages=messages,
|
|
**{k: v for k, v in kwargs.items() if k in ("temperature", "max_tokens", "top_p", "stream")},
|
|
)
|
|
choice = resp.choices[0]
|
|
return {
|
|
"content": choice.message.content or "",
|
|
"model": resp.model,
|
|
"provider": self.name,
|
|
"usage": {
|
|
"prompt_tokens": resp.usage.prompt_tokens if resp.usage else 0,
|
|
"completion_tokens": resp.usage.completion_tokens if resp.usage else 0,
|
|
"total_tokens": resp.usage.total_tokens if resp.usage else 0,
|
|
},
|
|
}
|
|
|
|
def models(self) -> list:
|
|
from openai import OpenAI
|
|
|
|
client = OpenAI(api_key=self.get_api_key())
|
|
return [m.id for m in client.models.list()]
|
|
|
|
def check_health(self) -> dict:
|
|
try:
|
|
from openai import OpenAI
|
|
client = OpenAI(api_key=self.get_api_key())
|
|
client.models.list()
|
|
return {"status": "ok", "details": "API reachable"}
|
|
except Exception as e:
|
|
logger.warning(f"OpenAI health check failed: {e}")
|
|
return {"status": "error", "details": str(e)}
|