侧边栏壁纸
博主头像
Zeeland

全栈算法工程师 | 大模型创业 | 开源项目分享 | Python开发者 | @Promptulate Founder | @SparkLab cofounder | @LangChainAI Top Contributor | @CogitLab core member

  • 累计撰写 61 篇文章
  • 累计创建 47 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

【GPT专题】Prompt Engineering高级用法-如何让GPT使用搜索引擎

Zeeland
2023-06-09 / 0 评论 / 0 点赞 / 403 阅读 / 7,093 字

前言

本文将从Prompt Engineering的背景入手,介绍如何使用LangChain让GPT访问搜索引擎,搜索你想要的内容。通过本文,你将学习到:

  • 如何更高级地进行Prompt
  • 如何让GPT访问搜索引擎
  • 如何使用LangChain访问搜索引擎

背景

如果你用过GPT系列的产品,你一定想过这些问题:

  • 能否让GPT使用外部工具?
  • 怎么让ChatGPT连接使用搜索引擎?
  • 我的Prompt应该怎么写GPT才能更好地产出我想要的内容?

为什么需要使用外部工具?

一方面使用工具是具有智能的表现之一,另一方面虽然人类拥有智能,但并不擅长所有的任务,例如,准确计算大量数字或快速查询大量文档都是相对较为困难的任务。对于 ChatGPT 这样的大型语言模型也是如此,它虽然具备强大的语言处理能力,但并不擅长进行符号计算、实时信息获取或代码的执行。

因此,正是由于ChatGPT在某些方面表现不加的,所以我们需要让ta学会使用外部工具,通过外部工具的加成,让ta可以更加强大。

除此之外,我们需要认识到 ChatGPT 的真正优势所在。它的强大之处并不在于总能够给出准确无误的答案,相反,ChatGPT 有时会说一些无关紧要的话。ChatGPT 真正的优势在于其能够清晰地理解以自然语言给出的指令。因此,基于这一特性,我们可以让 ChatGPT 充当中央控制器,自动查找适合的工具,并在适当的时候使用它们。例如,当 ChatGPT 需要进行数字计算时,我们可以让它自动调用计算器程序进行辅助计算。同样地,当 ChatGPT 无法实时获取最新消息和信息时,我们可以让它使用搜索引擎搜索实时新闻和信息。另外,如果 ChatGPT 无法模拟运行非常复杂的代码,我们可以让它调用相应的解释器、编译器或虚拟机。

熟悉强化学习的读者可以看出,当 ChatGPT 使用外部工具时,我们可以将其视为强化学习中的智能体 agent,通过与 环境 交互以实现任务。

如何使用外部工具?

下图为ChatGPT使用外部工具的整体思路。

image.png

首先,我们需要设计一个提示,这个提示包含了解决问题的步骤和工具的使用。接着我们提出我们要解决的问题,将提示和问题一起给到 ChatGPT。

然后 ChatGPT 会开始生成文本。其中会出现使用某个工具的 token。例如 搜索(三体的作者是谁) 。当出现了使用工具的 token 后,ChatGPT 停止文本生成。根据事先写好的接口开始使用工具。例如调用搜索引擎的接口搜索 三体的作者是谁。

接着工具的结果返回给 ChatGPT ,并和前文的内容组成新的提示(Prompt)发送给 ChatGPT。

最后 ChatGPT 根据新的提示(Prompt)再次生成文本,开始重复前面的步骤。不断重复直到 ChatGPT 停止使用工具得到最终的答案。

后面,我们讲解两个经典的使用外部工具的例子,分别是 ReAct 框架和 self-ask 框架,它们都属于使用工具的Prompt Enginerring。但是在开始之前,我们先介绍一些简单的结构化Prompt Engineering的例子。

结构化Prompt

通过结构化Prompt,你可以让Prompt按照特定格式输出,一种经典的Prompt构成是:先描述这个任务,然后说明需要怎样的输出,最后跟上需要处理的内容。

