import json import urllib.request from generate import MODEL, URL PROMPT = """Split the user's question into standalone search queries, one per thing it asks about. If it asks about one thing, return the question unchanged as the only query. Return at most 3 queries. Reply with JSON only: {"queries": ["..."]}""" def decompose(query): body = json.dumps({"model": MODEL, "temperature": 0, "reasoning_effort": "none", "response_format": {"type": "json_object"}, "messages": [{"role": "system", "content": PROMPT}, {"role": "user", "content": query}]}).encode() req = urllib.request.Request(URL, body, {"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=900) as resp: content = json.load(resp)["choices"][0]["message"]["content"] try: queries = [q for q in json.loads(content)["queries"] if isinstance(q, str) and q.strip()] except (ValueError, KeyError, TypeError): queries = [] return list(dict.fromkeys(queries))[:3] or [query]