Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/tutorial/en/src/task_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,18 @@
# When multiple tool calls are generated, and ``parallel_tool_calls`` is set to ``True``,
# they will be executed in parallel by the ``asyncio.gather`` function.
#
# .. note:: The parallel tool execution in ``ReActAgent`` is implemented based on ``asyncio.gather``. Therefore, to maximize the effect of parallel tool execution, both the tool function itself and the logic within it must be asynchronous.
#
# .. note:: When running, please ensure that parallel tool calling is supported at the model level and the corresponding parameters are set correctly (can be passed through ``generate_kwargs``). For example, for the DashScope API, you need to set ``parallel_tool_calls`` to ``True``, otherwise parallel tool calling will not be possible.


# prepare a tool function
def example_tool_function(tag: str) -> ToolResponse:
async def example_tool_function(tag: str) -> ToolResponse:
"""A sample example tool function"""
start_time = datetime.now().strftime("%H:%M:%S.%f")

# Sleep for 3 seconds to simulate a long-running task
time.sleep(3)
await asyncio.sleep(3)

end_time = datetime.now().strftime("%H:%M:%S.%f")
return ToolResponse(
Expand All @@ -152,6 +155,10 @@ def example_tool_function(tag: str) -> ToolResponse:
model=DashScopeChatModel(
model_name="qwen-max",
api_key=os.environ["DASHSCOPE_API_KEY"],
# Preset the generation kwargs to enable parallel tool calls
generate_kwargs={
"parallel_tool_calls": True,
},
),
memory=InMemoryMemory(),
formatter=DashScopeChatFormatter(),
Expand Down
11 changes: 9 additions & 2 deletions docs/tutorial/zh_CN/src/task_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@
# 当 LLM 生成多个工具调用且 ``parallel_tool_calls`` 设置为 ``True`` 时,
# 它们将通过 ``asyncio.gather`` 函数并行执行。
#
# .. note:: ``ReActAgent`` 中的工具并行调用是基于异步 ``asyncio.gather`` 实现的,因此,只有当工具函数是异步函数,同时工具函数内也为异步逻辑时,才能最大程度发挥工具并行执行的效果
#
# .. note:: 运行时请确保模型层面支持工具并行调用,并且相应参数设置正确(可以通过 ``generate_kwargs`` 传入),例如对于DashScope API,需要设置 ``parallel_tool_calls`` 为 ``True``,否则将无法进行并行工具调用。


# 准备一个工具函数
def example_tool_function(tag: str) -> ToolResponse:
async def example_tool_function(tag: str) -> ToolResponse:
"""一个示例工具函数"""
start_time = datetime.now().strftime("%H:%M:%S.%f")

# 休眠 3 秒以模拟长时间运行的任务
time.sleep(3)
await asyncio.sleep(3)

end_time = datetime.now().strftime("%H:%M:%S.%f")
return ToolResponse(
Expand All @@ -142,6 +145,10 @@ def example_tool_function(tag: str) -> ToolResponse:
model=DashScopeChatModel(
model_name="qwen-max",
api_key=os.environ["DASHSCOPE_API_KEY"],
# 启用并行工具调用
generate_kwargs={
"parallel_tool_calls": True,
},
),
memory=InMemoryMemory(),
formatter=DashScopeChatFormatter(),
Expand Down
Loading