035fe6d658
Addresses issues found while reviewing the initial phase-2 implementation against real data: **Bug: uncertain bucket starved from the LLM.** `discover_entities` was dropping the regex-uncertain bucket whenever real git/manifest signal existed — which is exactly when `--llm` is most useful for cleaning up prose noise. The uncertain candidates never reached the refinement step. Fixed: only drop when `llm_provider is None`. **Context collection: word boundaries, not substring.** `_collect_contexts` used substring matching on lower-cased lines, so the name "Go" matched "good", "going", "forgot". Switched to a `(?<!\w)…(?!\w)` regex so short names only match at token boundaries. **Authoritative-source detection replaces confidence threshold.** Previously the refinement step skipped entries with `confidence >= 0.95` to avoid second-guessing manifest-backed projects. That threshold was fragile — the regex detector produces 0.99 confidence for things like `code file reference (5x)` on framework names (OpenAPI, etc.), so those skipped the LLM despite being regex-only noise. New helpers `_is_authoritative_person` / `_is_authoritative_project` look at the actual signal strings (commits, package.json, etc.) to decide. **Now also refines regex-derived people.** After #1148's high-pronoun-signal fix, the regex detector can promote non-people to the `people` bucket (e.g. a capitalized common noun that happened to appear near pronouns). The LLM now gets a chance to clean those up, while git-authored people are still skipped. **Robust JSON extraction.** Small local models routinely wrap JSON output in prose ("Sure, here's the classification: {…}"). The previous code-fence stripper failed on that. `_extract_json_candidates` now does balanced-bracket extraction with string-aware quote handling, so it recovers JSON from: - raw responses - markdown fenced blocks - JSON embedded inside surrounding text - multiple candidate objects/arrays **Prompt guidance for frameworks vs user projects.** Added an explicit instruction: frameworks, runtimes, APIs, cloud services, and third-party vendors (Angular, OpenAPI, Terraform, Bun, Google, etc.) are TOPIC unless the context clearly says it's the user's own codebase. Directly addresses a false-positive pattern observed during dev runs. **Defensive mtime.** `convo_scanner._safe_mtime` catches OSError during `stat()` — permission changes, filesystem races, broken symlinks — and sorts the affected file to the end of the newest-first order rather than crashing the scan. **Cosmetic:** merged two adjacent f-strings on the same line in `backends/chroma.py` and `llm_client.py` (no behaviour change). 15 new tests cover the OSError fallback, word-boundary matching, JSON extraction variants, authoritative-source helpers, refining high- confidence regex projects, and end-to-end LLM refinement preserving the uncertain bucket.
306 lines
10 KiB
Python
306 lines
10 KiB
Python
"""
|
|
llm_client.py — Minimal provider abstraction for LLM-assisted entity refinement.
|
|
|
|
Three providers cover the useful space:
|
|
|
|
- ``ollama`` (default): local models via http://localhost:11434. Works fully
|
|
offline. Honors MemPalace's "zero-API required" principle.
|
|
- ``openai-compat``: any OpenAI-compatible ``/v1/chat/completions`` endpoint.
|
|
Covers OpenRouter, LM Studio, llama.cpp server, vLLM, Groq, Fireworks,
|
|
Together, and most self-hosted setups.
|
|
- ``anthropic``: the official Messages API. Opt-in for users who want Haiku
|
|
quality without setting up a local model.
|
|
|
|
All providers expose the same ``classify(system, user, json_mode)`` method and
|
|
the same ``check_available()`` probe. No external SDK dependencies — stdlib
|
|
``urllib`` only.
|
|
|
|
JSON mode matters here: we always ask for structured output. Providers
|
|
differ on how to request it (Ollama: ``format: json``; OpenAI-compat:
|
|
``response_format``; Anthropic: prompt-level instruction) and this module
|
|
normalizes that away from the caller.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from dataclasses import dataclass
|
|
from typing import Optional
|
|
from urllib.error import HTTPError, URLError
|
|
from urllib.request import Request, urlopen
|
|
|
|
|
|
class LLMError(RuntimeError):
|
|
"""Raised for any provider failure — transport, parse, auth, missing model."""
|
|
|
|
|
|
@dataclass
|
|
class LLMResponse:
|
|
text: str
|
|
model: str
|
|
provider: str
|
|
raw: dict
|
|
|
|
|
|
# ==================== BASE ====================
|
|
|
|
|
|
class LLMProvider:
|
|
name: str = "base"
|
|
|
|
def __init__(
|
|
self,
|
|
model: str,
|
|
endpoint: Optional[str] = None,
|
|
api_key: Optional[str] = None,
|
|
timeout: int = 120,
|
|
):
|
|
self.model = model
|
|
self.endpoint = endpoint
|
|
self.api_key = api_key
|
|
self.timeout = timeout
|
|
|
|
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
|
|
raise NotImplementedError
|
|
|
|
def check_available(self) -> tuple[bool, str]:
|
|
"""Return ``(ok, message)``. Fast probe that the provider is reachable."""
|
|
raise NotImplementedError
|
|
|
|
|
|
def _http_post_json(url: str, body: dict, headers: dict, timeout: int) -> dict:
|
|
"""POST JSON and return the parsed response. Raises LLMError on any failure."""
|
|
req = Request(
|
|
url,
|
|
data=json.dumps(body).encode("utf-8"),
|
|
headers={"Content-Type": "application/json", **headers},
|
|
)
|
|
try:
|
|
with urlopen(req, timeout=timeout) as resp:
|
|
return json.loads(resp.read())
|
|
except HTTPError as e:
|
|
detail = ""
|
|
try:
|
|
detail = e.read().decode("utf-8", errors="replace")[:500]
|
|
except Exception:
|
|
pass
|
|
raise LLMError(f"HTTP {e.code} from {url}: {detail or e.reason}") from e
|
|
except (URLError, OSError) as e:
|
|
raise LLMError(f"Cannot reach {url}: {e}") from e
|
|
except json.JSONDecodeError as e:
|
|
raise LLMError(f"Malformed response from {url}: {e}") from e
|
|
|
|
|
|
# ==================== OLLAMA ====================
|
|
|
|
|
|
class OllamaProvider(LLMProvider):
|
|
name = "ollama"
|
|
DEFAULT_ENDPOINT = "http://localhost:11434"
|
|
|
|
def __init__(
|
|
self,
|
|
model: str,
|
|
endpoint: Optional[str] = None,
|
|
timeout: int = 180,
|
|
**_: object,
|
|
):
|
|
super().__init__(
|
|
model=model,
|
|
endpoint=endpoint or self.DEFAULT_ENDPOINT,
|
|
timeout=timeout,
|
|
)
|
|
|
|
def check_available(self) -> tuple[bool, str]:
|
|
try:
|
|
with urlopen(f"{self.endpoint}/api/tags", timeout=5) as resp:
|
|
data = json.loads(resp.read())
|
|
except (URLError, HTTPError, OSError, json.JSONDecodeError) as e:
|
|
return False, f"Cannot reach Ollama at {self.endpoint}: {e}"
|
|
names = {m.get("name", "") for m in data.get("models", []) or []}
|
|
# Ollama tags may or may not include ':latest' — accept either form
|
|
wanted = {self.model, f"{self.model}:latest"}
|
|
if not names & wanted:
|
|
return (
|
|
False,
|
|
f"Model '{self.model}' not loaded in Ollama. Run: ollama pull {self.model}",
|
|
)
|
|
return True, "ok"
|
|
|
|
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
|
|
body: dict = {
|
|
"model": self.model,
|
|
"messages": [
|
|
{"role": "system", "content": system},
|
|
{"role": "user", "content": user},
|
|
],
|
|
"stream": False,
|
|
"options": {"temperature": 0.1},
|
|
}
|
|
if json_mode:
|
|
body["format"] = "json"
|
|
data = _http_post_json(f"{self.endpoint}/api/chat", body, headers={}, timeout=self.timeout)
|
|
text = (data.get("message") or {}).get("content", "")
|
|
if not text:
|
|
raise LLMError(f"Empty response from Ollama (model={self.model})")
|
|
return LLMResponse(text=text, model=self.model, provider=self.name, raw=data)
|
|
|
|
|
|
# ==================== OPENAI-COMPAT ====================
|
|
|
|
|
|
class OpenAICompatProvider(LLMProvider):
|
|
"""Any OpenAI-compatible ``/v1/chat/completions`` endpoint.
|
|
|
|
Supply ``--llm-endpoint http://host:port`` (with or without ``/v1``).
|
|
API key via ``--llm-api-key`` or the ``OPENAI_API_KEY`` env var.
|
|
"""
|
|
|
|
name = "openai-compat"
|
|
|
|
def __init__(
|
|
self,
|
|
model: str,
|
|
endpoint: Optional[str] = None,
|
|
api_key: Optional[str] = None,
|
|
timeout: int = 120,
|
|
**_: object,
|
|
):
|
|
resolved_key = api_key or os.environ.get("OPENAI_API_KEY")
|
|
super().__init__(model=model, endpoint=endpoint, api_key=resolved_key, timeout=timeout)
|
|
|
|
def _resolve_url(self) -> str:
|
|
if not self.endpoint:
|
|
raise LLMError("openai-compat provider requires --llm-endpoint")
|
|
url = self.endpoint.rstrip("/")
|
|
if url.endswith("/chat/completions"):
|
|
return url
|
|
if not url.endswith("/v1"):
|
|
url = f"{url}/v1"
|
|
return f"{url}/chat/completions"
|
|
|
|
def check_available(self) -> tuple[bool, str]:
|
|
if not self.endpoint:
|
|
return False, "no --llm-endpoint configured"
|
|
base = self.endpoint.rstrip("/")
|
|
base = base.removesuffix("/chat/completions").removesuffix("/v1")
|
|
try:
|
|
req = Request(f"{base}/v1/models")
|
|
if self.api_key:
|
|
req.add_header("Authorization", f"Bearer {self.api_key}")
|
|
with urlopen(req, timeout=5):
|
|
pass
|
|
except (URLError, HTTPError, OSError) as e:
|
|
return False, f"Cannot reach {self.endpoint}: {e}"
|
|
return True, "ok"
|
|
|
|
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
|
|
body: dict = {
|
|
"model": self.model,
|
|
"messages": [
|
|
{"role": "system", "content": system},
|
|
{"role": "user", "content": user},
|
|
],
|
|
"temperature": 0.1,
|
|
}
|
|
if json_mode:
|
|
body["response_format"] = {"type": "json_object"}
|
|
headers = {}
|
|
if self.api_key:
|
|
headers["Authorization"] = f"Bearer {self.api_key}"
|
|
data = _http_post_json(self._resolve_url(), body, headers=headers, timeout=self.timeout)
|
|
try:
|
|
text = data["choices"][0]["message"]["content"]
|
|
except (KeyError, IndexError, TypeError) as e:
|
|
raise LLMError(f"Unexpected response shape: {e}") from e
|
|
if not text:
|
|
raise LLMError(f"Empty response from {self.name} (model={self.model})")
|
|
return LLMResponse(text=text, model=self.model, provider=self.name, raw=data)
|
|
|
|
|
|
# ==================== ANTHROPIC ====================
|
|
|
|
|
|
class AnthropicProvider(LLMProvider):
|
|
name = "anthropic"
|
|
DEFAULT_ENDPOINT = "https://api.anthropic.com"
|
|
API_VERSION = "2023-06-01"
|
|
|
|
def __init__(
|
|
self,
|
|
model: str,
|
|
api_key: Optional[str] = None,
|
|
endpoint: Optional[str] = None,
|
|
timeout: int = 120,
|
|
**_: object,
|
|
):
|
|
key = api_key or os.environ.get("ANTHROPIC_API_KEY")
|
|
super().__init__(
|
|
model=model,
|
|
endpoint=endpoint or self.DEFAULT_ENDPOINT,
|
|
api_key=key,
|
|
timeout=timeout,
|
|
)
|
|
|
|
def check_available(self) -> tuple[bool, str]:
|
|
if not self.api_key:
|
|
return False, "ANTHROPIC_API_KEY not set (use --llm-api-key or env)"
|
|
# Don't probe — a live request would cost money. First real call will
|
|
# surface auth errors if the key is invalid.
|
|
return True, "ok"
|
|
|
|
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
|
|
if not self.api_key:
|
|
raise LLMError("Anthropic provider requires ANTHROPIC_API_KEY env or --llm-api-key")
|
|
sys_prompt = system
|
|
if json_mode:
|
|
sys_prompt += "\n\nRespond with valid JSON only, no prose."
|
|
body = {
|
|
"model": self.model,
|
|
"max_tokens": 2048,
|
|
"temperature": 0.1,
|
|
"system": sys_prompt,
|
|
"messages": [{"role": "user", "content": user}],
|
|
}
|
|
headers = {
|
|
"X-API-Key": self.api_key,
|
|
"anthropic-version": self.API_VERSION,
|
|
}
|
|
data = _http_post_json(
|
|
f"{self.endpoint}/v1/messages", body, headers=headers, timeout=self.timeout
|
|
)
|
|
try:
|
|
text = "".join(
|
|
b.get("text", "") for b in data.get("content", []) or [] if b.get("type") == "text"
|
|
)
|
|
except (AttributeError, TypeError) as e:
|
|
raise LLMError(f"Unexpected response shape: {e}") from e
|
|
if not text:
|
|
raise LLMError(f"Empty response from Anthropic (model={self.model})")
|
|
return LLMResponse(text=text, model=self.model, provider=self.name, raw=data)
|
|
|
|
|
|
# ==================== FACTORY ====================
|
|
|
|
|
|
PROVIDERS: dict[str, type[LLMProvider]] = {
|
|
"ollama": OllamaProvider,
|
|
"openai-compat": OpenAICompatProvider,
|
|
"anthropic": AnthropicProvider,
|
|
}
|
|
|
|
|
|
def get_provider(
|
|
name: str,
|
|
model: str,
|
|
endpoint: Optional[str] = None,
|
|
api_key: Optional[str] = None,
|
|
timeout: int = 120,
|
|
) -> LLMProvider:
|
|
"""Build a provider by name. Raises LLMError on unknown provider."""
|
|
cls = PROVIDERS.get(name)
|
|
if cls is None:
|
|
raise LLMError(f"Unknown provider '{name}'. Choices: {sorted(PROVIDERS.keys())}")
|
|
return cls(model=model, endpoint=endpoint, api_key=api_key, timeout=timeout)
|