大模型从入门到应用——LangChain:代理(Agents)-[代理执行器(Agent Executor):结合使用Agent和VectorStore]

2023-09-17 23:12:39

分类目录:《大模型从入门到应用》总目录


代理执行器接受一个代理和工具,并使用代理来决定调用哪些工具以及以何种顺序调用。本文将参数如何结合使用Agent和VectorStore。这种用法是将数据加载到VectorStore中,并希望以Agent的方式与之进行交互。

推荐的方法是创建一个RetrievalQA,然后将其作为整体Agent中的工具来使用。让我们在下面看一下如何实现,我们可以使用多个不同的vectordbs,将Agent作为它们之间的路由器。有两种不同的方法可以实现这一点:

  • 让Agent像正常工具一样使用vectorstores
  • 设置return_direct=True来将Agent真正用作路由

创建VectorStore

from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
llm = OpenAI(temperature=0)
from pathlib import Path
relevant_parts = []
for p in Path(".").absolute().parts:
    relevant_parts.append(p)
    if relevant_parts[-3:] == ["langchain", "docs", "modules"]:
        break
doc_path = str(Path(*relevant_parts) / "state_of_the_union.txt")
from langchain.document_loaders import TextLoader
loader = TextLoader(doc_path)
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings, collection_name="state-of-union")

日志输出:

Running Chroma using direct local API.
Using DuckDB in-memory for database. Data will be transient.
state_of_union = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=docsearch.as_retriever())

输入:

from langchain.document_loaders import WebBaseLoader
loader = WebBaseLoader("https://beta.ruff.rs/docs/faq/")
docs = loader.load()
ruff_texts = text_splitter.split_documents(docs)
ruff_db = Chroma.from_documents(ruff_texts, embeddings, collection_name="ruff")
ruff = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=ruff_db.as_retriever())

日志输出:

Running Chroma using direct local API.
Using DuckDB in-memory for database. Data will be transient.

创建代理

# Import things that are needed generically
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.tools import BaseTool
from langchain.llms import OpenAI
from langchain import LLMMathChain, SerpAPIWrapper
tools = [
    Tool(
        name = "State of Union QA System",
        func=state_of_union.run,
        description="useful for when you need to answer questions about the most recent state of the union address. Input should be a fully formed question."
    ),
    Tool(
        name = "Ruff QA System",
        func=ruff.run,
        description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question."
    ),
]
# Construct the agent. We will use the default agent type here.
# See documentation for a full list of options.
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("What did biden say about ketanji brown jackson in the state of the union address?")

日志输出:

Entering new AgentExecutor chain...
I need to find out what Biden said about Ketanji Brown Jackson in the State of the Union address.
Action: State of Union QA System
Action Input: What did Biden say about Ketanji Brown Jackson in the State of the Union address?
Observation:  Biden said that Jackson is one of the nation's top legal minds and that she will continue Justice Breyer's legacy of excellence.
Thought:I now know the final answer
Final Answer: Biden said that Jackson is one of the nation's top legal minds and that she will continue Justice Breyer's legacy of excellence.

Finished chain.

输出:

"Biden said that Jackson is one of the nation's top legal minds and that she will continue Justice Breyer's legacy of excellence."

输入:

agent.run("Why use ruff over flake8?")

输出:

Entering new AgentExecutor chain...
I need to find out the advantages of using ruff over flake8
Action: Ruff QA System
Action Input: What are the advantages of using ruff over flake8?
Observation:  Ruff can be used as a drop-in replacement for Flake8 when used (1) without or with a small number of plugins, (2) alongside Black, and (3) on Python 3 code. It also re-implements some of the most popular Flake8 plugins and related code quality tools natively, including isort, yesqa, eradicate, and most of the rules implemented in pyupgrade. Ruff also supports automatically fixing its own lint violations, which Flake8 does not.
Thought:I now know the final answer
Final Answer: Ruff can be used as a drop-in replacement for Flake8 when used (1) without or with a small number of plugins, (2) alongside Black, and (3) on Python 3 code. It also re-implements some of the most popular Flake8 plugins and related code quality tools natively, including isort, yesqa, eradicate, and most of the rules implemented in pyupgrade. Ruff also supports automatically fixing its own lint violations, which Flake8 does not.

Finished chain.

输出:

'Ruff can be used as a drop-in replacement for Flake8 when used (1) without or with a small number of plugins, (2) alongside Black, and (3) on Python 3 code. It also re-implements some of the most popular Flake8 plugins and related code quality tools natively, including isort, yesqa, eradicate, and most of the rules implemented in pyupgrade. Ruff also supports automatically fixing its own lint violations, which Flake8 does not.'

仅将Agent用作路由器

如果我们打算将Agent用作路由,并且只想直接返回RetrievalQAChain的结果,我们还可以设置return_direct=True

需要注意的是,在上面的示例中,Agent在查询RetrievalQAChain之后还做了一些额外的工作,我们可以避免这样做,直接返回结果。

tools = [
    Tool(
        name = "State of Union QA System",
        func=state_of_union.run,
        description="useful for when you need to answer questions about the most recent state of the union address. Input should be a fully formed question.",
        return_direct=True
    ),
    Tool(
        name = "Ruff QA System",
        func=ruff.run,
        description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question.",
        return_direct=True
    ),
]
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("What did biden say about ketanji brown jackson in the state of the union address?")

日志输出:

Entering new AgentExecutor chain...
I need to find out what Biden said about Ketanji Brown Jackson in the State of the Union address.
Action: State of Union QA System
Action Input: What did Biden say about Ketanji Brown Jackson in the State of the Union address?
Observation:  Biden said that Jackson is one of the nation's top legal minds and that she will continue Justice Breyer's legacy of excellence.


Finished chain.

输出:

" Biden said that Jackson is one of the nation's top legal minds and that she will continue Justice Breyer's legacy of excellence."

输入:

agent.run("Why use ruff over flake8?")

日志输出:

Entering new AgentExecutor chain...
I need to find out the advantages of using ruff over flake8
Action: Ruff QA System
Action Input: What are the advantages of using ruff over flake8?
Observation:  Ruff can be used as a drop-in replacement for Flake8 when used (1) without or with a small number of plugins, (2) alongside Black, and (3) on Python 3 code. It also re-implements some of the most popular Flake8 plugins and related code quality tools natively, including isort, yesqa, eradicate, and most of the rules implemented in pyupgrade. Ruff also supports automatically fixing its own lint violations, which Flake8 does not.


Finished chain.

输出:

' Ruff can be used as a drop-in replacement for Flake8 when used (1) without or with a small number of plugins, (2) alongside Black, and (3) on Python 3 code. It also re-implements some of the most popular Flake8 plugins and related code quality tools natively, including isort, yesqa, eradicate, and most of the rules implemented in pyupgrade. Ruff also supports automatically fixing its own lint violations, which Flake8 does not.'

多跳向量存储推理

由于vectorstores可以很容易地作为Agent中的工具使用,因此可以轻松使用现有的Agent框架回答依赖于vectorstores的多跳问题。

tools = [
    Tool(
        name = "State of Union QA System",
        func=state_of_union.run,
        description="useful for when you need to answer questions about the most recent state of the union address. Input should be a fully formed question, not referencing any obscure pronouns from the conversation before."
    ),
    Tool(
        name = "Ruff QA System",
        func=ruff.run,
        description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question, not referencing any obscure pronouns from the conversation before."
    ),
]
# Construct the agent. We will use the default agent type here.
# See documentation for a full list of options.
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
agent.run("What tool does ruff use to run over Jupyter Notebooks? Did the president mention that tool in the state of the union?")

日志输出:

Entering new AgentExecutor chain...
I need to find out what tool ruff uses to run over Jupyter Notebooks, and if the president mentioned it in the state of the union.
Action: Ruff QA System
Action Input: What tool does ruff use to run over Jupyter Notebooks?
Observation:  Ruff is integrated into nbQA, a tool for running linters and code formatters over Jupyter Notebooks. After installing ruff and nbqa, you can run Ruff over a notebook like so: > nbqa ruff Untitled.ipynb
Thought:I now need to find out if the president mentioned this tool in the state of the union.
Action: State of Union QA System
Action Input: Did the president mention nbQA in the state of the union?
Observation:  No, the president did not mention nbQA in the state of the union.
Thought:I now know the final answer.
Final Answer: No, the president did not mention nbQA in the state of the union.

Finished chain.

输出:

'No, the president did not mention nbQA in the state of the union.'

参考文献:
[1] LangChain官方网站:https://www.langchain.com/
[2] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/
[3] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

更多推荐

基于Java旅游管理系统设计实现(源码+lw+部署文档+讲解等)