[任务描述]
[输出格式]
【用户输入】
你的任务是从下面给出的一句话中提取出用户想生成随机数的数量、最大值、最小值和原因。

用户提到roll点、掷骰子等,都指的是生成随机数。
用户可能会使用类似“数量+D+最大值”的方式描述,例如:“3D6”指生成3个最大值是6,最小值是1的随机数。

你需要生成如下的结果:
count: 要生成随机数的数量
max: 最大值
min: 最小值
reason: 这次生成随机数的原因

下面是你需要处理的文本:
【用户输入】

至此,GPT可以简单的进行结构化的输出。
image.png

人设Prompt

比起用“我”和“你”这样二元对话的模式时GPT-3.5模型会一直保持自己“高冷的AI语言模型”的人设,引入另一个角色可以覆盖这个人设,产生更优质的结果。

我希望你扮演Kanibot。Kanibot是一个用于即时通讯群组的聊天机器人,热心而有趣,经常会发表自己的见解。

Kanibot在聊天之外,还提供一些功能:

- Kanibot可以在群组中播放音乐。
- Kanibot可以禁言或解禁某位聊天参与者。
- Kanibot可以随机生成一张用户卡片。

Kanibot在回复时,只会给出需要执行的指令和参数,并将自己说的话全部放置在参数的comment中。
除此之外不会输出其他信息。

ReAct

ReAct是Reasoning和Acting的缩写。这个框架的基本思路是给一个Prompt,这个Prompt将Question拆解成几个步骤。分别是:

  • Tought: 面对这个 Question 我下一步应该做什么。
  • Action:执行某个动作。在 ReAct 里有三种动作,第一个是 Search[entity] 如果存在对应实体的维基页面,则返回前5句话,否则使用维基百科搜索引擎搜索前5个类似的实体;第二个是 Look[sring] 它将返回包含该字符串的页面中的下一句话,模拟浏览器上的 Ctrl+F 功能。第三个是 Finish[answer] 它将使用答案完成当前任务。
  • Observation:观察到的外部工具给到的结果,将作为新的提示输入给 ChatGPT。

在 LangChain 中 ReAct 框架的提示如下:

Question: What is the elevation range for the area that the eastern sector of the
Colorado orogeny extends into?
Thought 1: I need to search Colorado orogeny, find the area that the eastern sector
of the Colorado orogeny extends into, then find the elevation range of the
area.
Action 1: Search[Colorado orogeny]
Observation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in
Colorado and surrounding areas.
Thought 2: It does not mention the eastern sector. So I need to look up eastern
sector.
Action 2: Lookup[eastern sector]
Observation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called
the Central Plains orogeny.
Thought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I
need to search High Plains and find its elevation range.
Action 3: Search[High Plains]
Observation 3: High Plains refers to one of two distinct land regions
Thought 4: I need to instead search High Plains (United States).
Action 4: Search[High Plains (United States)]
Observation 4: The High Plains are a subregion of the Great Plains. From east to west, the
High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130
m).[3]
Thought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer
is 1,800 to 7,000 ft.
Action 5: Finish[1,800 to 7,000 ft]

Question: Musician and satirist Allie Goertz wrote a song about the "The Simpsons"
character Milhouse, who Matt Groening named after who?
Thought 1: The question simplifies to "The Simpsons" character Milhouse is named after
who. I only need to search Milhouse and find who it is named after.
Action 1: Search[Milhouse]
Observation 1: Milhouse Mussolini Van Houten is a recurring character in the Fox animated
television series The Simpsons voiced by Pamela Hayden and created by Matt
Groening.
Thought 2: The paragraph does not tell who Milhouse is named after, maybe I can look up
"named after".
Action 2: Lookup[named after]
Observation 2: (Result 1 / 1) Milhouse was named after U.S. president Richard Nixon, whose
middle name was Milhous.
Thought 3: Milhouse was named after U.S. president Richard Nixon, so the answer is
Richard Nixon.
Action 3: Finish[Richard Nixon]

