llm_service.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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")
  150. @classmethod
  151. async def parse_tutorial_async(
  152. cls,
  153. prompt: str,
  154. system_prompt: Optional[str] = None,
  155. model: Optional[str] = None,
  156. max_retries: int = 1,
  157. ) -> Dict[str, Any]:
  158. """
  159. 利用大模型 (如 DeepSeek) 根据用户的自然语言需求,自动生成商品用户使用教程与操作指引结构化配置
  160. 支持前端传入自定义/微调后的 system_prompt
  161. """
  162. api_key = cls.get_api_key()
  163. if not api_key:
  164. logger.warning("[LlmService] No LLM API Key configured.")
  165. raise BizLogicError("LLM API Key not configured")
  166. url = cls.get_completions_url()
  167. selected_model = (model or "").strip() or cls.get_default_model()
  168. temperature = 0.3
  169. timeout_seconds = settings.llm_timeout_seconds or 45
  170. headers = {
  171. "Authorization": f"Bearer {api_key}",
  172. "Content-Type": "application/json",
  173. }
  174. default_instruction = (
  175. "You are an expert e-commerce operations specialist for international visa, embassy, and appointment booking services.\n"
  176. "Your primary mission is to generate a MANDATORY PRE-ORDER PREREQUISITE CHECKLIST (下单前置必备条件与门槛说明) for customers before they can purchase this service.\n\n"
  177. "CORE EMPHASIS:\n"
  178. "The single most important purpose of this guide is establishing the HARD PRE-CONDITIONS required BEFORE placing an order. Customers must satisfy these prerequisites first; otherwise, their automated booking order cannot proceed and will fail.\n\n"
  179. "CORE WRITING STYLE:\n"
  180. "1. PREREQUISITE-FIRST & ULTRA-CONCISE: Focus exclusively on what the buyer MUST have ready before paying. Avoid generic website tutorials or fluffy disclaimers. Every word must count.\n"
  181. "2. ALL ENGLISH: Regardless of input language, generate crisp, authoritative, professional English.\n\n"
  182. "JSON SCHEMA:\n"
  183. "{\n"
  184. ' "display_type": "both", // Allowed: "both", "modal", "link", "embedded"\n'
  185. ' "button_text": "⚠️ Mandatory Pre-Order Checklist", // Short warning/checklist label (3-5 words with emoji)\n'
  186. ' "modal_title": "Mandatory Prerequisites Before Placing Order", // Clear pre-order gate title (5-8 words)\n'
  187. ' "link_url": "https://...", // Official portal URL if mentioned, else ""\n'
  188. ' "link_text": "Official Portal ↗", // Short label (2-3 words)\n'
  189. ' "badge_text": "Mandatory Prerequisite", // Badge indicating required pre-order gate (1-3 words)\n'
  190. ' "summary": "...", // STRICTLY 1-2 punchy sentences warning the buyer what MUST be ready before ordering (under 30 words).\n'
  191. ' "steps": [\n'
  192. ' {\n'
  193. ' "step_no": 1,\n'
  194. ' "title": "Action Title", // 2-4 words (e.g. "Activated Portal Account", "Valid Passport Scan", "Cancel Prior Bookings")\n'
  195. ' "desc": "One crisp sentence explaining the exact prerequisite required before order (strictly under 20 words)."\n'
  196. ' }\n'
  197. ' ],\n'
  198. ' "content": "..." // Max 2 critical FAQs addressing pre-order blockers (e.g. can I order first without account? overlapping bookings?). Format: **Q: ...?**\\nA: One crisp sentence.\n'
  199. "}\n\n"
  200. "CRITICAL RULES:\n"
  201. "1. FOCUS ON PRE-CONDITIONS: Every single step MUST be a prerequisite task completed BEFORE ordering.\n"
  202. "2. MAX 3 STEPS: Each step's description MUST be strictly 1 single, compact sentence (< 20 words).\n"
  203. "3. SUMMARY: Strictly 1 to 2 sentences max (< 35 words total).\n"
  204. "4. FAQ: Strictly 2 questions maximum, with direct 1-sentence answers.\n"
  205. "5. Output STRICT JSON only without Markdown wrappers or greetings."
  206. )
  207. effective_system_prompt = system_prompt.strip() if (system_prompt and system_prompt.strip()) else default_instruction
  208. payload = {
  209. "model": selected_model,
  210. "messages": [
  211. {"role": "system", "content": effective_system_prompt},
  212. {"role": "user", "content": prompt}
  213. ],
  214. "temperature": temperature,
  215. "response_format": {"type": "json_object"}
  216. }
  217. timeout = aiohttp.ClientTimeout(total=float(timeout_seconds))
  218. for attempt in range(max_retries + 1):
  219. try:
  220. async with aiohttp.ClientSession(timeout=timeout) as session:
  221. async with session.post(url, headers=headers, json=payload) as response:
  222. if response.status != 200:
  223. error_text = await response.text()
  224. logger.error(f"[LlmService] HTTP {response.status} from {url}: {error_text}")
  225. if attempt == max_retries:
  226. raise BizLogicError(f"LLM request failed: HTTP {response.status}")
  227. await asyncio.sleep(0.5)
  228. continue
  229. result = await response.json()
  230. choices = result.get("choices", [])
  231. if not choices:
  232. raise BizLogicError("LLM response did not contain completion choices")
  233. content_str = choices[0].get("message", {}).get("content", "")
  234. parsed = cls.clean_json_response(content_str)
  235. if not isinstance(parsed, dict):
  236. raise BizLogicError("LLM returned non-dict JSON")
  237. # 若模型在顶层嵌套了 tutorial_config / tutorial / config / data,先解包
  238. if "tutorial_config" in parsed and isinstance(parsed["tutorial_config"], dict):
  239. parsed = parsed["tutorial_config"]
  240. elif "tutorial" in parsed and isinstance(parsed["tutorial"], dict):
  241. parsed = parsed["tutorial"]
  242. elif "config" in parsed and isinstance(parsed["config"], dict):
  243. parsed = parsed["config"]
  244. elif "data" in parsed and isinstance(parsed["data"], dict):
  245. parsed = parsed["data"]
  246. # 全量健壮字段对齐与多格式兼容,确保精准填充至表单各位置
  247. btn_txt = parsed.get("button_text") or parsed.get("button_label") or parsed.get("btn_text") or "📖 Guide: Prepare Your Account"
  248. m_title = parsed.get("modal_title") or parsed.get("title") or parsed.get("name") or "Official Portal Registration & Booking Guide"
  249. l_url = parsed.get("link_url") or parsed.get("url") or parsed.get("link") or ""
  250. l_txt = parsed.get("link_text") or parsed.get("url_text") or "Open Official Portal ↗"
  251. summ = parsed.get("summary") or parsed.get("overview") or parsed.get("description") or ""
  252. raw_faq = parsed.get("content") or parsed.get("faq") or parsed.get("faq_content") or parsed.get("details") or ""
  253. if isinstance(raw_faq, list):
  254. faq_lines = []
  255. for i, item in enumerate(raw_faq):
  256. if isinstance(item, dict):
  257. q = item.get("q") or item.get("question") or item.get("title") or f"Question {i+1}"
  258. a = item.get("a") or item.get("answer") or item.get("desc") or item.get("content") or ""
  259. faq_lines.append(f"**Q{i+1}. {q}**\n{a}")
  260. elif isinstance(item, str):
  261. faq_lines.append(item)
  262. faq_content = "\n\n".join(faq_lines)
  263. else:
  264. faq_content = str(raw_faq)
  265. badge_txt = parsed.get("badge_text") or "Prerequisite Guide"
  266. d_type = str(parsed.get("display_type", "both")).lower()
  267. if "modal" in d_type:
  268. d_type = "modal"
  269. elif "link" in d_type:
  270. d_type = "link"
  271. elif "embed" in d_type:
  272. d_type = "embedded"
  273. else:
  274. d_type = "both"
  275. steps_raw = parsed.get("steps", [])
  276. if not isinstance(steps_raw, list):
  277. if isinstance(steps_raw, str):
  278. steps_raw = [s.strip() for s in steps_raw.split("\n") if s.strip()]
  279. else:
  280. steps_raw = []
  281. normalized_steps = []
  282. for idx, step in enumerate(steps_raw):
  283. if isinstance(step, dict):
  284. normalized_steps.append({
  285. "step_no": step.get("step_no", idx + 1),
  286. "title": step.get("title") or step.get("step_title") or step.get("name") or f"Step {idx + 1}",
  287. "desc": step.get("desc") or step.get("description") or step.get("content") or step.get("detail") or "",
  288. })
  289. elif isinstance(step, str) and step.strip():
  290. normalized_steps.append({
  291. "step_no": idx + 1,
  292. "title": f"Step {idx + 1}",
  293. "desc": step.strip(),
  294. })
  295. return {
  296. "enabled": True,
  297. "display_type": d_type,
  298. "button_text": btn_txt,
  299. "modal_title": m_title,
  300. "link_url": l_url,
  301. "link_text": l_txt,
  302. "badge_text": badge_txt,
  303. "summary": summ,
  304. "steps": normalized_steps,
  305. "content": faq_content,
  306. }
  307. except BizLogicError:
  308. raise
  309. except Exception as e:
  310. logger.error(f"[LlmService] parse_tutorial_async attempt {attempt} failed: {e}")
  311. if attempt == max_retries:
  312. raise BizLogicError(f"AI tutorial parsing failed: {str(e)}")
  313. await asyncio.sleep(0.5)
  314. raise BizLogicError("AI tutorial parsing failed")
  315. @classmethod
  316. async def chat_completion_with_tools_async(
  317. cls,
  318. messages: list[dict],
  319. tools: Optional[list[dict]] = None,
  320. model: Optional[str] = None,
  321. temperature: Optional[float] = None,
  322. max_retries: int = 1,
  323. ) -> dict:
  324. """
  325. 调用 OpenAI 兼容接口,支持 Function Calling (tools)
  326. 返回 choices[0]["message"] (包含 role, content, tool_calls 等)
  327. """
  328. api_key = cls.get_api_key()
  329. if not api_key:
  330. logger.warning("[LlmService] No LLM API Key configured.")
  331. raise BizLogicError("LLM API Key not configured")
  332. url = cls.get_completions_url()
  333. selected_model = (model or "").strip() or cls.get_default_model()
  334. temp = settings.llm_temperature if temperature is None else temperature
  335. timeout_seconds = settings.llm_timeout_seconds or 45
  336. headers = {
  337. "Authorization": f"Bearer {api_key}",
  338. "Content-Type": "application/json",
  339. }
  340. payload: dict[str, Any] = {
  341. "model": selected_model,
  342. "messages": messages,
  343. "temperature": temp,
  344. }
  345. if tools:
  346. payload["tools"] = tools
  347. payload["tool_choice"] = "auto"
  348. timeout = aiohttp.ClientTimeout(total=float(timeout_seconds))
  349. for attempt in range(max_retries + 1):
  350. try:
  351. async with aiohttp.ClientSession(timeout=timeout) as session:
  352. async with session.post(url, headers=headers, json=payload) as response:
  353. if response.status != 200:
  354. error_text = await response.text()
  355. logger.error(f"[LlmService] HTTP {response.status} from {url}: {error_text}")
  356. if attempt == max_retries:
  357. raise BizLogicError(f"LLM request failed: HTTP {response.status} - {error_text[:200]}")
  358. await asyncio.sleep(0.5)
  359. continue
  360. result = await response.json()
  361. choices = result.get("choices", [])
  362. if not choices:
  363. raise BizLogicError("LLM response did not contain completion choices")
  364. return choices[0].get("message", {})
  365. except BizLogicError:
  366. raise
  367. except Exception as e:
  368. logger.error(f"[LlmService] Exception on attempt {attempt + 1}: {e}")
  369. if attempt == max_retries:
  370. raise BizLogicError(f"LLM invocation error: {str(e)}")
  371. await asyncio.sleep(0.5)
  372. raise BizLogicError("LLM request failed")
  373. @classmethod
  374. async def chat_completion_stream_async(
  375. cls,
  376. messages: list[dict],
  377. tools: Optional[list[dict]] = None,
  378. model: Optional[str] = None,
  379. temperature: Optional[float] = None,
  380. ):
  381. """
  382. 流式调用 OpenAI 兼容接口,逐块 yield delta 字典
  383. """
  384. api_key = cls.get_api_key()
  385. if not api_key:
  386. logger.warning("[LlmService] No LLM API Key configured.")
  387. raise BizLogicError("LLM API Key not configured")
  388. url = cls.get_completions_url()
  389. selected_model = (model or "").strip() or cls.get_default_model()
  390. temp = settings.llm_temperature if temperature is None else temperature
  391. timeout_seconds = settings.llm_timeout_seconds or 60
  392. headers = {
  393. "Authorization": f"Bearer {api_key}",
  394. "Content-Type": "application/json",
  395. }
  396. payload: dict[str, Any] = {
  397. "model": selected_model,
  398. "messages": messages,
  399. "temperature": temp,
  400. "stream": True,
  401. }
  402. if tools:
  403. payload["tools"] = tools
  404. payload["tool_choice"] = "auto"
  405. timeout = aiohttp.ClientTimeout(total=float(timeout_seconds))
  406. async with aiohttp.ClientSession(timeout=timeout) as session:
  407. async with session.post(url, headers=headers, json=payload) as response:
  408. if response.status != 200:
  409. error_text = await response.text()
  410. logger.error(f"[LlmService stream] HTTP {response.status} from {url}: {error_text}")
  411. raise BizLogicError(f"LLM stream request failed: HTTP {response.status}")
  412. async for line in response.content:
  413. line_str = line.decode("utf-8").strip()
  414. if not line_str or not line_str.startswith("data:"):
  415. continue
  416. data_str = line_str[5:].strip()
  417. if data_str == "[DONE]":
  418. break
  419. try:
  420. chunk = json.loads(data_str)
  421. choices = chunk.get("choices", [])
  422. if choices:
  423. delta = choices[0].get("delta", {})
  424. yield delta
  425. except Exception:
  426. continue