import aiohttp import asyncio import json import os import re from typing import Optional, Dict, Any from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from app.core.config import settings from app.core.biz_exception import NotFoundError, BizLogicError from app.core.logger import logger from app.schemas.llm import ParseUserInputsPayload, ParseUserInputsOut from app.models.schema import VasSchema class LlmService: """ 通用 LLM 服务(OpenAI-compatible 协议) 支持 Qwen (DashScope)、OpenAI、DeepSeek、Moonshot、Ollama 等任意标准兼容模型 """ @classmethod def get_api_key(cls) -> str: """获取 API Key(优先读取 settings.llm_api_key,其次读取环境变量 LLM_API_KEY)""" return ( (settings.llm_api_key or "").strip() or os.getenv("LLM_API_KEY", "").strip() ) @classmethod def get_completions_url(cls) -> str: """获取并规范化 /chat/completions 完整请求 URL""" base = ( (settings.llm_base_url or "").strip() or os.getenv("LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1").strip() ).rstrip("/") if base.endswith("/chat/completions"): return base return f"{base}/chat/completions" @classmethod def get_default_model(cls) -> str: """获取默认模型名称""" return ( (settings.llm_model or "").strip() or os.getenv("LLM_MODEL", "qwen-plus").strip() ) @classmethod def clean_json_response(cls, content_str: str) -> Dict[str, Any]: """ 从 LLM 返回的内容中提取并解析合法 JSON,自动剥离 Markdown 代码块 """ if not content_str: return {} text = content_str.strip() # 匹配 ```json ... ``` 或 ``` ... ``` code_block_match = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", text, re.IGNORECASE) if code_block_match: text = code_block_match.group(1).strip() # 尝试直接解析 try: return json.loads(text) except json.JSONDecodeError: # 尝试提取最外层的 { ... } brace_match = re.search(r"(\{[\s\S]*\})", text) if brace_match: return json.loads(brace_match.group(1)) raise @classmethod async def handle_parse(cls, db: AsyncSession, payload: ParseUserInputsPayload) -> ParseUserInputsOut: """ 处理数据解析接口请求 """ stmt = select(VasSchema).where(VasSchema.id == payload.schema_id) obj = (await db.execute(stmt)).scalar_one_or_none() if not obj: raise NotFoundError("Schema not exist") parsed_obj = await cls.parse_data_async( user_text=payload.input_raw_str, json_schema=obj.schema_json, model=payload.model ) return ParseUserInputsOut(parsed_obj=parsed_obj) @classmethod async def parse_data_async( cls, user_text: str, json_schema: dict, model: Optional[str] = None, max_retries: int = 1 ) -> Dict[str, Any]: """ [异步版本] 调用配置好的 OpenAI 兼容接口解析数据 """ api_key = cls.get_api_key() if not api_key: logger.warning("[LlmService] No LLM API Key configured.") raise BizLogicError("LLM API Key not configured") url = cls.get_completions_url() selected_model = (model or "").strip() or cls.get_default_model() temperature = settings.llm_temperature timeout_seconds = settings.llm_timeout_seconds or 30 headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } # 构造 Prompt system_instruction = "You are a specialized data extraction API. Output valid JSON only." user_prompt = f""" Extract data from the text strictly based on the provided JSON Schema. [JSON Schema] {json.dumps(json_schema, ensure_ascii=False)} [User Text] {user_text} """ payload = { "model": selected_model, "messages": [ {"role": "system", "content": system_instruction}, {"role": "user", "content": user_prompt} ], "temperature": temperature, "response_format": {"type": "json_object"} } timeout = aiohttp.ClientTimeout(total=float(timeout_seconds)) for attempt in range(max_retries + 1): try: async with aiohttp.ClientSession(timeout=timeout) as session: async with session.post(url, headers=headers, json=payload) as response: if response.status != 200: error_text = await response.text() logger.error(f"[LlmService] HTTP {response.status} from {url}: {error_text}") if attempt == max_retries: raise BizLogicError(f"LLM request failed: HTTP {response.status}") await asyncio.sleep(0.5) continue result = await response.json() choices = result.get("choices", []) if not choices: raise BizLogicError("LLM response did not contain completion choices") content_str = choices[0].get("message", {}).get("content", "") try: return cls.clean_json_response(content_str) except Exception as e: logger.error(f"[LlmService] Failed to parse JSON from LLM response: {content_str}, error: {e}") raise BizLogicError("Failed to parse valid JSON from LLM response") except BizLogicError: raise except Exception as e: logger.error(f"[LlmService] Exception on attempt {attempt + 1}: {e}") if attempt == max_retries: raise BizLogicError(f"LLM invocation error: {str(e)}") await asyncio.sleep(0.5) raise BizLogicError("LLM request failed")