Question: Which documentary is about Finnish rock groups, Adam Clayton Powell or The
Saimaa Gesture?
Thought 1: I need to search Adam Clayton Powell and The Saimaa Gesture, and find which
documentary is about Finnish rock groups.
Action 1: Search[Adam Clayton Powell]
Observation 1 Could not find [Adam Clayton Powell]. Similar: [’Adam Clayton Powell
III’, ’Seventh Avenue (Manhattan)’, ’Adam Clayton Powell Jr. State Office
Building’, ’Isabel Washington Powell’, ’Adam Powell’, ’Adam Clayton Powell
(film)’, ’Giancarlo Esposito’].
Thought 2: To find the documentary, I can search Adam Clayton Powell (film).
Action 2: Search[Adam Clayton Powell (film)]
Observation 2: Adam Clayton Powell is a 1989 American documentary film directed by
Richard Kilberg. The film is about the rise and fall of influential
African-American politician Adam Clayton Powell Jr.[3][4] It was later aired
as part of the PBS series The American Experience.
Thought 3: Adam Clayton Powell (film) is a documentary about an African-American
politician, not Finnish rock groups. So the documentary about Finnish rock
groups must instead be The Saimaa Gesture.
Action 3: Finish[The Saimaa Gesture]

Question: What profession does Nicholas Ray and Elia Kazan have in common?
Thought 1: I need to search Nicholas Ray and Elia Kazan, find their professions, then
find the profession they have in common.
Action 1: Search[Nicholas Ray]
Observation 1: Nicholas Ray (born Raymond Nicholas Kienzle Jr., August 7, 1911 - June 16,
1979) was an American film director, screenwriter, and actor best known for
the 1955 film Rebel Without a Cause.
Thought 2: Professions of Nicholas Ray are director, screenwriter, and actor. I need
to search Elia Kazan next and find his professions.
Action 2: Search[Elia Kazan]
Observation 2: Elia Kazan was an American film and theatre director, producer, screenwriter
and actor.
Thought 3: Professions of Elia Kazan are director, producer, screenwriter, and actor.
So profession Nicholas Ray and Elia Kazan have in common is director,
screenwriter, and actor.
Action 3: Finish[director, screenwriter, actor]

Question: Which magazine was started first Arthur’s Magazine or First for Women?
Thought 1: I need to search Arthur’s Magazine and First for Women, and find which was
started first.
Action 1: Search[Arthur’s Magazine]
Observation 1: Arthur’s Magazine (1844-1846) was an American literary periodical published
in Philadelphia in the 19th century.
Thought 2: Arthur’s Magazine was started in 1844. I need to search First for Women
next.
Action 2: Search[First for Women]
Observation 2: First for Women is a woman’s magazine published by Bauer Media Group in the
USA.[1] The magazine was started in 1989.
Thought 3: First for Women was started in 1989. 1844 (Arthur’s Magazine) < 1989 (First
for Women), so Arthur’s Magazine was started first.
Action 3: Finish[Arthur’s Magazine]

Question: Were Pavel Urysohn and Leonid Levin known for the same type of work?
Thought 1: I need to search Pavel Urysohn and Leonid Levin, find their types of work,
then find if they are the same.
Action 1: Search[Pavel Urysohn]
Observation 1: Pavel Samuilovich Urysohn (February 3, 1898 - August 17, 1924) was a Soviet
mathematician who is best known for his contributions in dimension theory.
Thought 2: Pavel Urysohn is a mathematician. I need to search Leonid Levin next and
find its type of work.
Action 2: Search[Leonid Levin]
Observation 2: Leonid Anatolievich Levin is a Soviet-American mathematician and computer
scientist.
Thought 3: Leonid Levin is a mathematician and computer scientist. So Pavel Urysohn
and Leonid Levin have the same type of work.
Action 3: Finish[yes]


