AGI围城
首页
基础知识
工程实践
所见所思
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

AGI围城

是的,这个世界还是很有趣的。
首页
基础知识
工程实践
所见所思
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 提示词工程

  • 大模型API调用

  • OpenAI工具

  • 嵌入向量

  • 检索增强生成(RAG)

  • LlamaIndex

  • LangChain

  • Agent

    • 1.Agent概述
    • 2.ReAct智能体
    • 3.SelfAskWithSearch智能体
      • 3.1 SelfAskWithSearch 智能体
      • 3.2 代码实现
      • 3.3 SelfAskWithSearch Prompt 模板
      • 3.4 执行过程
  • Workflow

  • Transformer

  • 微调

  • MCP

  • A2A

  • 基础知识
  • Agent
xiao_sl
2025-03-17
目录

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

# 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

# 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

SelfAskWithSearch 通过自我提问逐步分解问题:先问"冯小刚的老婆是谁" → 得知是徐帆 → 再问"徐帆演过哪些电影" → 得到最终答案。

编辑 (opens new window)
上次更新: 2025/12/19, 15:17:48
2.ReAct智能体
1.Workflow概述

← 2.ReAct智能体 1.Workflow概述→

最近更新
01
我是如何发现临时邮箱的?一个真实的故事
06-12
02
4.核心实现
05-26
03
3.A2A开发实践
05-22
更多文章>
Theme by Vdoing | Copyright © 2019-2025 AGI围城 | 桂ICP备2024034950号 | 桂公网安备45142202000030
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式