Spaces:
Running
Running
Download app/openrouter.py from 2Btainment/Fuugi-API: direct link, hf CLI and curl.
- Browser
- Download file 4.72 kB
-
https://huggingface.co/spaces/2Btainment/Fuugi-API/resolve/main/app/openrouter.py
- Command line
-
hf download hf://spaces/2Btainment/Fuugi-API/app/openrouter.py
-
curl -L -o openrouter.py https://huggingface.co/spaces/2Btainment/Fuugi-API/resolve/main/app/openrouter.py
4.72 kB
| 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 | |
| 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], | |
| ) | |