client.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """DeepSeek LLM 客户端:基于 HelloAgents 框架的 HelloAgentsLLM 实现
  2. PaperMind 的多 Agent 编排构建在 HelloAgents 框架之上:
  3. - LLM 调用统一走框架的 HelloAgentsLLM(OpenAI 兼容适配器,自动适配 DeepSeek)
  4. - chat() 返回与 OpenAI SDK ChatCompletion 兼容的对象,ToolLoop 等既有调用方无需改动
  5. """
  6. import asyncio
  7. import logging
  8. from typing import Any, Optional
  9. from hello_agents import HelloAgentsLLM
  10. from backend.config import settings
  11. logger = logging.getLogger(__name__)
  12. class _ShimMessage:
  13. """把 HelloAgentsLLM 的文本响应包装为 OpenAI ChatCompletion.message 形状"""
  14. def __init__(self, content: str, reasoning_content: Optional[str] = None) -> None:
  15. self.role = "assistant"
  16. self.content = content
  17. self.reasoning_content = reasoning_content
  18. self.tool_calls = None
  19. class _ShimChoice:
  20. def __init__(self, message: _ShimMessage) -> None:
  21. self.message = message
  22. self.finish_reason = "stop"
  23. self.index = 0
  24. class ChatCompletionShim:
  25. """无工具调用的文本响应适配:兼容 response.choices[0].message.content 访问方式"""
  26. def __init__(self, llm_response: Any) -> None:
  27. self.choices = [
  28. _ShimChoice(
  29. _ShimMessage(
  30. content=getattr(llm_response, "content", "") or "",
  31. reasoning_content=getattr(llm_response, "reasoning_content", None),
  32. )
  33. )
  34. ]
  35. self.model = getattr(llm_response, "model", settings.DEEPSEEK_MODEL)
  36. self.usage = getattr(llm_response, "usage", None)
  37. self.id = "papermind-hello-agents"
  38. class DeepSeekClient:
  39. """DeepSeek 异步客户端:底层由 HelloAgents 框架的 HelloAgentsLLM 驱动
  40. HelloAgentsLLM 基于 OpenAI 兼容适配器访问 DeepSeek,
  41. chat() 对外保持与 OpenAI SDK ChatCompletion 兼容的返回形状。
  42. """
  43. def __init__(self) -> None:
  44. # 框架 LLM:模型/密钥/地址来自项目配置,适配器自动识别 DeepSeek
  45. self.llm = HelloAgentsLLM(
  46. model=settings.DEEPSEEK_MODEL,
  47. api_key=settings.DEEPSEEK_API_KEY,
  48. base_url=settings.DEEPSEEK_BASE_URL,
  49. temperature=settings.LLM_TEMPERATURE,
  50. timeout=settings.LLM_TIMEOUT,
  51. )
  52. self.model = settings.DEEPSEEK_MODEL
  53. self.timeout = settings.LLM_TIMEOUT
  54. self.max_retries = settings.TOOL_MAX_RETRIES
  55. async def chat(
  56. self,
  57. messages: list[dict],
  58. tools: Optional[list[dict]] = None,
  59. tool_choice: str = "auto",
  60. max_tokens: Optional[int] = None,
  61. ) -> Any:
  62. """调用 Chat Completions,返回模型回复(ChatCompletion 兼容对象)
  63. 有工具时走框架 ainvoke_with_tools(返回原生 ChatCompletion);
  64. 无工具时走框架 ainvoke,包装为兼容形状。
  65. 单次调用失败时按指数退避重试 max_retries 次。
  66. max_tokens: 显式设置输出 token 上限,None 用 API 默认值(4096)。
  67. """
  68. kwargs: dict[str, Any] = {}
  69. if max_tokens is not None:
  70. kwargs["max_tokens"] = max_tokens
  71. last_exc: Optional[Exception] = None
  72. total_attempts = self.max_retries + 1 # 初次调用 + 重试次数
  73. for attempt in range(total_attempts):
  74. try:
  75. if tools:
  76. # Function Calling:框架返回原生 ChatCompletion(含 tool_calls)
  77. response = await self.llm.ainvoke_with_tools(
  78. messages=messages,
  79. tools=tools,
  80. tool_choice=tool_choice,
  81. **kwargs,
  82. )
  83. return response
  84. # 纯文本:框架返回 LLMResponse,包装为 ChatCompletion 兼容形状
  85. response = await self.llm.ainvoke(messages=messages, **kwargs)
  86. return ChatCompletionShim(response)
  87. except Exception as exc:
  88. last_exc = exc
  89. logger.warning(
  90. "DeepSeek 调用失败 (attempt=%d/%d): %s",
  91. attempt + 1,
  92. total_attempts,
  93. exc,
  94. )
  95. if attempt < self.max_retries:
  96. # 指数退避:1s, 2s, 4s ...
  97. backoff = 2 ** attempt
  98. await asyncio.sleep(backoff)
  99. raise RuntimeError(
  100. f"DeepSeek 调用失败,已重试 {self.max_retries} 次"
  101. ) from last_exc