function_calling.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. """Function Calling 工具调用循环:调用模型→执行工具→回填结果→继续调用"""
  2. import asyncio
  3. import json
  4. import logging
  5. import time
  6. from typing import Any, Awaitable, Callable, Optional, Union
  7. from backend.config import settings
  8. from backend.llm.client import DeepSeekClient
  9. from backend.models.schemas import ToolCallRecord
  10. logger = logging.getLogger(__name__)
  11. # 工具可调用对象类型:同步或异步函数均可
  12. ToolCallable = Union[Callable[..., Any], Callable[..., Awaitable[Any]]]
  13. def _message_to_dict(message: Any) -> dict:
  14. """把 OpenAI 返回的 message 对象转为可序列化的 dict,便于回填到 messages"""
  15. msg: dict = {"role": message.role, "content": message.content or ""}
  16. tool_calls = getattr(message, "tool_calls", None)
  17. if tool_calls:
  18. msg["tool_calls"] = [
  19. {
  20. "id": tc.id,
  21. "type": "function",
  22. "function": {
  23. "name": tc.function.name,
  24. "arguments": tc.function.arguments,
  25. },
  26. }
  27. for tc in tool_calls
  28. ]
  29. return msg
  30. class ToolLoop:
  31. """工具调用循环
  32. 循环逻辑:调用模型 → 若返回 tool_calls 则执行对应工具 →
  33. 把工具结果以 role=tool 回填 → 继续调用,直到模型无 tool_calls 返回最终文本。
  34. """
  35. def __init__(
  36. self,
  37. client: DeepSeekClient,
  38. tool_registry: dict[str, ToolCallable],
  39. ) -> None:
  40. self.client = client
  41. self.tool_registry = tool_registry
  42. self.tool_calls: list[ToolCallRecord] = []
  43. async def run(
  44. self,
  45. messages: list[dict],
  46. tools_schema: list[dict],
  47. max_iterations: int = 10,
  48. ) -> str:
  49. """运行工具调用循环,返回模型最终文本回复"""
  50. # 复制一份消息,避免修改入参
  51. working_messages = list(messages)
  52. final_content = ""
  53. for iteration in range(max_iterations):
  54. response = await self.client.chat(
  55. messages=working_messages,
  56. tools=tools_schema,
  57. tool_choice="auto",
  58. )
  59. choice = response.choices[0]
  60. message = choice.message
  61. final_content = message.content or ""
  62. # 把模型消息追加到对话上下文
  63. working_messages.append(_message_to_dict(message))
  64. tool_calls = getattr(message, "tool_calls", None)
  65. if not tool_calls:
  66. # 模型未请求工具,返回最终文本
  67. return final_content
  68. # 依次执行每个工具调用并回填结果
  69. for tool_call in tool_calls:
  70. record, content = await self._execute_tool_call(tool_call)
  71. working_messages.append(
  72. {
  73. "role": "tool",
  74. "tool_call_id": tool_call.id,
  75. "content": content,
  76. }
  77. )
  78. # 达到最大迭代次数仍未结束,降级返回最后一次内容
  79. logger.warning("工具调用循环达到最大迭代次数 %d", max_iterations)
  80. return final_content
  81. async def _invoke(self, tool_fn: ToolCallable, args: dict) -> Any:
  82. """调用工具,自动区分同步/异步函数
  83. 同步工具用 asyncio.to_thread 包装,避免阻塞事件循环;
  84. 异步工具直接 await。
  85. """
  86. if asyncio.iscoroutinefunction(tool_fn):
  87. return await tool_fn(**args)
  88. return await asyncio.to_thread(tool_fn, **args)
  89. async def _execute_tool_call(self, tool_call: Any) -> tuple[ToolCallRecord, str]:
  90. """执行单个工具调用,返回 (调用记录, 回填结果文本)
  91. 工具执行失败时重试 TOOL_MAX_RETRIES 次,仍失败则记录错误并继续(降级不崩溃)。
  92. """
  93. tool_name = tool_call.function.name
  94. # 解析工具参数
  95. try:
  96. args = json.loads(tool_call.function.arguments or "{}")
  97. except json.JSONDecodeError:
  98. args = {}
  99. args_summary = json.dumps(args, ensure_ascii=False)[:200]
  100. # 查找工具
  101. tool_fn = self.tool_registry.get(tool_name)
  102. if tool_fn is None:
  103. record = ToolCallRecord(
  104. tool_name=tool_name,
  105. args_summary=args_summary,
  106. success=False,
  107. duration_sec=0.0,
  108. error=f"未注册的工具: {tool_name}",
  109. )
  110. self.tool_calls.append(record)
  111. return record, f"工具执行失败: 未注册的工具 {tool_name}"
  112. # 执行工具(带重试)
  113. start = time.perf_counter()
  114. last_error: Optional[str] = None
  115. result: Any = None
  116. success = False
  117. total_attempts = settings.TOOL_MAX_RETRIES + 1 # 初次 + 重试次数
  118. for attempt in range(total_attempts):
  119. try:
  120. result = await self._invoke(tool_fn, args)
  121. success = True
  122. break
  123. except Exception as exc:
  124. last_error = str(exc)
  125. logger.warning(
  126. "工具 %s 执行失败 (attempt=%d/%d): %s",
  127. tool_name,
  128. attempt + 1,
  129. total_attempts,
  130. exc,
  131. )
  132. duration = time.perf_counter() - start
  133. record = ToolCallRecord(
  134. tool_name=tool_name,
  135. args_summary=args_summary,
  136. success=success,
  137. duration_sec=round(duration, 3),
  138. error=None if success else last_error,
  139. result=result if success else None, # 持久化工具返回值,供 ReportAgent 整合
  140. )
  141. self.tool_calls.append(record)
  142. # 构造回填内容:成功则序列化结果,失败则回填错误信息(降级继续)
  143. if success:
  144. if isinstance(result, str):
  145. content = result
  146. else:
  147. content = json.dumps(result, ensure_ascii=False, default=str)
  148. else:
  149. content = f"工具执行失败: {last_error}"
  150. return record, content
  151. def get_tool_records(self) -> list[ToolCallRecord]:
  152. """返回所有工具调用记录"""
  153. return list(self.tool_calls)