問題:特斯拉公司 CEO 的出生地是哪個國家?
答案:南非。
推理過程:
第一跳:確定特斯拉(Tesla, Inc.)的 CEO。特斯拉的 CEO 是埃隆·馬斯克(Elon Musk)。
第二跳:查找埃隆·馬斯克的出生地。埃隆·馬斯克出生于南非的比勒陀利亞(Pretoria, South Africa)。
使用搜索工具獲取輔助信息。
代碼:
import os
from typing import Annotatedfrom tavily import TavilyClientfrom 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, LocalCommandLineCodeExecutorconfig_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 questionBegin!
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 questionBegin!
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