3.SelfAskWithSearch智能体
# 3.1 SelfAskWithSearch 智能体
SelfAskWithSearch 是一种基于自我提问和搜索的策略。代理在执行任务时,会首先自问一个问题,然后通过搜索引擎或其他工具获取相关的信息,再用这些信息回答问题。
# 3.2 代码实现
import dotenv
import os
import calendar
from langchain.agents import AgentExecutor
from langchain.agents import create_self_ask_with_search_agent
from langchain_community.utilities import SerpAPIWrapper
from langchain.tools import Tool, tool
from langchain_openai import ChatOpenAI
from langchain import hub
import dateutil.parser as parser
dotenv.load_dotenv()
serpapi_api_key = os.getenv('SERPAPI_API_KEY')
search = SerpAPIWrapper(serpapi_api_key=serpapi_api_key)
tools = [
Tool.from_function(
func=search.run,
name="Intermediate Answer", # 必须使用这个名称
description="搜素引擎",
max_results=1
),
]
# 下载一个现有的 Prompt 模板
self_ask_prompt = hub.pull("hwchase17/self-ask-with-search")
print(self_ask_prompt.template)
llm = ChatOpenAI(model='gpt-4o', temperature=0, seed=23)
# self_ask_with_search_agent 只能传一个名为 'Intermediate Answer' 的 tool
agent = create_self_ask_with_search_agent(llm, tools, self_ask_prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)
result = agent_executor.invoke({"input": "冯小刚的老婆演过哪些电影,用中文回答"})
print(result)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 3.3 SelfAskWithSearch Prompt 模板
Question: Who lived longer, Muhammad Ali or Alan Turing?
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali
Question: When was the founder of craigslist born?
Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952
Question: Who was the maternal grandfather of George Washington?
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
Question: Are both the directors of Jaws and Casino Royale from the same country?
Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate answer: New Zealand.
So the final answer is: No
Question: {input}
Are followup questions needed here:{agent_scratchpad}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 3.4 执行过程
> Entering new AgentExecutor chain...
是的。
Follow up: 冯小刚的老婆是谁?
Intermediate answer: 冯小刚的老婆是徐帆。
Follow up: 徐帆演过哪些电影?
Intermediate answer: 徐帆演过的电影包括《唐山大地震》、《手机》、《一九四二》、《非诚勿扰》等。
So the final answer is: 徐帆演过的电影包括《唐山大地震》、《手机》、《一九四二》、《非诚勿扰》等。
> Finished chain.
{'input': '冯小刚的老婆演过哪些电影,用中文回答', 'output': '徐帆演过的电影包括《唐山大地震》、《手机》、《一九四二》、《非诚勿扰》等。'}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
SelfAskWithSearch 通过自我提问逐步分解问题:先问"冯小刚的老婆是谁" → 得知是徐帆 → 再问"徐帆演过哪些电影" → 得到最终答案。
编辑 (opens new window)
上次更新: 2025/12/19, 15:17:48