Spaces:
Running
Running
File size: 4,717 Bytes
ad8bc9a 45b7965 ad8bc9a 45b7965 ad8bc9a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | from __future__ import annotations
import asyncio
from dataclasses import dataclass
from typing import Any
import httpx
from app.config import Settings
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
FALLBACK_STATUS_CODES = {400, 404, 408, 409, 422, 429}
class OpenRouterNotConfigured(Exception):
pass
class OpenRouterUnavailable(Exception):
pass
@dataclass(frozen=True, slots=True)
class Completion:
content: str
model: str
class OpenRouterClient:
def __init__(
self,
settings: Settings,
transport: httpx.AsyncBaseTransport | None = None,
) -> None:
self.settings = settings
self._client = httpx.AsyncClient(
transport=transport,
timeout=httpx.Timeout(settings.upstream_timeout_seconds),
follow_redirects=False,
)
async def close(self) -> None:
await self._client.aclose()
async def complete(self, messages: list[dict[str, str]]) -> Completion:
if not self.settings.openrouter_api_key:
raise OpenRouterNotConfigured
headers = {
"Authorization": f"Bearer {self.settings.openrouter_api_key}",
"Content-Type": "application/json",
"HTTP-Referer": self.settings.openrouter_site_url,
"X-Title": "Fuugi Companion API",
}
try:
async with asyncio.timeout(self.settings.upstream_timeout_seconds):
for model in self.settings.models:
try:
payload: dict[str, Any] = {
"model": model,
"messages": messages,
"temperature": 0.4,
"max_tokens": self.settings.max_output_tokens,
"reasoning": {"enabled": True, "exclude": True},
}
if self.settings.web_search_enabled:
payload["tools"] = [
{
"type": "openrouter:web_search",
"parameters": {
"engine": self.settings.web_search_engine,
"mode": "auto",
"max_results": self.settings.web_search_max_results,
"max_uses": self.settings.web_search_max_uses,
"max_total_results": self.settings.web_search_max_results,
"max_characters": self.settings.web_search_max_characters,
},
}
]
payload["max_tool_calls"] = self.settings.web_search_max_uses + 1
response = await self._client.post(
OPENROUTER_URL,
headers=headers,
json=payload,
)
except httpx.RequestError:
continue
if response.status_code in {401, 403}:
raise OpenRouterUnavailable
if response.status_code >= 500 or response.status_code in FALLBACK_STATUS_CODES:
continue
if not 200 <= response.status_code < 300:
continue
completion = self._parse(response, model)
if completion is not None:
return completion
except TimeoutError as error:
raise OpenRouterUnavailable from error
raise OpenRouterUnavailable
def _parse(self, response: httpx.Response, requested_model: str) -> Completion | None:
try:
data: dict[str, Any] = response.json()
content: Any = data["choices"][0]["message"]["content"]
except (KeyError, IndexError, TypeError, ValueError):
return None
if isinstance(content, list):
content = "".join(
str(part.get("text", ""))
for part in content
if isinstance(part, dict) and part.get("type") in {None, "text"}
)
if not isinstance(content, str) or not content.strip():
return None
model = data.get("model")
if not isinstance(model, str) or not model.strip():
model = requested_model
return Completion(
content=content.strip()[: self.settings.max_output_characters],
model=model.strip()[:200],
)
|