llm_service.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import aiohttp
  2. import asyncio
  3. import json
  4. import os
  5. import re
  6. from typing import Optional, Dict, Any
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from sqlalchemy import select
  9. from app.core.config import settings
  10. from app.core.biz_exception import NotFoundError, BizLogicError
  11. from app.core.logger import logger
  12. from app.schemas.llm import ParseUserInputsPayload, ParseUserInputsOut
  13. from app.models.schema import VasSchema
  14. class LlmService:
  15. """
  16. 通用 LLM 服务(OpenAI-compatible 协议)
  17. 支持 Qwen (DashScope)、OpenAI、DeepSeek、Moonshot、Ollama 等任意标准兼容模型
  18. """
  19. @classmethod
  20. def get_api_key(cls) -> str:
  21. """获取 API Key(优先读取 settings.llm_api_key,其次读取环境变量 LLM_API_KEY)"""
  22. return (
  23. (settings.llm_api_key or "").strip()
  24. or os.getenv("LLM_API_KEY", "").strip()
  25. )
  26. @classmethod
  27. def get_completions_url(cls) -> str:
  28. """获取并规范化 /chat/completions 完整请求 URL"""
  29. base = (
  30. (settings.llm_base_url or "").strip()
  31. or os.getenv("LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1").strip()
  32. ).rstrip("/")
  33. if base.endswith("/chat/completions"):
  34. return base
  35. return f"{base}/chat/completions"
  36. @classmethod
  37. def get_default_model(cls) -> str:
  38. """获取默认模型名称"""
  39. return (
  40. (settings.llm_model or "").strip()
  41. or os.getenv("LLM_MODEL", "qwen-plus").strip()
  42. )
  43. @classmethod
  44. def clean_json_response(cls, content_str: str) -> Dict[str, Any]:
  45. """
  46. 从 LLM 返回的内容中提取并解析合法 JSON,自动剥离 Markdown 代码块
  47. """
  48. if not content_str:
  49. return {}
  50. text = content_str.strip()
  51. # 匹配 ```json ... ``` 或 ``` ... ```
  52. code_block_match = re.search(r"```(?:json)?\s*([\s\S]*?)\s*```", text, re.IGNORECASE)
  53. if code_block_match:
  54. text = code_block_match.group(1).strip()
  55. # 尝试直接解析
  56. try:
  57. return json.loads(text)
  58. except json.JSONDecodeError:
  59. # 尝试提取最外层的 { ... }
  60. brace_match = re.search(r"(\{[\s\S]*\})", text)
  61. if brace_match:
  62. return json.loads(brace_match.group(1))
  63. raise
  64. @classmethod
  65. async def handle_parse(cls, db: AsyncSession, payload: ParseUserInputsPayload) -> ParseUserInputsOut:
  66. """
  67. 处理数据解析接口请求
  68. """
  69. stmt = select(VasSchema).where(VasSchema.id == payload.schema_id)
  70. obj = (await db.execute(stmt)).scalar_one_or_none()
  71. if not obj:
  72. raise NotFoundError("Schema not exist")
  73. parsed_obj = await cls.parse_data_async(
  74. user_text=payload.input_raw_str,
  75. json_schema=obj.schema_json,
  76. model=payload.model
  77. )
  78. return ParseUserInputsOut(parsed_obj=parsed_obj)
  79. @classmethod
  80. async def parse_data_async(
  81. cls,
  82. user_text: str,
  83. json_schema: dict,
  84. model: Optional[str] = None,
  85. max_retries: int = 1
  86. ) -> Dict[str, Any]:
  87. """
  88. [异步版本] 调用配置好的 OpenAI 兼容接口解析数据
  89. """
  90. api_key = cls.get_api_key()
  91. if not api_key:
  92. logger.warning("[LlmService] No LLM API Key configured.")
  93. raise BizLogicError("LLM API Key not configured")
  94. url = cls.get_completions_url()
  95. selected_model = (model or "").strip() or cls.get_default_model()
  96. temperature = settings.llm_temperature
  97. timeout_seconds = settings.llm_timeout_seconds or 30
  98. headers = {
  99. "Authorization": f"Bearer {api_key}",
  100. "Content-Type": "application/json",
  101. }
  102. # 构造 Prompt
  103. system_instruction = "You are a specialized data extraction API. Output valid JSON only."
  104. user_prompt = f"""
  105. Extract data from the text strictly based on the provided JSON Schema.
  106. [JSON Schema]
  107. {json.dumps(json_schema, ensure_ascii=False)}
  108. [User Text]
  109. {user_text}
  110. """
  111. payload = {
  112. "model": selected_model,
  113. "messages": [
  114. {"role": "system", "content": system_instruction},
  115. {"role": "user", "content": user_prompt}
  116. ],
  117. "temperature": temperature,
  118. "response_format": {"type": "json_object"}
  119. }
  120. timeout = aiohttp.ClientTimeout(total=float(timeout_seconds))
  121. for attempt in range(max_retries + 1):
  122. try:
  123. async with aiohttp.ClientSession(timeout=timeout) as session:
  124. async with session.post(url, headers=headers, json=payload) as response:
  125. if response.status != 200:
  126. error_text = await response.text()
  127. logger.error(f"[LlmService] HTTP {response.status} from {url}: {error_text}")
  128. if attempt == max_retries:
  129. raise BizLogicError(f"LLM request failed: HTTP {response.status}")
  130. await asyncio.sleep(0.5)
  131. continue
  132. result = await response.json()
  133. choices = result.get("choices", [])
  134. if not choices:
  135. raise BizLogicError("LLM response did not contain completion choices")
  136. content_str = choices[0].get("message", {}).get("content", "")
  137. try:
  138. return cls.clean_json_response(content_str)
  139. except Exception as e:
  140. logger.error(f"[LlmService] Failed to parse JSON from LLM response: {content_str}, error: {e}")
  141. raise BizLogicError("Failed to parse valid JSON from LLM response")
  142. except BizLogicError:
  143. raise
  144. except Exception as e:
  145. logger.error(f"[LlmService] Exception on attempt {attempt + 1}: {e}")
  146. if attempt == max_retries:
  147. raise BizLogicError(f"LLM invocation error: {str(e)}")
  148. await asyncio.sleep(0.5)
  149. raise BizLogicError("LLM request failed")