feat(llm): talk to OpenAI-compatible servers such as Strata

A base URL that ends in /v1 now selects the OpenAI-compatible API: chat goes
to /chat/completions and embeddings to /embeddings. The conversion lives in
src/jarvis/llm.py only, so the reply engine and every other caller keep their
Ollama-shaped messages and responses.

- Requests drop private engine keys, send tool-call arguments as JSON strings,
  map num_predict to max_tokens and the thinking switch to
  chat_template_kwargs.enable_thinking, and leave out num_ctx and keep_alive.
- Responses come back with message.content, tool calls with dict arguments and
  an id, and message.thinking from reasoning_content.
- Streaming, the warm-up (one-token chat completion), the intent judge and the
  dictation clean-up use the same path. A server without an embedding model
  fails open to the text paths.
- Specs and docs/llm_contexts.md describe the transport; roadmap records M3
  step 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 12:32:48 +09:00
parent beaacb39cd
commit 6b23a8adc7
11 changed files with 558 additions and 31 deletions

View File

@@ -2,7 +2,31 @@ from __future__ import annotations
import requests
def _openai_embedding(text: str, base_url: str, model: str, timeout_sec: float) -> list[float] | None:
"""``/embeddings`` of an OpenAI-compatible server. A server without an
embedding model (Strata serves the chat model only) answers an error, and
the caller falls back to text search."""
try:
resp = requests.post(
f"{base_url.rstrip('/')}/embeddings",
json={"model": model, "input": text},
timeout=timeout_sec,
)
resp.raise_for_status()
data = resp.json().get("data") or []
vec = data[0].get("embedding") if data and isinstance(data[0], dict) else None
if isinstance(vec, list):
return [float(x) for x in vec]
except Exception:
return None
return None
def get_embedding(text: str, base_url: str, model: str, timeout_sec: float = 15.0) -> list[float] | None:
from ..llm import is_openai_compatible
if is_openai_compatible(base_url):
return _openai_embedding(text, base_url, model, timeout_sec)
try:
resp = requests.post(
f"{base_url.rstrip('/')}/api/embeddings",