Files
semif-agent/semif_agent/llm.py
T
Denton Social 3d0cff325c assessment: make assess/fidelity SemIf decisions, drop LLM from decision path
Assessment was an LLM generation step emitting prose. Move it to SemIf:
- assess:outcome (success/failure @ tau) and assess:requeue (complete/retry,
  updated_request = request.text bounded by max_reentries) are logged decisions.
- Run summary is deterministic (category.skill + ok/failed + action_log/new_state).
- Fidelity gate is a real authoring:fidelity accept/reconsider decision plus
  static integration findings; it only triggers a rewrite, never a diagnosis.
- Corrective regens receive the raw evidence bundle as-is (render_evidence),
  never an LLM paraphrase.
- Every EngineUnavailable is fatal app-wide: Scheduler.fatal is recorded, the
  worker stops, and the CLI prints 'decision engine not available' and exits
  non-zero. Remove llm.assess/review_skill_body (keep _parse_json).
- Tests: ScriptedEngine double in tests/conftest.py for scheduling mechanics;
  replace the review_skill_body integration test with a SemIf fidelity test;
  add deterministic-summary and evidence-renderer unit tests.
2026-09-28 17:49:26 -05:00

47 lines
1.7 KiB
Python

"""OpenAI-compatible LLM client — retained but dormant.
Decision-making (assessment, requeue, fidelity) now belongs to SemIf; this
client no longer participates in any decision path. The `llm` endpoint stays
configured for now (retirement is backlogged), so the class remains as the
stdlib transport plus `_parse_json`, which the authoring parsers borrow. Uses
only the stdlib HTTP client so the core stays dependency-free.
"""
from __future__ import annotations
import json
import urllib.error
import urllib.request
class LLMClient:
def __init__(self, base_url: str, model: str, timeout: float = 120.0):
self.base_url = base_url.rstrip("/")
self.model = model
self.timeout = timeout
def _chat(self, messages: list[dict], temperature: float = 0.0) -> str:
url = f"{self.base_url}/chat/completions"
body = json.dumps(
{"model": self.model, "messages": messages, "temperature": temperature}
).encode("utf-8")
request = urllib.request.Request(
url, data=body, headers={"Content-Type": "application/json"}
)
try:
with urllib.request.urlopen(request, timeout=self.timeout) as response:
payload = json.loads(response.read().decode("utf-8"))
except urllib.error.URLError as exc:
raise RuntimeError(
f"LLM endpoint unreachable at {url}: {exc}. Is your local server running?"
) from exc
return payload["choices"][0]["message"]["content"]
@staticmethod
def _parse_json(raw: str) -> dict:
text = raw.strip()
start, end = text.find("{"), text.rfind("}")
if start != -1 and end != -1:
text = text[start : end + 1]
return json.loads(text)