2026-05-28 19:02:16 +08:00

157 lines
5.7 KiB
Python

from __future__ import annotations
import json
import os
import urllib.error
import urllib.request
from dataclasses import dataclass
from typing import Any
@dataclass(frozen=True)
class LlmSettings:
base_url: str
model: str
api_key: str
api_style: str = "anthropic"
max_tokens: int = 900
@dataclass(frozen=True)
class LlmPageSynthesis:
summary: str
answer_blueprint: tuple[str, ...]
key_evidence: tuple[str, ...]
confidence: str = "medium"
class LlmPageSynthesizer:
def __init__(self, settings: LlmSettings) -> None:
self.settings = settings
@classmethod
def from_environment(cls) -> "LlmPageSynthesizer | None":
base_url = os.environ.get("D_WIKI_LLM_BASE_URL") or os.environ.get("ROBOGO_LLM_BASE_URL")
model = os.environ.get("D_WIKI_LLM_MODEL") or os.environ.get("ROBOGO_LLM_MODEL")
api_key = os.environ.get("D_WIKI_LLM_API_KEY") or os.environ.get("ROBOGO_LLM_API_KEY")
if not base_url or not model or not api_key:
return None
api_style = os.environ.get("D_WIKI_LLM_API_STYLE") or os.environ.get("ROBOGO_LLM_API_STYLE") or "anthropic"
max_tokens = int(os.environ.get("D_WIKI_LLM_MAX_TOKENS", "900"))
return cls(LlmSettings(base_url=base_url, model=model, api_key=api_key, api_style=api_style, max_tokens=max_tokens))
def synthesize(self, prompt: str) -> LlmPageSynthesis | None:
text = self._complete(prompt).strip()
payload = _extract_json(text)
if not isinstance(payload, dict):
return None
summary = str(payload.get("summary") or "").strip()
if not summary:
return None
answer_blueprint = _string_tuple(payload.get("answer_blueprint"))
key_evidence = _string_tuple(payload.get("key_evidence"))
confidence = str(payload.get("confidence") or "medium").strip().lower()
if confidence not in {"low", "medium", "high"}:
confidence = "medium"
return LlmPageSynthesis(
summary=summary,
answer_blueprint=answer_blueprint,
key_evidence=key_evidence,
confidence=confidence,
)
def _complete(self, prompt: str) -> str:
if self.settings.api_style == "openai":
return self._complete_openai(prompt)
return self._complete_anthropic(prompt)
def _complete_anthropic(self, prompt: str) -> str:
payload = {
"model": self.settings.model,
"max_tokens": self.settings.max_tokens,
"temperature": 0,
"system": "You compile local documentation into concise, evidence-grounded wiki pages. Return only JSON.",
"messages": [{"role": "user", "content": prompt}],
}
data = self._post_json(_join_url(self.settings.base_url, "v1/messages"), payload)
content = data.get("content")
if isinstance(content, list):
return "\n".join(str(item.get("text", "")) for item in content if isinstance(item, dict))
return str(data.get("text") or "")
def _complete_openai(self, prompt: str) -> str:
payload = {
"model": self.settings.model,
"temperature": 0,
"messages": [
{
"role": "system",
"content": "You compile local documentation into concise, evidence-grounded wiki pages. Return only JSON.",
},
{"role": "user", "content": prompt},
],
}
data = self._post_json(_join_url(self.settings.base_url, "v1/chat/completions"), payload)
choices = data.get("choices")
if isinstance(choices, list) and choices:
first = choices[0]
if isinstance(first, dict):
message = first.get("message")
if isinstance(message, dict):
return str(message.get("content") or "")
return ""
def _post_json(self, url: str, payload: dict[str, Any]) -> dict[str, Any]:
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
request = urllib.request.Request(
url,
data=body,
method="POST",
headers={
"content-type": "application/json",
"accept": "application/json",
"x-api-key": self.settings.api_key,
"authorization": f"Bearer {self.settings.api_key}",
"anthropic-version": "2023-06-01",
},
)
try:
with urllib.request.urlopen(request, timeout=60) as response:
return json.loads(response.read().decode("utf-8"))
except urllib.error.HTTPError as exc:
detail = exc.read().decode("utf-8", errors="ignore")
raise RuntimeError(f"D_WIKI_LLM_HTTP_ERROR: {exc.code} {detail[:300]}") from exc
def _join_url(base_url: str, suffix: str) -> str:
base = base_url.rstrip("/")
if base.endswith("/v1/messages") or base.endswith("/v1/chat/completions"):
return base
if base.endswith("/v1"):
return f"{base}/{suffix.removeprefix('v1/')}"
return f"{base}/{suffix}"
def _extract_json(text: str) -> Any:
cleaned = text.strip()
if cleaned.startswith("```"):
cleaned = cleaned.strip("`")
if cleaned.lower().startswith("json"):
cleaned = cleaned[4:].strip()
decoder = json.JSONDecoder()
for index, char in enumerate(cleaned):
if char != "{":
continue
try:
value, _end = decoder.raw_decode(cleaned[index:])
except json.JSONDecodeError:
continue
return value
return None
def _string_tuple(value: Any) -> tuple[str, ...]:
if not isinstance(value, list):
return ()
return tuple(str(item).strip() for item in value if str(item).strip())