博主介绍:✌全网粉丝30W+,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌🍅文末获取源码联系🍅👇🏻精彩专栏推荐订阅👇🏻不然下次找不到哟2022-2024年最全的计算机软件毕业设计选题

python经典百题之最大公约数与最小公倍数

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。方法1:辗转相除法(欧几里德算法)求最大公约数defgcd_euclidean(m,n):whilen:m,n=n,m%nreturnmm=36n=48gcd_result=gcd_euclidean(m,n)print("GCD:",gcd_result)#计算

echarts-可视化地图防重叠文本框

我在第一篇可视化地图中,有一些基础介绍,本篇文章就是多展示一些效果,大家可以按需获取。先直接上效果图这里的配置项有用到1、通过geo展示多层地图,这样可以像上图所示,通过错位有了一些3D效果;2、北京的特殊图标展示通过scatter类型实现;3、区域散点图effectScatter类型;4、有方向流动的线,lines类

Vue2023 面试归纳及复习(2)

1vue3中的动态组件和KeepAlive组件动态组件component<component>动态组件是一种可以根据数据变化而动态加载不同组件的方式。使用动态组件可以有效地减少代码复杂度,提高组件的复用性和灵活性。动态组件通过一个特殊的属性is来实现动态加载,is的值可以是组件的名称或组件对象。KeepAliveKee

设计模式-责任链模式

“单一职责原则”要求一个类仅负责的一个不可分业务逻辑,但这并不意味着能够实现这部分业务逻辑的只能有一个类,业务逻辑可能是会因运行时数据而选择不同类。比如在日常工作中,请假审批可能受请假天数、请假类型等因素影响,而须由不同领导来负责审批。再比如在银行取钱时,取钱业务审批申请可能会受到你所取钱总数、存储类型等因素影响,而须

node 之 express 框架(初级)

一、express热更新1、安装扩展npminstallnode-dev-D2、在根目录下的package.json文件中进行配置3、之后的启动执行下面的命令即可npmrundev二、mvc中的模板引擎1、ejs模板引擎的安装npminstallejs-s2、在根目录下的app.js文件中配置app.set('view

【计算机毕业设计】基于SpringBoot+Vue网络云端日记本系统的设计与实现

博主主页:一季春秋博主简介:专注Java技术领域和毕业设计项目实战、Java、微信小程序、安卓等技术开发,远程调试部署、代码讲解、文档指导、ppt制作等技术指导。主要内容:毕业设计(Java项目、小程序等)、简历模板、学习资料、面试题库、技术咨询。🍅文末获取联系🍅精彩专栏推荐订阅👇🏻👇🏻不然下次找不到哟Sp

C#回调函数学习1

回调函数(CallbackFunction)是一种函数指针,它指向的是由用户自己定义的回调函数。我们将这个回调函数的指针作为参数传递给另外一个函数,在这个函数工作完成后,它将通过这个回调函数的指针来回调通知调用者处理结果。此定义来自网上;看一个例子;这是用委托实现的回调函数;usingSystem;usingSyste

懒人福音!数据显示:剧烈运动5分钟即可预防癌症,风险降低30%

剧烈的间歇性生活方式体育活动,简称VILPA,是由悉尼大学查尔斯·珀金斯中心的研究人员创造的,用来描述我们每天津津有味地进行的非常短暂的活动爆发,每次大约一分钟。这包括剧烈的家务、在杂货店周围大量购物、爆发力量行走或与孩子们玩高能量游戏等活动。早在,2022年12月8日,澳大利亚悉尼大学、英国伦敦大学的研究人员在Nat

ipv6笔记及总结

1、路由器请求消息RouterSolicitation和路由器通告RouterAdvertisement消息主要用于无状态地址的情况下,有状态的情况使用的是dhcpv6server分配(例如:IPv6地址以及其他信息(DNS、域名等))。2、关于IPv6自动配置及M、O比特的小结那么对于自动配置来说,主机在什么情况下,

25 WEB漏洞-XSS跨站之原理分类及攻击手法

目录XSS跨站漏洞产生原理,危害,特点?本质产生层面函数类漏洞操作对应层,危害影响浏览器内核版本等XSS跨站漏洞分类:反射,存储,DOMXSS常规攻击手法:平台,工具,结合其他等演示案例:XSS平台及工具简要演示测试XSS跨站漏洞产生原理,危害,特点?本质跨站跟我们语言上面是没有什么关系,大部分都是前端的一个漏洞在变量

热文推荐