|
# retoor <retoor@molodetz.nl>
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from devplacepy import stealth
|
|
from devplacepy.config import INTERNAL_GATEWAY_URL, INTERNAL_MODEL
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
CHAT_TIMEOUT_SECONDS = 120.0
|
|
DEFAULT_MAX_TOKENS = 1200
|
|
|
|
|
|
async def complete_chat(
|
|
messages: list[dict],
|
|
api_key: str,
|
|
*,
|
|
gateway_url: str = INTERNAL_GATEWAY_URL,
|
|
model: str = INTERNAL_MODEL,
|
|
max_tokens: int = DEFAULT_MAX_TOKENS,
|
|
temperature: float = 0.2,
|
|
) -> str:
|
|
payload = {
|
|
"model": model,
|
|
"messages": messages,
|
|
"max_tokens": max_tokens,
|
|
"temperature": temperature,
|
|
}
|
|
headers = {
|
|
"Authorization": f"Bearer {api_key}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
async with stealth.stealth_async_client(timeout=CHAT_TIMEOUT_SECONDS) as client:
|
|
response = await client.post(gateway_url, json=payload, headers=headers)
|
|
if response.status_code >= 400:
|
|
raise RuntimeError(f"chat gateway returned {response.status_code}")
|
|
data = response.json()
|
|
choices = data.get("choices") or []
|
|
if not choices:
|
|
raise RuntimeError("chat gateway returned no choices")
|
|
return (choices[0].get("message", {}).get("content") or "").strip()
|