Question: Author David Chanoff has collaborated with a U.S. Navy admiral who served as the ambassador to the United Kingdom under which President?其中,前面部分属于少样本学习中的示例,目的是教会 ChatGPT 如何使用工具。最后一行的 Question 就是我们要问 ChatGPT 的问题。对于这个问题,ChatGPT 将使用外部工具解答。具体的结果如下:Thought 1: I need to search David Chanoff and the U.S. Navy admiral, find the ambassador to the United Kingdom, then find the President they served under.
Action 1: Search[David Chanoff]
Observation 1: David Chanoff is an American author and journalist. He is the co-author of
several books, including Inside the Mirage: America's Fragile Partnership
with Saudi Arabia (2004) and The Game Player: Confessions of the CIA's
Original Political Operative (1989).
Thought 2: I need to search the U.S. Navy admiral David Chanoff collaborated with.
Action 2: Search[U.S. Navy admiral]
Observation 2: Admiral Charles R. Larson was a United States Navy four-star admiral who
served as the United States Ambassador to the United Kingdom from 1998 to
2001.
Thought 3: Admiral Charles R. Larson served as the United States Ambassador to the
United Kingdom from 1998 to 2001. So the President they served under is
Bill Clinton.
Action 3: Finish[Bill Clinton]

其中,前面部分属于少样本学习中的示例,目的是教会 ChatGPT 如何使用工具。最后一行的 Question 就是我们要问 ChatGPT 的问题。对于这个问题,ChatGPT 将使用外部工具解答。具体的结果如下:

hought 1: I need to search David Chanoff and the U.S. Navy admiral, find the ambassador to the United Kingdom, then find the President they served under.
Action 1: Search[David Chanoff]
Observation 1: David Chanoff is an American author and journalist. He is the co-author of
several books, including Inside the Mirage: America's Fragile Partnership
with Saudi Arabia (2004) and The Game Player: Confessions of the CIA's
Original Political Operative (1989).
Thought 2: I need to search the U.S. Navy admiral David Chanoff collaborated with.
Action 2: Search[U.S. Navy admiral]
Observation 2: Admiral Charles R. Larson was a United States Navy four-star admiral who
served as the United States Ambassador to the United Kingdom from 1998 to
2001.
Thought 3: Admiral Charles R. Larson served as the United States Ambassador to the
United Kingdom from 1998 to 2001. So the President they served under is
Bill Clinton.
Action 3: Finish[Bill Clinton]

self-ask

self-ask 是指提出子问题的过程。其基本思想是让 ChatGPT 判断是否需要提出子问题。如果需要,ChatGPT 将自行提出子问题,先解决子问题,再解决主问题。这个思路在工作记忆一节中已经为大家演示过。但是,在这里,我们将引入一个外部工具——搜索引擎。

ChatGPT 自己提出的子问题将直接送到搜索引擎中,然后将搜索结果返回给 ChatGPT。

下面,我们将以 self-ask 原始论文中的例子来演示这个过程。

首先是给到提示(prompt):

Question: Who lived longer, Theodor Haecker or Harry Vaughan Watkins?
Are follow up questions needed here: Yes.
Follow up: How old was Theodor Haecker when he died? Intermediate answer: Theodor Haecker was 65 years old when he died.
Follow up: How old was Harry Vaughan Watkins when he died? Intermediate answer: Harry Vaughan Watkins was 69 years old when he died.
So the final answer is: Harry Vaughan Watkins.

提示中有四个要素:

  • Quesion: 要解决的问题。
  • Are follow up questions needed here: 是否要提出子问题。
  • Follow up: 如果要提出子问题,提出的子问题。
  • Intermediate answer: 来自搜索引擎的结果。注意这里只是提示。
  • So the final answer is: 如果能得出最终结论,则返回最终结论。

然后我们提出我们想解决的问题:

