"""LLM service — call LLM API.""" import os, json import httpx LLM_URL = "https://api.aillm.ru/v1/chat/completions" LLM_KEY = os.environ.get("LLM_KEY", "") LLM_MODEL = "gpt-oss-120b" def call_llm(current_spec, doc_text, build_prompt_fn): """Call LLM. Returns (parsed_result, prompt_id).""" prompt, prompt_id = build_prompt_fn(current_spec, doc_text) payload = { "model": LLM_MODEL, "messages": [{"role": "user", "content": prompt}], "max_tokens": 8000, "temperature": 0.1, } with httpx.Client(http2=True, timeout=120, verify=False) as client: resp = client.post( LLM_URL, json=payload, headers={ "Authorization": f"Bearer {LLM_KEY}", "Content-Type": "application/json", }, ) resp.raise_for_status() data = resp.json() raw_text = data.get("choices", [{}])[0].get("message", {}).get("content", "") json_text = raw_text if "```json" in json_text: json_text = json_text.split("```json")[1].split("```")[0] elif "```" in json_text: json_text = json_text.split("```")[1].split("```")[0] return json.loads(json_text.strip()), prompt_id