AutoGen框架的ReAct推理模式的多跳测试

问题:特斯拉公司 CEO 的出生地是哪个国家?
答案:南非。
推理过程
第一跳:确定特斯拉(Tesla, Inc.)的 CEO。特斯拉的 CEO 是埃隆·马斯克(Elon Musk)。
第二跳:查找埃隆·马斯克的出生地。埃隆·马斯克出生于南非的比勒陀利亚(Pretoria, South Africa)。

使用搜索工具获取辅助信息。

代码:

import os
from typing import Annotated

from tavily import TavilyClient

from autogen import AssistantAgent, UserProxyAgent, config_list_from_json, register_function
from autogen.agentchat.contrib.capabilities import teachability
from autogen.cache import Cache
from autogen.coding import DockerCommandLineCodeExecutor, LocalCommandLineCodeExecutor

config_list = [
    {"model": "gpt-4.1-mini", "api_key": "输入你的key"},
    {"model": "gpt-3.5-turbo", "api_key": "输入你的key"},
]


#使用Tavily作为搜索网络的工具"
tavily = TavilyClient("输入申请的Tavily的api")

#定义搜索动作
def search_tool(query: Annotated[str, "The search query"]) -> Annotated[str, "The search results"]:
    return tavily.get_search_context(query=query, search_depth="advanced")

#构建一个通用的ReAct提示和一个基于ReAct提示的自定义消息函数
ReAct_prompt = """
Answer the following questions as best you can. You have access to tools provided.

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!
Question: {input}
"""

# 定义ReAct推理提示模版
def react_prompt_message(sender, recipient, context):
    return ReAct_prompt.format(input=context["question"])

#代码执行器环境
os.makedirs("coding", exist_ok=True)
# Use docker executor for running code in a container if you have docker installed.
# code_executor = DockerCommandLineCodeExecutor(work_dir="coding")
code_executor = LocalCommandLineCodeExecutor(work_dir="coding")

#创建代理
"""
AssistantAgent:可对话agent,不与人类交互,只与LLM或外部工具交互。
UserProxyAgent:可对话agent,可与人类进行交互
"""
#这里设置了两个Agent,UserAgent模拟人类输入请求。Assistant执行“思考-动作-观察”的ReAct模式解决问答。
user_proxy = UserProxyAgent(
    name="User",
    #s_termination_msg 控制Agent聊天结束。规定以TERMINATE结束聊天。
    is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"), 
    # human_input_mode参数设置人类输入模式;有三种模式:
    #"ALWAYS"总是请求人类输入;NEVER 过程中从不请求,自主运行。
    #TERMINATE 仅在满足终止条件时才请求人工输入。
    human_input_mode="NEVER", #对话模式;
    max_consecutive_auto_reply=10,
    code_execution_config={"executor": code_executor},
)

assistant = AssistantAgent(
    name="Assistant",
    system_message="You are a multi-hop reasoning assistant, skilled at answering complex questions through the use of tools. Reply TERMINATE when the task is done.",
    llm_config={"config_list": config_list, "cache_seed": None},
)

# Register the search tool.
register_function(
    search_tool,
    caller=assistant,
    executor=user_proxy,
    name="search_tool",
    description="Search the web for the given query",
)

# # 本地存储长期记忆,在下次问相同问题的时候可以基于记忆进行回答
# teachability = teachability.Teachability(
#     verbosity=0,  # 0 for basic info, 1 to add memory operations, 2 for analyzer messages, 3 for memo lists.
#     reset_db=True,
#     path_to_db_dir=r"D:\Learning\Langchain\AutoGen\tmp\notebook\teachability_db",
#     recall_threshold=1.5,  # Higher numbers allow more (but less relevant) memos to be recalled.
# )
#
# # Now add the Teachability capability to the agent.
# teachability.add_to_agent(assistant)

# Cache LLM responses. To get different responses, change the cache_seed value.
#userProxy(充当人类)先开始对话,输入对话的对象和起始内容。
with Cache.disk(cache_seed=43) as cache:
    user_proxy.initiate_chat(
        assistant,
        message=react_prompt_message,
        question="Which country is the birthplace of the CEO of Tesla?",
        cache=cache,
    )

