6.4.6 Self-ask with search代理
自问自答结合搜索(Self-ask with search)是一种人工智能代理执行任务的方法,它通过提出问题并利用搜索结果来找到答案,这种方法特别适合解决那些需要额外信息才能解答的问题。在实际应用中,自问自答结合搜索代理的功能非常强大,因为它结合了搜索的广度和语言模型的理解能力。自问自答结合搜索代理的常用场景有:
- 事实查找:回答关于特定事件、人物、地点或数据的问题。
- 比较分析:比较不同选项或项目的优劣。
- 趋势预测:基于搜索结果预测未来的趋势或事件。
- 决策支持:提供信息帮助用户做出决策。
实现自问自答结合搜索代理的基本步骤如下所示:
(1)定义问题:首先,明确需要解决的问题。
(2)自问:代理识别出要回答这个问题所需的关键信息,并生成一个或多个辅助问题来获取这些信息。
(3)搜索:代理使用搜索工具来找到辅助问题的答案。
(4)分析结果:代理分析搜索结果,提取出有用的信息。
(5)综合信息:代理将获取到的信息综合起来,形成对原始问题的直接回答。
(6)提供答案:最后,代理将综合后的信息作为原始问题的答案提供给用户。
请看下面的实例,演示了使用Self-ask with search代理来回答问题的过程。
实例6-2:使用Self-ask with search代理回答问题(源码路径:codes\6\Self-ask.py)
实例文件Self-ask.py的具体实现代码如下所示。
from langchain_openai import ChatOpenAI
from langchain import SerpAPIWrapper
from langchain.agents import initialize_agent, Tool, AgentType
# 初始化ChatOpenAI实例
llm = ChatOpenAI(
openai_api_key='YOUR_API_KEY', # 替换为你的API密钥
base_url='https://api.chatanywhere.tech/v1',
model='gpt-3.5-turbo',
temperature=0,
)
# 初始化SerpAPIWrapper实例
search = SerpAPIWrapper(serpapi_api_key='YOUR_SERPAPI_KEY') # 替换为你的SerpAPI密钥
# 创建工具列表,包含中间答案工具
tools = [
Tool(
name="Intermediate Answer",
func=search.run,
description="useful for when you need to answer questions about current events. You should only use this when all else fails. This tool should be used only as a last resort."
)
]
# 初始化Self-Ask with Search代理
self_ask_with_search = initialize_agent(
tools,
llm,
agent=AgentType.SELF_ASK_WITH_SEARCH,
verbose=True,
handle_parsing_errors=True
)
# 运行代理并获取问题的答案
answer = self_ask_with_search.run("使用玫瑰作为国花的国家的首都是哪里?")
print(answer)
在上述代码中,搜索工具SerpAPIWrapper是用于获取搜索结果的,而ChatOpenAI则用于生成文本和回答。AgentType.SELF_ASK_WITH_SEARCH指定了代理的类型,即自问自答结合搜索的代理。上述代码的实现流程如下所示:
(1)初始化ChatOpenAI实例:创建了一个ChatOpenAI实例,用于生成文本。您需要用您自己的OpenAI API密钥替换'YOUR_API_KEY'。
(2)初始化SerpAPIWrapper实例:创建了一个SerpAPIWrapper实例,用于执行网络搜索。同样,您需要用您自己的SerpAPI密钥替换'YOUR_SERPAPI_KEY'。
(3)创建工具列表:定义了一个名为"Intermediate Answer"的工具,该工具将使用search.run方法来获取搜索结果。
(4)初始化Self-Ask with Search代理:使用initialize_agent函数初始化了一个代理,指定了工具列表、语言模型、代理类型(AgentType.SELF_ASK_WITH_SEARCH)、是否详细输出(verbose=True)以及是否处理解析错误(handle_parsing_errors=True)。
(5)运行代理并获取答案:调用self_ask_with_search.run方法并传入问题,代理将尝试找到并打印出答案。
执行后会输出:
> Entering new AgentExecutor chain...
> Initializing Self-Ask with Search agent...
> The question is: "使用玫瑰作为国花的国家的首都是哪里?"
> Identifying the need to search for the country that has the rose as its national symbol...
> Executing search using SerpAPIWrapper...
> Search results received: [List of search results with relevant information]
> Analyzing search results to determine the country with the rose as its national flower...
> Country identified: United States of America
> Generating a follow-up question to find the capital of the identified country...
> Follow-up question: "What is the capital of the United States of America?"
> Using ChatOpenAI to answer the follow-up question...
> Response from ChatOpenAI: "The capital of the United States of America is Washington, D.C."
> Integrating the answer into the final response...
> Finished chain.
Final Answer: The capital of the country that has the rose as its national flower, the United States of America, is Washington, D.C.