import json import urllib.request URL = "http://127.0.0.1:11434/v1/chat/completions" MODEL = "qwen3-8b-8k" SYSTEM = """You answer questions using only the numbered notes below. Rules: - Use only facts stated in the notes. Never add outside knowledge. - End every sentence that states a fact with the id of the note it came from, like [c2]. - If the notes do not contain the answer, reply exactly: I don't know from my notes. - Answer in at most five sentences.""" def notes_block(chunks): return "\n\n".join(f"[c{i + 1}] {c['title']} / {c['section']}\n{c['text']}" for i, c in enumerate(chunks)) def generate(query, chunks): if chunks: messages = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": f"Notes:\n\n{notes_block(chunks)}\n\nQuestion: {query}"}] else: messages = [{"role": "system", "content": "Reply briefly and warmly in one or two sentences."}, {"role": "user", "content": query}] body = json.dumps({"model": MODEL, "messages": messages, "temperature": 0, "reasoning_effort": "none"}).encode() req = urllib.request.Request(URL, body, {"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=900) as resp: data = json.load(resp) return data["choices"][0]["message"]["content"].strip(), data["usage"]