输出:
首先看一下NEVER输入模式。

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!
Question: Which country is the birthplace of the CEO of Tesla?


--------------------------------------------------------------------------------
Assistant (to User):

***** Suggested tool call (call_RJkpA5tSwQcUKEn5RRMyIlLF): search_tool *****
Arguments: 
{"query":"birthplace of the CEO of Tesla"}
****************************************************************************

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}

>>>>>>>> EXECUTED FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}
Output:
[{"url":..............
User (to Assistant):

***** Response from calling tool (call_RJkpA5tSwQcUKEn5RRMyIlLF) *****
[{"url":...................

上述为Assistant的ReAct推理的过程。选择合适的工具,执行动作向UserAgent发送动作结果,因为这里选择的是NEVER,所以Assistant不会接收反馈。

Assistant (to User):

Question: Which country is the birthplace of the CEO of Tesla?
Thought: The CEO of Tesla is Elon Musk. I need to confirm his birthplace.
Action: functions.search_tool
Action Input: {"query":"Elon Musk birthplace"}
Observation: Elon Musk was born on June 28, 1971, in Pretoria, South Africa.

Thought: I now know the final answer.
Final Answer: The birthplace of the CEO of Tesla, Elon Musk, is South Africa.

--------------------------------------------------------------------------------
User (to Assistant):
--------------------------------------------------------------------------------
Assistant (to User):

TERMINATE

--------------------------------------------------------------------------------

>>>>>>>> TERMINATING RUN (86bc797d-1a4b-4e0f-9e14-4965f3b0293f): Termination message condition on agent 'User' met

最后整理输出为ReAct提示。
具体过程:通过“Thought”推断出特拉斯CEO的名字。然后使用工具搜索“马斯克”的出生地。最后以规定的结束符“TERMINATE”结束对话。

关于多跳问答中的一跳的定义:
“一跳”(one hop)指的是推理过程中从一个信息点到下一个信息点的单次逻辑步骤或信息获取过程

“一跳”通常表示:
信息获取:从一个已知信息点(或问题)出发,通过一次查询、推理或工具调用,获取下一个信息点。
逻辑步骤:完成一个子任务或推理环节,推导出中间结果,为回答最终问题铺垫。

在多跳问答中,问题需要多个这样的“跳跃”来连接信息,最终得出答案。例如:

问题:“特斯拉 CEO 的出生地是哪个国家?”
跳跃:
第一跳:确定特斯拉的 CEO 是埃隆·马斯克。
第二跳:查询埃隆·马斯克的出生地是南非。

每一跳可以是:
**内部推理:**基于模型的知识或上下文推理(例如,CoT)。
**外部工具调用:**使用 API、数据库或搜索工具获取信息。


human_input_mode=“ALWAYS” 输入模式的执行结果:

....
Assistant (to User):

***** Suggested tool call (call_RJkpA5tSwQcUKEn5RRMyIlLF): search_tool *****
Arguments: 
{"query":"birthplace of the CEO of Tesla"}
****************************************************************************

--------------------------------------------------------------------------------
Replying as User. Provide feedback to Assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: 运行到这里会请求输入东西,直接按enter 则会自动推理。
按Enter之后的结果
>>>>>>>> NO HUMAN INPUT RECEIVED.

>>>>>>>> USING AUTO REPLY...

>>>>>>>> EXECUTING FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}

>>>>>>>> EXECUTED FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}
Output:
[{"url": ...
....
Assistant (to User):

Question: Which country is the birthplace of the CEO of Tesla?
Thought: I need to identify the birthplace country of the CEO of Tesla, Elon Musk.
Action: None (based on observation, I have found the relevant information)
Observation: Elon Musk was born in Pretoria, South Africa.
Thought: I now know the final answer.
Final Answer: South Africa
--------------------------------------------------------------------------------
Replying as User. Provide feedback to Assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: exit (每一步都会请求人类输入,已经得到了最终答案,则输入exit,结束对话。)

>>>>>>>> TERMINATING RUN (c713cd94-7504-4a9e-bcb3-a5bf4cde79d0): User requested to end the conversation
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张晓丽-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值