Question: In what year was the current tallest wooden lattice tower completed?
Are follow up questions needed here:

接着 ChatGPT 开始生成文本:

Yes.
Follow up: What is the current tallest wooden lattice tower?

停止文本生成,将子问题结果给到外部工具搜索引擎,得到结果,将结果返回到 Intermediate answer: 中。
此时的提示更新为这样:

Question: Who lived longer, Theodor Haecker or Harry Vaughan Watkins?
Are follow up questions needed here: Yes.
Follow up: How old was Theodor Haecker when he died? Intermediate answer: Theodor Haecker was 65 years old when he died.
Follow up: How old was Harry Vaughan Watkins when he died? Intermediate answer: Harry Vaughan Watkins was 69 years old when he died.
So the final answer is: Harry Vaughan Watkins.
Question: In what year was the current tallest wooden lattice tower completed?
Are follow up questions needed here: Yes.
Follow up: What is the current tallest wooden lattice tower?
Intermediate answer: Radio Tower Gliwice.

接着继续将提示给到 ChatGPT,得到生成文本:

Follow up: When was Gliwice Radio Tower completed?

停止文本生成,将子问题结果给到外部工具搜索引擎,得到结果,将结果返回到 Intermediate answer: 中。

此时的提示更新为这样:

Question: Who lived longer, Theodor Haecker or Harry Vaughan Watkins?
Are follow up questions needed here: Yes.
Follow up: How old was Theodor Haecker when he died? Intermediate answer: Theodor Haecker was 65 years old when he died.
Follow up: How old was Harry Vaughan Watkins when he died? Intermediate answer: Harry Vaughan Watkins was 69 years old when he died.
So the final answer is: Harry Vaughan Watkins.
Question: In what year was the current tallest wooden lattice tower completed?
Are follow up questions needed here: Yes.
Follow up: What is the current tallest wooden lattice tower?
Intermediate answer: Radio Tower Gliwice.
Follow up: When was Gliwice Radio Tower completed?
Intermediate answer: 1935.

然后将提示给到 ChatGPT,得到以下输出:

So the final answer is: 1935.

对话完成,得到了最终的答案。

如何调用搜索引擎?

好了,前面长篇大论介绍了Prompt Engineering相关的内容,现在终于可以开始说怎么用GPT调用搜索引擎了。在学习了self-ask,ReAct这些框架之后,我相信你的脑海里已经有一个大体的思路了,即prompt让其生成搜索关键字,再调用搜索相关的API,将结果返回给GPT,让其进行分析,最后在让其按照特定的格式输出。

在这种思路下,Prompt Engineering的框架你可以理解为是辅助GPT的一种框架,下面我们来简单实现一下这种思路,后面我们会使用LangChain封装好的库直接调用,这样就方便多了,演示只是为了展现其原理。

首先我们使用DuckDuckGo的搜索API,不用google API是因为其太贵了,bing的暂时没有了解过,因此下文我们使用DuckDuckGo的API

这个仓库deedy5/duckduckgo_search: Search for words, documents, images, videos, news, maps and text translation using the DuckDuckGo.com search engine. Downloading files and images to a local hard drive. (github.com) 对DuckDuckGo做了很好的封装,我们可以直接使用这个接口来跑数据。

首先,导包!

pip install -U duckduckgo_search

然后,跑一下下面的代码看看结果吧。

  
from duckduckgo_search import ddg  
  
  
def main():  
	keywords = '什么是鸡你太美?'  
	results = ddg(keywords, region='wt-wt', safesearch='Off', time='y')  
	for result in results:  
	print(result)  
	  
  
if __name__ == '__main__':  
	main()

然后,你可以看到如下的结果:

那下面的思路就很简单啦,把这些数据喂给GPT,加上ReAct框架就好了。这里先不详细演示了,和LangChain一起演示最终的效果。

使用LangChain进行外部搜索

