David Prince
production: clean source snapshot — no history bloat
71b4454
Raw
History Blame Contribute Delete
1.88 kB
import os
import httpx
from typing import Dict, Any
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "zai-glm-4.7")
CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY", "").strip()
CEREBRAS_URL = "https://api.cerebras.ai/v1/chat/completions"
class ModelGateway:
def __init__(self):
self.client = httpx.AsyncClient(
timeout=httpx.Timeout(180.0),
follow_redirects=True,
)
async def generate(
self,
model: str,
prompt: str,
system: str = "You are DOLOR3V Autonomous Builder.",
) -> Dict[str, Any]:
if not CEREBRAS_API_KEY:
raise RuntimeError("CEREBRAS_API_KEY is not configured")
payload = {
"model": model,
"messages": [
{
"role": "system",
"content": system,
},
{
"role": "user",
"content": prompt,
},
],
"temperature": 0.2,
"max_tokens": 4096,
}
response = await self.client.post(
CEREBRAS_URL,
headers={
"Authorization": f"Bearer {CEREBRAS_API_KEY}",
"Content-Type": "application/json",
},
json=payload,
)
response.raise_for_status()
data = response.json()
if "choices" not in data:
raise RuntimeError(f"Unexpected Cerebras response: {data}")
text = (
data["choices"][0]
.get("message", {})
.get("content", "")
.strip()
)
return {
"text": text,
"provider": "cerebras",
"model": model,
"raw": data,
}
async def close(self):
await self.client.aclose()
model_gateway = ModelGateway()