AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

[Feature] 支持并行工具调用(Parallel Tool Execution)

Open Fronut opened this issue 5 months ago • 8 comments

Description / 描述

当前 tool_loop_agent_runner 在处理 LLM 同一轮返回的多个 tool call 时,采用串行 for 循环:

# tool_loop_agent_runner.py L995
for func_tool_name, func_tool_args, func_tool_id in zip(
    llm_response.tools_call_name,
    llm_response.tools_call_args,
    llm_response.tools_call_ids,
):
    # 一个个执行,上一个完成才轮到下一个

现在 OpenAI / Anthropic / DeepSeek 等主流模型都支持同一轮返回多个独立的 tool call(parallel function calling),但 AstrBot 全部串行化了。

具体影响

LLM 一次返回 3 个独立工具调用(典型场景:同时查多个信息):
  web_search("Python 3.13 新特性")   → 耗时 2s
  web_search("Rust 1.85 新特性")     → 耗时 2s    } 串行总计 6s+
  get_weather("Beijing")            → 耗时 2s

如果并行,总计只需 2s(取最长),延迟理论降低 3 倍。

业界对照

Spring AI、LangChain4j、Vercel AI SDK、Google ADK 均已支持或正在推进并行工具调用。AstrBot 社区目前没有相关讨论。

建议实现方式

对读操作(纯查询类工具,无副作用)使用 asyncio.gather 并行执行;写操作保持串行以避免竞态。可配合配置项 parallel_tool_execution,默认关闭,用户手动开启。

Use Case / 使用场景

  1. 多源信息聚合:用户问「对比 Python 3.13、Rust 1.85、Go 1.24 的新特性」,LLM 一次发出 3 个 web_search,当前串行 6s+,并行只需 2s
  2. 多地点查询:用户问「北京、上海、广州明天天气怎么样」,3 个 get_weather 可并行
  3. 多文件读取:Agent 需要同时读多个文件做交叉分析
  4. 子 Agent 并行:配合原生 subagent orchestrator,同步模式下多个 transfer_to_* 可并行派发,减少对 background_task=true 的依赖(后台异步存在上下文扁平化、不可取消等问题)

Willing to Submit PR? / 是否愿意提交PR?

  • [x] Yes, I am willing to submit a PR. / 是的,我愿意提交 PR。

Code of Conduct

Fronut avatar Jun 21 '26 15:06 Fronut

这个好

lq-259 avatar Jun 23 '26 10:06 lq-259

我想尝试实现这个功能,先确认一下边界,避免把所有工具调用直接 asyncio.gather 后引入副作用和取消问题。

当前 ToolLoopAgentRunner 会按 LLM 返回顺序逐个执行 tool call;FunctionTool 也没有“只读 / 可并行”的元数据,因此我倾向于先做一个保守版本:

  1. 为工具增加显式 parallel_safe 标记,默认 False;不根据名称或描述猜测是否安全。
  2. 仅并行执行同一轮中连续的、显式标记为安全的工具;有副作用、直接向用户发送消息、后台任务和未标记的 MCP 工具继续串行。
  3. 并发任务各自收集结果,最终仍按 LLM 原始 tool-call 顺序回填,避免破坏 provider 的 tool-result 关联。
  4. 停止 Agent 时取消并关闭这一批所有 executor,确保不会有后台遗留执行。
  5. 补充延迟工具的并发测试、结果保序、单个失败、取消和串行工具不并发等测试。

初版不做 WebUI 配置,先确保执行语义正确;后续如有需要再增加全局开关或并发上限。这个方向可以接受吗?确认后我再开 PR。

destire-mio avatar Jul 12 '26 10:07 destire-mio

@destire-mio 我认为这很好,但我只是协作者,具体如何您应该考虑@ 维护者

Fronut avatar Jul 12 '26 11:07 Fronut

这个好像好早的版本就有了

landamao avatar Jul 14 '26 15:07 landamao

这个好像好早的版本就有了

代码中目前版本都还没有,发多个调用就是串行执行

Fronut avatar Jul 14 '26 15:07 Fronut

这个好像好早的版本就有了

代码中目前版本都还没有,发多个调用就是串行执行

哦哦,是串行,但是不应该就是用串行一个一个执行吗,并行可能带来未知风险

landamao avatar Jul 14 '26 15:07 landamao

这个好像好早的版本就有了

代码中目前版本都还没有,发多个调用就是串行执行

哦哦,是串行,但是不应该就是用串行一个一个执行吗,并行可能带来未知风险

在Codex中,只读操作比如读取代码文件联网查资料等等都是并行的。明确某些可以并行的只读操作完全可行

Fronut avatar Jul 14 '26 15:07 Fronut

Hi maintainers! I'd like to pick up #8943. @destire-mio proposed a conservative design in July (explicit parallel_safe metadata, consecutive-group batching, default off) — I agree with that direction. My plan: group consecutive parallel_safe tool calls from the same LLM round into asyncio.gather batches, preserving result order, error handling, hooks, and abort semantics exactly as today; read-only built-ins (web_search, kb_search) get marked safe, writes and MCP tools stay serial. @destire-mio , are you still working on this? If not, I'll open a draft PR within ~2 weeks. Any existing plans or concerns? Thanks!

subaoyan16 avatar Sep 02 '26 10:09 subaoyan16