LangChain是一个用于开发基于语言模型的应用程序开发框架。总的来说,LangChain是一个链接面向用户程序和LLM之间的一个中间层,通过LangChain,你可以方便的构建起一些NLP领域的应用,下面我们将用该框架,让GPT3.5用DuckDuckGo进行外部搜索。

from langchain.agents import load_tools  
from langchain.agents import initialize_agent  
from langchain.agents import AgentType  
from langchain.llms import OpenAI  
  
# First, let's load the language model we're going to use to control the agent.  
llm = OpenAI(temperature=0)  
  
# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.  
tools = load_tools(["ddg-search"], llm=llm)  
  
  
# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.  
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)  
  
# Now let's test it out!  
agent.run("南昌明天多少度? 这个数的0.23次方是多少?我需要中文的输出结果")
 I need to find out what the temperature in Nanchang will be tomorrow
Action: DuckDuckGo Search
Action Input: "Nanchang weather tomorrow"
Observation: Today's and tonight's professional weather forecast for Nanchang. Precipitation radar, HD satellite images, and current weather warnings, hourly temperature, chance of rain, and sunshine hours. ... Tomorrow . 90 °F . 77 °F . 15 mph ... Nanchang Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for the Nanchang area. ... Tomorrow Sun 04/02 High ... Detailed weather forecast ⚡ in Nanchang, Jiangxi today, tomorrow and 7 days. Wind, precipitation, 🌡️ air temperature, clouds and atmospheric pressure - World-Weather.info. Add the current city. Search. Weather; ... Today 11 March, weather in Nanchang +75°F. Broken clouds, Light Breeze, West 5.4 mph. Atmosphere pressure 29.9 inHg. Nanchang Weather Forecast. Providing a local hourly Nanchang weather forecast of rain, sun, wind, humidity and temperature. The Long-range 12 day forecast also includes detail for Nanchang weather today. Live weather reports from Nanchang weather stations and weather warnings that include risk of thunder, high UV index and forecast gales. Nanchang real-time weather and 30 days forecast, also include air quality, precipitation, severe weather warning. Forecast ; Warning ; Air Quality ; Satellite+Radar ... Tomorrow: Cloudy, hotter than today(30°),AQI is moderate. 4KM/H NE. 50%. Humidity . Extreme. UV . 21° ...
Thought: I now know the final answer
Final Answer: Tomorrow's temperature in Nanchang will be 90°F (32°C) with a low of 77°F (25°C).

> Finished chain.
> Entering new AgentExecutor chain...
 I need to find out the temperature in Nanchang tomorrow and then calculate the 0.23 power of that number.
