"""LLM service — call LLM API with optional DI.""" import json from config import LLM_URL, LLM_KEY, LLM_MODEL # Ленивый singleton — обратная совместимость _llm_client = None def _get_default_client(): global _llm_client if _llm_client is None: from services.llm_client import HttpxLLMClient _llm_client = HttpxLLMClient(url=LLM_URL, key=LLM_KEY, model=LLM_MODEL) return _llm_client def call_llm(current_spec, doc_text, build_prompt_fn, llm_client=None): """Call LLM. Returns (parsed_result, prompt_id). llm_client: LLMClient (optional). Default — HttpxLLMClient (prod). """ if llm_client is None: llm_client = _get_default_client() prompt, prompt_id = build_prompt_fn(current_spec, doc_text) raw_text = llm_client.complete(prompt) 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