rmi-backend/app/core/cerebras_provider.py
opencode c762564d40 style(rmi-backend): complete lint cleanup — 1175→0 ruff errors
- Fix 71 invalid-syntax files (class-body newline-broken assignments)
- Add from/None chain to 307 B904 raise-without-from sites
- Add B008 ignore to ruff.toml (already in pyproject.toml)
- Noqa F401 on __init__.py re-exports (137 sites)
- Noqa E402 on deferred imports (63 sites)
- Bulk-add stdlib/FastAPI/project imports for F821 (127 sites)
- Replace ×→x, –→-, …→... in docstrings (4093 chars)
- Manual refactor of 5 SIM103/SIM116 patterns

Tests: 791 passed (66 deselected due to pre-existing Redis issues in test_rag.py)
Co-authored-by: opencode <opencode@rugmunch.io>
2026-07-06 15:43:20 +02:00

49 lines
1.7 KiB
Python

"""Cerebras provider - GPT-OSS-120B, fastest inference on Earth (9ms).
Free tier: 14,400 req/day, 1M tokens/day."""
import logging
import os
import httpx
logger = logging.getLogger(__name__)
CEREBRAS_KEY = os.getenv("CEREBRAS_API_KEY", "")
BASE = "https://api.cerebras.ai/v1"
async def cerebras_chat(
prompt: str, system: str | None = None, temperature: float = 0.7, max_tokens: int = 1024
) -> dict | None:
"""GPT-OSS-120B via Cerebras - 9ms latency. Use for real-time, latency-sensitive tasks."""
if not CEREBRAS_KEY:
return None
messages = []
if system:
messages.append({"role": "system", "content": system})
messages.append({"role": "user", "content": prompt})
try:
async with httpx.AsyncClient(timeout=15) as c:
r = await c.post(
f"{BASE}/chat/completions",
json={
"model": "gpt-oss-120b",
"messages": messages,
"max_tokens": max_tokens,
"temperature": temperature,
},
headers={"Authorization": f"Bearer {CEREBRAS_KEY}"},
)
if r.status_code == 200:
d = r.json()
choice = d["choices"][0]["message"]
content = choice.get("content") or choice.get("reasoning", "")
return {
"response": content.strip(),
"model": "gpt-oss-120b",
"tokens": d.get("usage", {}).get("total_tokens", 0),
"latency_ms": round(d.get("time_info", {}).get("total_time", 0) * 1000),
"provider": "cerebras",
}
except Exception as e:
logger.warning(f"Cerebras failed: {e}")
return None