MVP 1.0
This commit is contained in:
1 parent
be11f82f84
commit
e42f751fdc
47 files changed
+3359
-301
No files matched your search
+73
-3
@@ -1,6 +1,7 @@
|
||||
"""Two independently configured OpenAI-compatible providers: generate vs detect."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
@@ -28,6 +29,53 @@ class ProviderError(Exception):
|
||||
self.diagnostics = diagnostics or {}
|
||||
|
||||
|
||||
PROVIDER_MESSAGE_MAX = 240
|
||||
_PROVIDER_USAGE_KEYS = ("prompt_tokens", "completion_tokens", "total_tokens", "cost", "total_cost")
|
||||
|
||||
|
||||
def provider_error_diagnostics(status_code: int, body: str, *, extra: dict | None = None) -> dict:
|
||||
"""Compact provider failure fields. No prompt body, no mapping labels."""
|
||||
diagnostics: dict = {"http_status": status_code}
|
||||
if extra:
|
||||
diagnostics.update({key: value for key, value in extra.items() if value is not None})
|
||||
parsed = None
|
||||
try:
|
||||
parsed = json.loads(body or "")
|
||||
except (TypeError, ValueError, json.JSONDecodeError):
|
||||
parsed = None
|
||||
if isinstance(parsed, dict):
|
||||
err = parsed.get("error")
|
||||
if isinstance(err, str) and err.strip():
|
||||
diagnostics["provider_message"] = err.strip()[:PROVIDER_MESSAGE_MAX]
|
||||
elif isinstance(err, dict):
|
||||
msg = str(err.get("message") or err.get("msg") or "").strip()
|
||||
if msg:
|
||||
diagnostics["provider_message"] = msg[:PROVIDER_MESSAGE_MAX]
|
||||
code = err.get("code") or err.get("type")
|
||||
if code:
|
||||
diagnostics["provider_code"] = str(code)[:80]
|
||||
usage = parsed.get("usage")
|
||||
if isinstance(usage, dict):
|
||||
kept = {key: usage[key] for key in _PROVIDER_USAGE_KEYS if usage.get(key) is not None}
|
||||
if kept:
|
||||
diagnostics["usage"] = kept
|
||||
if kept.get("cost") is not None:
|
||||
diagnostics["cost"] = kept["cost"]
|
||||
elif kept.get("total_cost") is not None:
|
||||
diagnostics["cost"] = kept["total_cost"]
|
||||
return diagnostics
|
||||
|
||||
|
||||
def _reject_message(role: str, status_code: int, diagnostics: dict) -> str:
|
||||
base = f"{role}-Provider hat die Anfrage abgelehnt"
|
||||
if status_code:
|
||||
base += f" (HTTP {status_code})"
|
||||
snippet = (diagnostics.get("provider_message") or "").strip()
|
||||
if snippet:
|
||||
return f"{base}: {snippet}"
|
||||
return f"{base}."
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProviderConfig:
|
||||
role: str
|
||||
@@ -193,23 +241,45 @@ def complete_chat(
|
||||
headers["X-Title"] = "Kansho"
|
||||
try:
|
||||
response = httpx.post(config.url, json=payload, headers=headers, timeout=timeout)
|
||||
except httpx.TimeoutException as exc:
|
||||
raise ProviderError(
|
||||
"provider_timeout",
|
||||
f"{config.role}-Provider hat nicht innerhalb von {int(timeout)} Sekunden geantwortet.",
|
||||
504,
|
||||
diagnostics={"timeout_s": timeout, "generate_requested": True},
|
||||
) from exc
|
||||
except httpx.HTTPError as exc:
|
||||
raise ProviderError("provider_unreachable", f"{config.role}-Provider ist nicht erreichbar.") from exc
|
||||
raise ProviderError(
|
||||
"provider_unreachable",
|
||||
f"{config.role}-Provider ist nicht erreichbar.",
|
||||
503,
|
||||
diagnostics={"generate_requested": True},
|
||||
) from exc
|
||||
if response.status_code >= 400:
|
||||
body = ""
|
||||
try:
|
||||
body = response.text or ""
|
||||
except Exception:
|
||||
body = ""
|
||||
diagnostics = provider_error_diagnostics(
|
||||
response.status_code,
|
||||
body,
|
||||
extra={"context_compression": compression, "generate_requested": True},
|
||||
)
|
||||
if CONTEXT_LENGTH_REJECT.search(body):
|
||||
raise ProviderError(
|
||||
"provider_context_length_rejected",
|
||||
"Der Anbieter hat die Anfrage wegen der Kontextlänge abgelehnt. "
|
||||
"Kanshō hat den Tagesdialog nicht automatisch gekürzt.",
|
||||
502,
|
||||
diagnostics={"http_status": response.status_code, "context_compression": compression},
|
||||
diagnostics=diagnostics,
|
||||
)
|
||||
raise ProviderError("provider_rejected", f"{config.role}-Provider hat die Anfrage abgelehnt.")
|
||||
raise ProviderError(
|
||||
"provider_rejected",
|
||||
_reject_message(config.role, response.status_code, diagnostics),
|
||||
502,
|
||||
diagnostics=diagnostics,
|
||||
)
|
||||
data = response.json()
|
||||
try:
|
||||
content = data["choices"][0]["message"]["content"]
|
||||
|
||||
Reference in new issue
Block a user