Files
old-books/_pipeline/llm.py
T
2026-10-17 15:27:14 -05:00

19 lines
857 B
Python

"""One call to an OpenAI-compatible chat endpoint. Used only for summaries, and only when a switch is on."""
import json
import os
import urllib.request
def complete(cfg, system, user):
c = cfg["llm"]
if not c["endpoint"]:
raise SystemExit("a summary switch is on but llm.endpoint is empty in settings.yaml")
headers = {"Content-Type": "application/json"}
if c.get("api_key_env"):
headers["Authorization"] = "Bearer " + os.environ[c["api_key_env"]]
body = {"model": c["model"], "temperature": 0,
"messages": [{"role": "system", "content": system}, {"role": "user", "content": user}]}
req = urllib.request.Request(c["endpoint"], json.dumps(body).encode(), headers)
with urllib.request.urlopen(req, timeout=c["timeout_s"]) as r:
return json.load(r)["choices"][0]["message"]["content"].strip()