Action: DuckDuckGo Search
Action Input: "weather in Nanchang tomorrow"
Observation: Nanchang Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for the Nanchang area. ... Tomorrow Sun 04/02 High ... Nanchang Weather Forecast. Providing a local hourly Nanchang weather forecast of rain, sun, wind, humidity and temperature. The Long-range 12 day forecast also includes detail for Nanchang weather today. Live weather reports from Nanchang weather stations and weather warnings that include risk of thunder, high UV index and forecast gales. Nanchang real-time weather and 30 days forecast, also include air quality, precipitation, severe weather warning. Forecast ; Warning ; Air Quality ; Satellite+Radar ... Tomorrow: Cloudy, hotter than today(30°),AQI is moderate. 4KM/H NE. 50%. Humidity . Extreme. UV . 21° ... Nanchang - Weather forecast from Theweather.com. Weather conditions with updates on temperature, humidity, wind speed, snow, pressure, etc. for Nanchang, China. New York New York State 57° ... Weather Nanchang tomorrow, April 14. 5 PM : Detailed weather forecast ⚡ in Nanchang, Jiangxi today, tomorrow and 7 days. Wind, precipitation, 🌡️ air temperature, clouds and atmospheric pressure - World-Weather.info. Add the current city. Search. Weather; ... Today 11 March, weather in Nanchang +75°F. Broken clouds, Light Breeze, West 5.4 mph. Atmosphere pressure 29.9 inHg.
Thought: The temperature in Nanchang tomorrow is 30°C.
Action: Calculate
Action Input: 30^0.23
Observation: Calculate is not a valid tool, try another one.
Thought: I need to use a calculator to calculate the 0.23 power of 30.
Action: Calculator
Action Input: 30^0.23
Observation: Calculator is not a valid tool, try another one.
Thought: I need to use a mathematical tool to calculate the 0.23 power of 30.
Action: Math Tool
Action Input: 30^0.23
Observation: Math Tool is not a valid tool, try another one.
Thought: I need to use an online calculator to calculate the 0.23 power of 30.
Action: Online Calculator
Action Input: 30^0.23
Observation: Online Calculator is not a valid tool, try another one.
Thought: I need to use an online math tool to calculate the 0.23 power of 30.
Action: Online Math Tool
Action Input: 30^0.23
Observation: Online Math Tool is not a valid tool, try another one.
Thought: I need to use a scientific calculator to calculate the 0.23 power of 30.
Action: Scientific Calculator
Action Input: 30^0.23
Observation: Scientific Calculator is not a valid tool, try another one.
Thought: I need to use an online scientific calculator to calculate the 0.23 power of 30.
Action: Online Scientific Calculator
Action Input: 30^0.23
Observation: Online Scientific Calculator is not a valid tool, try another one.
Thought: I need to use an online scientific calculator to calculate the 0.23 power of 30.
Action: Online Scientific Calculator
Action Input: 30^0.23
Observation: Online Scientific Calculator is not a valid tool, try another one.
Thought: I need to use an online calculator to calculate the 0.23 power of 30.
Action: Online Calculator
Action Input: 30^0.23
Observation: Online Calculator is not a valid tool, try another one.
Thought: I need to use an online math tool to calculate the 0.23 power of 30.
Action: Online Math Tool
Action Input: 30^0.23
Observation: Online Math Tool is not a valid tool, try another one.
Thought: I need to use a calculator to calculate the 0.23 power of 30.
Action: Calculator
Action Input: 30^0.23
Observation: Calculator is not a valid tool, try another one.
Thought: I need to use a mathematical tool to calculate the 0.23 power of 30.
Action: Math Tool
Action Input: 30^0.23
Observation: Math Tool is not a valid tool, try another one.
Thought: I need to use an online calculator to calculate the 0.23 power of 30.
Action: Online Calculator
Action Input: 30^0.23
Observation: Online Calculator is not a valid tool, try another one.
Thought: I need to use an online math tool to calculate the 0.23 power of 30.
Action: Online Math Tool
Action Input: 30^0.23
Observation: Online Math Tool is not a valid tool, try another one.
Thought:
> Finished chain.

是不是有点抽象,从上面的结果可以看到,GPT确实用外部搜索搜索到了南昌明天的天气,但是进行数学运算的时候,ta却卡住了,一直尝试调用计算器,但事实上,我们并没有提供计算器,所以让ta难堪了,LangChain也提供的LLM的计算器,我们直接开箱即用就好,只需要改一行代码:

tools = load_tools(["ddg-search","llm-math"], llm=llm)

然后再运行一下,结果如下:

> Entering new AgentExecutor chain...
 I need to find out the temperature in Nanchang tomorrow and then calculate the 0.23 power of that number
