"""OpenAI 兼容 LLM 客户端(httpx)。支持云端与本地 vLLM/Ollama(密钥可空)。""" import httpx from config import config _client = httpx.Client(timeout=300.0) def chat(messages: list[dict], temperature: float = 0.4, max_tokens: int = 4096) -> str: base_url, api_key, model = config.effective_llm() headers = {"Content-Type": "application/json"} if api_key: # 本地无鉴权端点不发送 Authorization headers["Authorization"] = f"Bearer {api_key}" resp = _client.post( f"{base_url.rstrip('/')}/chat/completions", headers=headers, json={ "model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens, "stream": False, }, ) resp.raise_for_status() data = resp.json() return data["choices"][0]["message"]["content"]