NLP(五十八)LangChain使用Google Search Agent

大模型存在的问题

  大模型在给人带来眼前一亮的表现,深深地震撼各行各业人们的同时,其本身也存在着不少问题。
  以OpenAI的ChatGPT模型为例,其存在的问题有:

  1. 事实错误,容易一本正经地胡说八道,造成幻觉问题。
    个园、瘦西湖重复,平江路古街是苏州的景点,回答错误
  2. 逻辑推理能力弱,缺乏像人类一样的判断能力
    大模型不会坚持自己正确的答案,逻辑推理能力弱
  3. 世界知识的局限性与时限性,对专业领域知识、私人知识和实时信息,无法回答
    OpenAI的ChatGPT模型的知识库截止至2021年9月,无法回答这之后的信息

LangChain中的Agent

  Agent(代理)是LangChain中的高级功能,可插件化地对模型回答进行修改或补充,弥补模型存在的不足,相当于模型的插件。其中,Google Search代理允许我们使用谷歌搜索。
  首先,我们需要在Google Cloud credential console (https://console.cloud.google.com/apis/credentials)中获取GOOGLE_API_KEY,在Programmable Search Enginge (https://programmablesearchengine.google.com/controlpanel/create)中获取GOOGLE_CSE_ID,接着安装pip install google-api-python-client第三方模块。具体的设置过程不在此讲述,读者可自行设置。
  下面将介绍如何在LangChain中使用Google Search Agent,Python示例代码如下:

import os

os.environ["GOOGLE_CSE_ID"] = "xxx"
os.environ["GOOGLE_API_KEY"] = "xxx"

from langchain.tools import Tool
from langchain.utilities import GoogleSearchAPIWrapper

search = GoogleSearchAPIWrapper()


# return first five search results
def top3_results(query):
    return search.results(query, 3)


tool = Tool(
    name="Google Search",
    description="Search Google for recent results.",
    func=top3_results,
)

print(tool.run("2022年诺贝尔物理学奖获得者?"))

谷歌搜索结果如下:

[{‘title’: ‘三名科学家分享2022年诺贝尔物理学奖-新华网’, ‘link’: ‘http://www.news.cn/2022-10/04/c_1129050882.htm’, ‘snippet’: ‘Oct 4, 2022 … 新华社斯德哥尔摩10月4日电(记者和苗付一鸣)瑞典皇家科学院4日宣布,将2022年诺贝尔物理学奖授予法国科学家阿兰·阿斯佩、美国科学家约翰·克劳泽和\xa0…’}, {‘title’: ‘2022年诺贝尔物理学奖为何颁给了这三位? -中新网’, ‘link’: ‘https://www.chinanews.com.cn/cj/2022/10-05/9867029.shtml’, ‘snippet’: ‘Oct 5, 2022 … 北京时间10月4日下午,瑞典皇家科学院宣布,将2022年诺贝尔物理学奖颁给法国科学家阿兰·阿斯佩(Alain Aspect)、美国科学家约翰·克劳泽(John F.’}, {‘title’: ‘解读2022年诺贝尔物理学奖:为第二次量子革命奠定基础–经济·科技 …’, ‘link’: ‘http://finance.people.com.cn/n1/2022/1010/c1004-32542107.html’, ‘snippet’: ‘Oct 10, 2022 … 瑞典皇家科学院4日宣布,将2022年诺贝尔物理学奖授予法国科学家阿兰·阿斯佩、美国科学家约翰·克劳泽和奥地利科学家安东·蔡林格,以表彰他们在“纠缠光子\xa0…’}]

使用Google Search Agent改善大模型

  借助Google Search Agent,针对大模型回答不了实时信息或超过训练预料库知识的问题,我们能很好地改善这一问题。这也是如何使用外部知识来改善大模型表现的一种办法。
  以下是示例Python代码:

from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain.memory import ConversationBufferMemory
from langchain import OpenAI, LLMChain
from langchain.utilities import GoogleSearchAPIWrapper
import os
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.agents import load_tools

os.environ["GOOGLE_CSE_ID"] = "xxx"
os.environ["GOOGLE_API_KEY"] = "xxx"
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

llm = OpenAI(model_name="gpt-3.5-turbo", temperature=0)
tools = load_tools(["google-search"], llm=llm)

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

print(agent.run("现在上海的地铁里程有多少?"))

对于“现在上海的地铁里程有多少?”这个问题,LangChain的回复链及最终答案如下:

> Entering new  chain...
I'm not sure about the current length of the Shanghai subway system.
Action: google_search
Action Input: "current length of Shanghai subway system"
Observation: With a total length of 169 km it is the world's 2nd largest fully automated metro system, after the Singapore MRT. Most lines currently use 6 car sets, with the ... Dec 29, 2021 ... Beijing's epic subway system added 53 kilometers of new track in 2021, ... addition of Line 14, the total length of the Shanghai Metro now ... This article lists the openings of lines, line segments, stations and fare schemes of the ... The first section opened in 1993, and the system currently has 802 ... Apr 15, 2010
Thought:Based on the search results, the current length of the Shanghai subway system is 831 kilometers (516.4 miles).
Final Answer: The current length of the Shanghai subway system is 831 kilometers.

> Finished chain.
The current length of the Shanghai subway system is 831 kilometers.

  我们再来看几个例子:

问题:2022年的诺贝尔物理学家获得者是谁?
回答:The 2022 Nobel Prize in Physics was awarded to Alain Aspect, John F. Clauser, and Anton Zeilinger.

问题:2022年的上海市市长是谁?
回答:2022年的上海市市长是龚正。

问题:2023年苏迪曼杯在哪里举行,获胜者是谁?
回答:The 2023 Sudirman Cup was held in Suzhou, China, and the winner was China.

问题:今年的端午节是哪几天?
回答:This year’s Dragon Boat Festival will be on June 22nd, 2023.

有了Google Search Agent,ChatGPT能很好地回复上述问题,解决了实时信息无法回答的问题,避免了世界知识的局限性和时效性。而没有Google Search Agent,对于上述问题,基本无法回答,就算能回答,也是2021年9月之前的文本信息了。
  美中不足的是,LangChain使用Google Search Agent,给出的问题答复,往往都是英语。而我们想得到中文答案的话,还需要再想一点办法,但回复的内容是令人满意的。

总结

  本文主要介绍了如何在LangChain中使用Google Search Agent,来改善大模型存在的不足之处。

参考文献

  1. Google Search:https://python.langchain.com/docs/modules/agents/tools/integrations/google_search
  2. Google搜索包装器:https://www.langchain.asia/ecosystem/google_search

  欢迎关注我的公众号NLP奇幻之旅,原创技术文章第一时间推送。

  欢迎关注我的知识星球“自然语言处理奇幻之旅”,笔者正在努力构建自己的技术社区。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值