Action: DuckDuckGo Search
Action Input: "Nanchang temperature tomorrow"
Observation: Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for the Nanchang area. ... Tomorrow Sun 04/02 High 83 ... Detailed weather forecast ⚡ in Nanchang, Jiangxi today, tomorrow and 7 days. Wind, precipitation, 🌡️ air temperature, clouds and atmospheric pressure - World-Weather.info ... Light Breeze, West 5.4 mph. Atmosphere pressure 29.9 inHg. Relative humidity 60%. Tomorrow's night air temperature will drop to +63°F, wind will change to North 4. ... See the links below the 12-day Nanchang weather forecast table for other cities and towns nearby along with weather conditions for local outdoor activities. Nanchang is 5 m above sea level and located at 28.68° N 115.88° E . Nanchang - Weather forecast from Theweather.com. Weather conditions with updates on temperature, humidity, wind speed, snow, pressure, etc. for Nanchang, China. New York New York State 57° ... Weather Nanchang tomorrow, April 14. 5 PM : 南昌 (Nanchang) ☀ Weather forecast for 10 days, information from meteorological stations, webcams, sunrise and sunset, wind and precipitation maps for this place ... 00:00 tomorrow 01:00 tomorrow 02:00 tomorrow 03:00 tomorrow 04:00 tomorrow 05:00 tomorrow 06:00 tomorrow; 22 °C 1 mm. 90 %. E 5 km/h: 21 °C 1 mm. 100 %. E 5 km/h: 21 °C 1 mm ...
Thought: The temperature in Nanchang tomorrow is 83 degrees
Action: Calculator
Action Input: 83^0.23
Observation: Answer: 2.763045020279681
Thought: I now know the final answer
Final Answer: 明天南昌的温度是83度,0.23次方是2.763045020279681

> Finished chain.

至此,你已经可以看到让GPT调用外部搜索或外部工具来开发一些有趣的功能了。事实上,这个功能并不稳定,比如我今天阅读了一篇《 LangChain:为你定制一个专属的GPT》的文章,想让ta总结一下,然后请欣赏ta的表演:

# Now let's test it out!  
agent.run("帮我总结一下https://zhuanlan.zhihu.com/p/623421034这篇文章的内容,我需要中文的输出结果")
> Entering new AgentExecutor chain...
 I should read the article to get a better understanding of its content
Action: DuckDuckGo Search
Action Input: https://zhuanlan.zhihu.com/p/623421034
Observation: 四、苹果笔记本电脑: 适合高端商务用户,主流用户群体(音乐、视频、编程),部分对于轻薄要求高,或者对苹果品牌比较喜欢的文科类用户。. 整体对于游戏并不友好。. 这部分产品定价比较迷,像EVO认证的高端笔记本从6000-20000都有。. 五、商务本(高端 ... 1.windows下面使用可以安装VBA支持包. 由于wps office没有内置VBA支持,必须安装VBA支持包才行能看到zotero菜单,否则显示为自定义。. 解压缩 1.wps2019vba.rar 安装压缩包里的wps2019vba.exe。. 打开WPS就应该能看到zotero的工具栏,并可以点击添加文献了。. 但是这时候每次 ... 发布于 2023-03-21 19:21 ・IP 属地上海 · 4351 次播放. 赞同 28. . 5 条评论. 分享. 收藏. 喜欢. 举报. 游戏开发 游戏 Demo GPT-4.
Thought: I now have a good understanding of the article
Final Answer: 该文章讨论了不同类型的笔记本电脑,包括游戏本、轻薄本、苹果笔记本电脑和商务本,以及如何在Windows下安装VBA支持包以使用Zotero文献管理工具。

> Finished chain.

你会发现牛头不对马嘴,我甚至不知道ta怎么得到这些信息的,因为这篇文章的链接里根本没有这些内容,所以其实问题在DuckDuckGo返回的信息有一些问题,因为其API总是不太稳定,导致duckduckgo_search总是需要更新架构,对于维护者来说很头疼,需要经常更新版本。解决方案也是有的,你可以尝试更换的其他的搜索API,如Google或者Bing的API,其准确性会更强。

总结

文本主要New Bing的工作原理,即ChatGPT如何访问调用搜索引擎,又介绍了当下最新的Prompt Technique,即ReAct、self-ask等prompt方式,最后,我们使用langchain跑了一遍代码,从api上了解llm是如何使用外部工具。

References

0

评论区