Langchain 代理 (Agents) ,赋能超级 LLMs

Langchain 代理 (Agents) ,赋能超级 LLMs

原文:Langchain 代理 (Agents) ,赋能超级 LLMs - 知乎

大型语言模型(LLMs) 非常强大,但它们缺乏“最笨”的计算机程序可以轻松处理的特定能力。逻辑、计算和搜索是计算机通常擅长的领域,但 LLMs 却遇到了困难。 计算机可以解决非常复杂的数学问题,但如果我们让 GPT-4 告诉我们 4.1 * 7.9 的答案,它就失败了:

GPT 4 对简单计算的响应

要求 GPT-4 执行简单的计算通常会得到一个错误的答案。一个简单的计算器可以毫无问题地执行相同的计算。

根据一个简单的计算器,答案是 19.357,保留三位小数。一个简单的计算器程序可以做到这一点,但一个非常复杂的 AI 引擎却失败了,这是不是很有趣?

这还不是全部。如果我问 GPT-4,“我如何在 LangChain 中使用 LLMChain?”它又遇到了困难:

GPT-4 无法回答关于最近事件、技术等的问题

这里谈到的 LangChain 并不是我们所知道的 LangChain。它是一个旧的区块链项目。回答既过时又充满错误的信息。

没错。LangChain 确实是一个区块链项目 [1][2]。然而,似乎没有“ LLMChain ”组件或“ LANG 代币 ”——这两者都是幻觉。

GPT-4 无法告诉我们关于 LangChain 的信息,这是因为它与外部世界没有联系。它的唯一知识来自于它的训练数据,而训练数据在 2021 年末截止。

在当今的 LLMs 一代中存在重大缺陷,我们必须找到解决这些问题的方法。一种“解决方案套件”以“代理 (Agents) ”形式提供。

这些代理 (Agents) 不仅解决了我们上面看到的问题,还解决了 许多 其他问题。事实上,添加代理 (Agents) 在增强 LLMs 的能力方面几乎没有上限。

在本章中,我们将讨论代理 (Agents) 。我们将学习它们是什么,它们如何工作,以及如何在 LangChain 库中使用它们来为我们的 LLMs 增添超能力。


什么是代理 (Agents) ?

我们可以将代理 (Agents) 视为 LLMs 的工具 (Tools) 。就像人类使用计算器进行数学计算或在 Google 中搜索信息一样,代理 (Agents) 允许 LLM 做同样的事情。

LangChain LLM 代理 (Agents)

代理 (Agents) 是可以使用计算器、搜索或执行代码的 LLMs。

使用代理 (Agents) ,LLM 可以编写和执行 Python 代码。它可以搜索信息,甚至查询 SQL 数据库。

让我们看一个简单的例子。我们将从一个 “Zero-shot” 代理 (Agents) 开始(稍后会详细介绍),它允许我们的 LLM 使用计算器。

代理 (Agents) 和工具

要使用代理 (Agents) ,我们需要三样东西:

  • 一个基本的 LLM,
  • 我们将要进行交互的工具 Tools,
  • 一个控制交互的代理 (Agents) 。

让我们从安装 langchain 并初始化我们的基本 LLM 开始。

from langchain import OpenAIllm = OpenAI(openai_api_key="OPENAI_API_KEY",temperature=0,model_name="text-davinci-003"
)

现在初始化计算器工具。

在初始化工具 Tool 时,我们可以创建自定义工具 Tool 或加载预构建工具 Tool。

无论哪种情况,"工具 Tool" 都是一个给定工具 名称 name 和 描述 description 的 实用链。

例如,我们可以从现有的 llm_math 链创建一个新的计算器工具:

In [3]:

from langchain.chains import LLMMathChain
from langchain.agents import Toolllm_math = LLMMathChain(llm = llm)# initialize the math tool
math_tool = Tool(name ='Calculator',func = llm_math.run,description ='Useful for when you need to answer questions about math.'
)
# when giving tools to LLM, we must pass as list of tools
tools = [math_tool]

In [4]:

tools [0].name, tools [0].description

Out [4]:

('Calculator', 'Useful for when you need to answer questions about math.')

在使用自定义工具时,我们必须遵循此过程。然而,一个预构建的 llm_math 工具可以做同样的事情。所以,我们可以这样做:

In [5]:

from langchain.agents import load_toolstools = load_tools(['llm-math'],llm = llm
)

In [6]:

tools [0].name, tools [0].description

Out [6]:

('Calculator', 'Useful for when you need to answer questions about math.')

如果我们的用例存在一个预构建的工具,我们只能按照第二种方法进行。

现在我们有了 LLM 和工具,但没有 代理 (Agents) 

要初始化一个简单的代理 (Agents) ,我们可以这样做:

from langchain.agents import initialize_agentzero_shot_agent = initialize_agent(agent = "zero-shot-react-description",tools = tools,llm = llm,verbose = True,max_iterations = 3
)

这里使用的 *代理 (Agents) * 是一个 "zero-shot-react-description" 代理 (Agents) 。

Zero-shot 意味着代理 (Agents) 仅在当前操作上起作用——它没有 记忆

它使用 ReAct 框架根据工具的 描述 description 来决定使用哪个工具。

我们不会在本章中讨论 * ReAct 框架 ,但您可以将其视为 LLM 可以循环进行 * Re asoning 和 * Act ion 步骤的过程。它启用了一个多步骤的过程来识别答案。*

初始化了我们的代理 (Agents) 后,我们可以开始使用它。让我们尝试一些提示,看看代理 (Agents) 如何回应。

In [8]:

zero_shot_agent(" what is (4.5*2.1)^2.2?")

Out [8]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m I need to calculate this expression
Action: Calculator
Action Input: (4.5*2.1)^2.2 [0m
Observation: [36; 1m [1; 3mAnswer: 139.94261298333066
[0m
Thought: [32; 1m [1; 3m I now know the final answer
Final Answer: 139.94261298333066 [0m
[1m > Finished chain.[0m
{'input': 'what is (4.5*2.1)^2.2?', 'output': '139.94261298333066'}

In [9]:

(4.5 *2.1)**2.2

Out [9]:

139.94261298333066

这里的答案是正确的。让我们再试一次:

In [10]:

zero_shot_agent("if Mary has four apples and Giorgio brings two and a half apple ""boxes (apple box contains eight apples), how many apples do we ""have?")

Out [10]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m I need to figure out how many apples are in the boxes
Action: Calculator
Action Input: 8 * 2.5 [0m
Observation: [36; 1m [1; 3mAnswer: 20.0
[0m
Thought: [32; 1m [1; 3m I need to add the apples Mary has to the apples in the boxes
Action: Calculator
Action Input: 4 + 20.0 [0m
Observation: [36; 1m [1; 3mAnswer: 24.0
[0m
Thought: [32; 1m [1; 3m I now know the final answer
Final Answer: We have 24 apples.[0m
[1m > Finished chain.[0m
{'input': 'if Mary has four apples and Giorgio brings two and a half apple boxes (apple box contains eight apples), how many apples do we have?','output': 'We have 24 apples.'}

看起来很棒!但是,如果我们决定问一个非数学问题呢?如果我们问一个简单的常识问题会怎样?

In [11]:

zero_shot_agent("what is the capital of Norway?")

Out [11]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m I need to look up the answer
Action: Look up
Action Input: Capital of Norway [0m
Observation: Look up is not a valid tool, try another one.
Thought: [32; 1m [1; 3m I need to find the answer using a tool
Action: Calculator
Action Input: N/A [0m

我们遇到了一个错误。问题在于代理 (Agents) 一直在尝试使用工具。然而,我们的代理 (Agents) 只包含一个工具——计算器。

幸运的是,我们可以通过给代理 (Agents) 添加更多工具来解决这个问题!让我们添加一个简单的 LLM 工具:

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChainprompt = PromptTemplate(input_variables = ["query"],template = "{query}"
)llm_chain = LLMChain(llm = llm, prompt = prompt)# initialize the LLM tool
llm_tool = Tool(name ='Language Model',func = llm_chain.run,description ='use this tool for general purpose queries and logic'
)

有了这个,我们有了一个新的通用 LLM 工具。我们只需将其添加到 tools 列表中并重新初始化代理 (Agents) :

tools.append(llm_tool)# reinitialize the agent
zero_shot_agent = initialize_agent(agent = "zero-shot-react-description",tools = tools,llm = llm,verbose = True,max_iterations = 3
)

现在我们可以向代理 (Agents) 提问关于数学和常识的问题。让我们尝试以下问题:

In [15]:

zero_shot_agent("what is the capital of Norway?")

Out [15]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m I need to find out what the capital of Norway is
Action: Language Model
Action Input: What is the capital of Norway?[0m
Observation: [33; 1m [1; 3m
The capital of Norway is Oslo.[0m
Thought: [32; 1m [1; 3m I now know the final answer
Final Answer: The capital of Norway is Oslo.[0m
[1m > Finished chain.[0m
{'input': 'what is the capital of Norway?','output': 'The capital of Norway is Oslo.'}

现在我们得到了正确的答案!我们可以问第一个问题:

In [16]:

zero_shot_agent(" what is (4.5*2.1)^2.2?")

Out [16]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m I need to calculate this expression
Action: Calculator
Action Input: (4.5*2.1)^2.2 [0m
Observation: [36; 1m [1; 3mAnswer: 139.94261298333066
[0m
Thought: [32; 1m [1; 3m I now know the final answer
Final Answer: 139.94261298333066 [0m
[1m > Finished chain.[0m
{'input': 'what is (4.5*2.1)^2.2?', 'output': '139.94261298333066'}

代理 (Agents) 理解该引用计算器工具,它确实给了我们正确的答案。

完成后,我们应该了解设计和提示具有不同工具的代理 (Agents) 的工作流程。现在让我们继续介绍可用的不同类型的代理 (Agents) 和工具。

代理 (Agents) 类型

LangChain 提供了几种类型的代理 (Agents) 。在本节中,我们将介绍一些最常见的类型。

Zero Shot ReAct

我们将从之前看到的代理 (Agents) 开始,即 zero-shot-react-description 代理 (Agents) 。

如前所述,我们使用此代理 (Agents) 在某些输入上执行 “zero-shot” 任务。这意味着代理 (Agents) 仅考虑与代理 (Agents) 的 一次 交互——它将没有 记忆

让我们创建一个 tools 列表,供代理 (Agents) 使用。我们将包括一个 llm-math 工具和一个 SQL 数据库工具,我们在 这里定义。

tools = load_tools(["llm-math"], llm = llm
)# add our custom SQL db tool
tools.append(sql_tool)

我们这样初始化 zero-shot-react-description 代理 (Agents) :

from langchain.agents import initialize_agentzero_shot_agent = initialize_agent(agent = "zero-shot-react-description", tools = tools, llm = llm,verbose = True,max_iterations = 3,
)

为了对 SQL 数据库工具进行一些背景说明,我们将使用它来查询一个看起来像这样的“股票数据库”:

obs_idstock_tickerpricedata
1‘ABC’2001 Jan 23
2‘ABC’2082 Jan 23
3‘ABC’2323 Jan 23
4‘ABC’2254 Jan 23
5‘ABC’2265 Jan 23
6‘XYZ’8101 Jan 23
7‘XYZ’8032 Jan 23
8‘XYZ’7983 Jan 23
9‘XYZ’7954 Jan 23
10‘XYZ’7915 Jan 23

现在我们可以开始向这个 SQL 数据库提问,并通过计算器工具进行配对计算。

In [16]:

result = zero_shot_agent("What is the multiplication of the ratio between stock prices for 'ABC' ""and 'XYZ' in January 3rd and the ratio between the same stock prices in ""January the 4th?"
)

Out [16]:

[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m I need to compare the stock prices of 'ABC' and 'XYZ' on two different days
Action: Stock DB
Action Input: Stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th[0m
[1m> Entering new SQLDatabaseChain chain...[0m
Stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th 
SQLQuery:[32;1m[1;3m SELECT stock_ticker, price, date FROM stocks WHERE (stock_ticker = 'ABC' OR stock_ticker = 'XYZ') AND (date = '2023-01-03' OR date = '2023-01-04')[0m
SQLResult: [33;1m[1;3m[('ABC', 232.0, '2023-01-03'), ('ABC', 225.0, '2023-01-04'), ('XYZ', 798.0, '2023-01-03'), ('XYZ', 795.0, '2023-01-04')][0m
Answer:[32;1m[1;3m The stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th were 232.0 and 798.0 respectively for 'ABC' and 'XYZ' on January 3rd, and 225.0 and 795.0 respectively for 'ABC' and 'XYZ' on January 4th.[0m
[1m> Finished chain.[0m
Observation: [33;1m[1;3m The stock prices of 'ABC' and 'XYZ' on January 3rd and January 4th were 232.0 and 798.0 respectively for 'ABC' and 'XYZ' on January 3rd, and 225.0 and 795.0 respectively for 'ABC' and 'XYZ' on January 4th.[0m
Thought:[32;1m[1;3m I need to calculate the ratio between the two stock prices on each day
Action: Calculator
Action Input: 232.0/798.0 and 225.0/795.0[0m
Observation: [36;1m[1;3mAnswer: 0.2907268170426065
0.2830188679245283
[0m
Thought:[32;1m[1;3m I need to calculate the multiplication of the two ratios
Action: Calculator
Action Input: 0.2907268170426065 * 0.2830188679245283[0m
Observation: [36;1m[1;3mAnswer: 0.08228117463469994
[0m
Thought:[32;1m[1;3m[0m
[1m> Finished chain.[0m

我们可以看到这里有很多输出。在每一步中,都有一个思考过程产生了一个选定的动作和动作输入。如果动作需要使用工具,那么观察结果(即工具的输出)就会被传回给代理。

如果我们看一下代理使用的提示,我们就可以看到 LLM 是如何决定使用哪个工具的。

In [17]:

print(zero_shot_agent.agent.llm_chain.prompt.template)

Out [17]:

Answer the following questions as best you can. You have access to the following tools:
Calculator: Useful for when you need to answer questions about math.
Stock DB: Useful for when you need to answer questions about stocks and their prices.
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Calculator, Stock DB]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {input}
Thought:{agent_scratchpad}

我们首先告诉 LLM 它可以使用的工具(Calculator 和 Stock DB)。在此之后,定义了一个示例格式,它遵循 Question(来自用户)、Thought(思考)、Action(动作)、Action Input(动作输入)、Observation(观察结果)的流程 - 并重复这个流程直到达到 Final Answer(最终答案)。

这些工具和思考过程将 LangChain 中的 agents 与 chains 分开。

而 chain 定义了一种即时的输入/输出过程,agents 的逻辑允许一步一步地进行思考。这种一步一步的过程的优势在于 LLM 可以通过多个推理步骤或工具来得出更好的答案。

我们还需要讨论提示的最后一部分。最后一行是 "Thought:{agent_scratchpad}"

agent_scratchpad 是我们添加代理 (Agents) 已经执行的 每个 思考或动作的地方。所有的思考和动作(在 当前 代理 (Agents) 执行器链中)都可以被 下一个 思考-动作-观察循环访问,从而实现代理 (Agents) 动作的连续性。

会话式 ReAct Conversational ReAct

Zero-shot 代理 (Agents) 的效果很好,但缺乏 会话式记忆。

这种缺乏记忆的情况对于需要在对话中 记住 以前的交互的聊天机器人类型的用例来说可能是有问题的。

幸运的是,我们可以使用 conversational-react-description 代理 (Agents) 来 记住 交互。

我们可以将这个代理 (Agents) 看作是我们之前的 Zero Shot ReAct 代理 (Agents) ,但具有 对话记忆

要初始化代理 (Agents) ,我们首先需要初始化我们想要使用的记忆。我们将使用简单的 ConversationBufferMemory

from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory(memory_key =" chat_history ")

我们在初始化代理 (Agents) 时将其传递给 memory 参数:

conversational_agent = initialize_agent(agent ='conversational-react-description', tools = tools, llm = llm,verbose = True,max_iterations = 3,memory = memory,
)

如果我们用类似的问题运行这个代理 (Agents) ,我们应该会看到与之前类似的过程:

In [22]:

result = conversational_agent("Please provide me the stock prices for ABC on January the 1st"
)

Out [22]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m
Thought: Do I need to use a tool? Yes
Action: Stock DB
Action Input: ABC on January the 1st [0m
[1m > Entering new SQLDatabaseChain chain...[0mABC on January the 1st 
SQLQuery: [32; 1m [1; 3m SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-01'[0mSQLResult:  [33; 1m [1; 3m[(200.0,)] [0mAnswer: [32; 1m [1; 3m The price of ABC on January the 1st was 200.0.[0m[1m > Finished chain.[0mObservation: [33; 1m [1; 3m The price of ABC on January the 1st was 200.0.[0mThought: [32; 1m [1; 3m Do I need to use a tool? NoAI: Is there anything else I can help you with?[0m[1m > Finished chain.[0m

到目前为止,这看起来与我们上一个 Zero-shot 代理 (Agents) 非常相似。然而,与我们的 Zero-shot 代理 (Agents) 不同,我们现在可以提问 后续 问题。让我们询问一下股票价格在 同一日期 上的 XYZ,而不指定 1 月 1 日。

In [24]:

result = conversational_agent("What are the stock prices for XYZ on the same day?"
)

Out [24]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m
Thought: Do I need to use a tool? Yes
Action: Stock DB
Action Input: Stock prices for XYZ on January 1st [0m
[1m > Entering new SQLDatabaseChain chain...[0m
Stock prices for XYZ on January 1st 
SQLQuery: [32; 1m [1; 3m SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-01'[0m
SQLResult:  [33; 1m [1; 3m[(810.0,)] [0m
Answer: [32; 1m [1; 3m The stock price for XYZ on January 1st was 810.0.[0m
[1m > Finished chain.[0m
Observation: [33; 1m [1; 3m The stock price for XYZ on January 1st was 810.0.[0m
Thought: [32; 1m [1; 3m Do I need to use a tool? No
AI: Is there anything else I can help you with?[0m
[1m > Finished chain.[0m

我们可以看到第一个 Action Input 中代理 (Agents) 正在寻找 "Stock prices for XYZ on January 1st"。它知道我们正在寻找 1 月 1 日,因为我们在之前的交互中提到了这个日期。

它是如何做到的呢?我们可以看一下提示模板来找出答案:

In [23]:

print(conversational_agent.agent.llm_chain.prompt.template)

Out [23]:

Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
TOOLS:
------
Assistant has access to the following tools:
> Calculator: Useful for when you need to answer questions about math.
> Stock DB: Useful for when you need to answer questions about stocks and their prices.
To use a tool, please use the following format:
"""```
Thought: Do I need to use a tool? Yes
Action: the action to take, should be one of [Calculator, Stock DB]
Action Input: the input to the action
Observation: the result of the action
"" " ```
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
"""```
Thought: Do I need to use a tool? No
AI: [your response here]
"" " ```
Begin!
Previous conversation history:
{chat_history}
New input: {input}
{agent_scratchpad}

我们在提示的开头有一个更大的指令设置,但最重要的是在提示的末尾附近的两行:

Previous conversation history: {chat_history}

这是我们将所有先前的交互添加到提示中的地方。在这个空间内将包含我们要求的信息 "Please provide me the stock prices for ABC on January the 1st" - 这使得代理 (Agents) 可以理解我们的后续问题是指同一日期。

值得注意的是,与 Zero-shot 代理 (Agents) 相比,会话式 ReAct 代理 (Agents) 在组合多个复杂步骤时会更加困难。如果我们要求代理 (Agents) 回答我们之前的问题,我们可以看到这一点:

In [26]:

result = conversational_agent("What is the multiplication of the ratio of the prices of stocks 'ABC' ""and 'XYZ' in January 3rd and the ratio of the same prices of the same ""stocks in January the 4th?"
)

Out [26]:

[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mThought: Do I need to use a tool? Yes
Action: Stock DB
Action Input: Get the ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd and the ratio of the same prices of the same stocks in January the 4th[0m
[1m> Entering new SQLDatabaseChain chain...[0m
Get the ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd and the ratio of the same prices of the same stocks in January the 4th 
SQLQuery:[32;1m[1;3m SELECT (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-03') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-03') AS ratio_jan_3, (SELECT price FROM stocks WHERE stock_ticker = 'ABC' AND date = '2023-01-04') / (SELECT price FROM stocks WHERE stock_ticker = 'XYZ' AND date = '2023-01-04') AS ratio_jan_4 FROM stocks LIMIT 5;[0m
SQLResult: [33;1m[1;3m[(0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283), (0.2907268170426065, 0.2830188679245283)][0m
Answer:[32;1m[1;3m The ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd is 0.2907268170426065 and the ratio of the same prices of the same stocks in January the 4th is 0.2830188679245283.[0m
[1m> Finished chain.[0m
Observation: [33;1m[1;3m The ratio of the prices of stocks 'ABC' and 'XYZ' in January 3rd is 0.2907268170426065 and the ratio of the same prices of the same stocks in January the 4th is 0.2830188679245283.[0m
Thought:[32;1m[1;3m Do I need to use a tool? No
AI: The answer is 0.4444444444444444. Is there anything else I can help you with?[0m
[1m> Finished chain.[0m
Spent a total of 2518 tokens

通过这样做,代理 (Agents) 仍然设法解决了问题,但使用了一个更复杂的纯 SQL 方法,而不是依赖于更直接的 SQL 和计算器工具。

ReAct 文档存储

另一个常见的代理 (Agents) 是 react-docstore 代理 (Agents) 。与之前一样,它使用 ReAct 方法,但现在明确地用于使用 LangChain 的 docstore 进行信息搜索和查找。

LangChain docstore 允许我们使用传统的检索方法存储和检索信息。其中一个 docstore 是维基百科,它使我们能够访问站点上的信息。

我们将使用两种 docstore 方法来实现这个代理 (Agents) - Search 和 Lookup

使用 Search,我们的代理 (Agents) 将搜索相关文章;

使用 Lookup,代理 (Agents) 将在检索到的文章中找到相关的信息块。

要初始化这两个工具,我们执行以下操作:

from langchain import Wikipedia
from langchain.agents.react.base import DocstoreExplorerdocstore = DocstoreExplorer(Wikipedia())
tools = [Tool(name = "Search",func = docstore.search,description ='search wikipedia'),Tool(name = "Lookup",func = docstore.lookup,description ='lookup a term in wikipedia')
]

现在初始化代理 (Agents) :

docstore_agent = initialize_agent(tools, llm, agent = "react-docstore", verbose = True,max_iterations = 3
)

让我们尝试以下内容:

In [30]:

docstore_agent("What were Archimedes' last words?")

Out [30]:

[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mThought: I need to search Archimedes and find his last words.
Action: Search[Archimedes][0m
Observation: [36;1m[1;3mArchimedes of Syracuse (; c. 287 – c. 212 BC) was a Greek mathematician, physicist, engineer...[0m
Thought:[32;1m[1;3m The paragraph does not mention Archimedes' last words. I need to look up "last words".
Action: Lookup[last words][0m
Observation: [33;1m[1;3m(Result 1/1) Plutarch (45–119 AD) wrote in his Parallel Lives that Archimedes was related to King Hiero...[0m
Thought:[32;1m[1;3m The last words attributed to Archimedes are "Do not disturb my circles", so the answer is "Do not disturb my circles".
Action: Finish["Do not disturb my circles"][0m
[1m> Finished chain.[0m
{'input': "What were Archimedes' last words?",'output': '"Do not disturb my circles"'}

这个代理 (Agents) 的提示非常长,所以我们会展示一个缩短版。它只包含了几个示例,展示了这个代理 (Agents) 应该如何使用 Search 和 Lookup 工具:

Question: What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?Thought: 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: Search [Colorado orogeny]
Observation: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.Thought: It does not mention the eastern sector. So I need to look up eastern sector.
Action: Lookup [eastern sector]
Observation: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.Thought: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.
Action: Search [High Plains]
Observation: High Plains refers to one of two distinct land regionsThought: I need to instead search High Plains (United States).
Action: Search [High Plains (United States)]
Observation: 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: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
Action: Finish [1,800 to 7,000 ft]

提示中包含了几个类似格式的示例。在提示的末尾,我们看到:

Question: {input}
{agent_scratchpad}

与之前一样,我们有一个 {input} 来传递最近的用户查询,还有一个 {agent_scratchpad} 来跟踪以前的思考和行动。

与我们的 会话式 代理 (Agents) 不同,这里没有 {chat_history} 输入。这意味着我们正在使用另一个 Zero-shot 代理 (Agents) 。

使用搜索自问

让我们看看最后一个代理 (Agents) - self-ask-with-search 代理 (Agents) 。当连接 LLM 和搜索引擎时,这是您应该考虑的第一个代理 (Agents) 。

代理 (Agents) 将根据需要执行搜索和提问步骤,以获得最终答案。我们这样初始化代理 (Agents) :

from langchain import SerpAPIWrapper# initialize the search chain
search = SerpAPIWrapper(serpapi_api_key ='serp_api_key')# create a search tool
tools = [Tool(name = "Intermediate Answer",func = search.run,description ='google search')
]# initialize the search enabled agent
self_ask_with_search = initialize_agent(tools,llm,agent = "self-ask-with-search",verbose = True
)

现在让我们问一个需要多次搜索和“自问”步骤的问题。

In [38]:

self_ask_with_search("who lived longer; Plato, Socrates, or Aristotle?"
)

Out [38]:

[1m > Entering new AgentExecutor chain...[0m
[32; 1m [1; 3m Yes.
Follow up: How old was Plato when he died?[0m
Intermediate answer: [36; 1m [1; 3meighty [0m [32; 1m [1; 3m
Follow up: How old was Socrates when he died?[0m
Intermediate answer: [36; 1m [1; 3mapproximately 71 [0m [32; 1m [1; 3m
Follow up: How old was Aristotle when he died?[0m
Intermediate answer: [36; 1m [1; 3m62 years [0m [32; 1m [1; 3m
So the final answer is: Plato [0m
[1m > Finished chain.[0m
{'input': 'who lived longer; Plato, Socrates, or Aristotle?','output': 'Plato'}

我们可以看到代理 (Agents) 的多步骤过程。它进行了多次后续问题来找到最终答案。


这就是关于 LangChain 代理 (Agents) 的章节的全部内容。正如您无疑注意到的那样,代理 (Agents) 涵盖了 LangChain 中广泛的工具范围。我们已经涵盖了很多基本知识,但还有很多可以讨论的内容。

代理 (Agents) 的变革潜力是大型语言模型(LLMs)的一大飞跃,只是时间问题,“LLM 代理 (Agents) ”这个术语将成为 LLMs 本身的代名词。

通过赋予 LLMs 利用工具和在这些代理 (Agents) 框架中导航复杂的多步骤思考过程的能力,我们正在进入一个庞大到令人难以置信的 AI 驱动机会的领域。

参考文献

[1] Langchain.io (2019), Wayback Machine

[2] 李俊航,Mother of Language Slides (2018),SlideShare


下一章:为 LLM Agents 构建自定义工具

LANGCHAIN 开发者技术交流社群

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/102138.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

华为发布LampSite X室内数字化创新解决方案,释放数字世界无限潜能

【阿联酋,迪拜,2023年10月11日】2023全球移动宽带论坛(Global MBB Forum 2022)期间,华为董事、ICT产品与解决方案总裁杨超斌重磅发布了全新一代5G室内数字化产品解决方案LampSite X系列,助力运营商打开商业…

canvas力导布局

老规矩&#xff0c;先上效果图 <html><head><style>* {margin: 0;padding: 0;}canvas {display: block;width: 100%;height: 100%;background: #000;}</style> </head><body><canvas id"network"></canvas> </…

如何选择高防CDN和高防IP?

目录 前言 一、对高防CDN的选择 1. 加速性能 2. 抗攻击能力 3. 全球覆盖能力 4. 可靠性和稳定性 二、对高防IP的选择 1. 防御能力 2. 服务质量 3. 安全性 4. 价格 三、高防CDN和高防IP的优缺点对比 1. 高防CDN的优缺点 2. 高防IP的优缺点 总结 前言 随着互联网…

U盘怎么设置为只读?U盘怎么只读加密?

当将U盘设置为只读模式时&#xff0c;将只能查看其中数据&#xff0c;无法对其中数据进行编辑、复制、删除等操作。那么&#xff0c;怎么将U盘设置成只读呢&#xff1f; U盘如何设置成只读&#xff1f; 有些U盘带有写保护开关&#xff0c;当打开时&#xff0c;U盘就会处于只读…

京东商品品牌数据采集接口,京东商品详情数据接口,京东API接口

采集京东商品品牌数据的方法如下&#xff1a; 打开网页。在首页【输入框】中输入目标网址批量输入多个关键词并搜索。创建【循环列表】&#xff0c;采集所有商品列表中的数据。编辑字段。创建【循环翻页】&#xff0c;采集多页数据。设置滚动和修改【循环翻页】XPath。启动采集…

使用docker安装db2

使用docker安装db2 1. 前言1.1 关于docker的安装1.2 安装db2版本选择参考 2. 拉取镜像3. 启动镜像4. 进入容器&#xff0c;切换用户4.1 进入容器4.2 切换用户4.3 其他命令 5. 可视化工具DBeaver连接db25.1 连接5.2 简单使用 1. 前言 1.1 关于docker的安装 关于Linux上docker的…

【考研408常用数据结构】C/C++实现代码汇总

文章目录 前言数组多维数组的原理、作用稀疏数组 链表单向链表的增删改查的具体实现思路约瑟夫环问题&#xff08;可不学&#xff09;双向链表 树二叉搜索树中序线索二叉树哈夫曼树的编码与译码红黑树B树B树 堆顺序与链式结构队列实现优先队列排序算法&#xff08;重点&#xf…

第五节 C++ 循环结构(算法)

文章目录 前言介绍1. for 语句1.1 语法结构1.2 语法流程的执行过程1.2.1 案例 1:循环的正序输入和倒序输入1.2.2 案例2 : 求1~n的平方数1.2.3 案例 3: 求输入a和b,求a~b区间数. 1.3 for 循环案例练习1.3.1 求最大值与最小值1.3.2 计算奇数和和偶数和1.3.3 计算平均气温与最高气…

Stable diffusion 用DeOldify给黑白照片、视频上色

老照片常常因为当时的技术限制而只有黑白版本。然而现代的 AI 技术,如 DeOldify,可以让这些照片重现色彩。 本教程将详细介绍如何使用 DeOldify 来给老照片上色。. 之前介绍过基于虚拟环境的 基于DeOldify的给黑白照片、视频上色,本次介绍对于新手比较友好的在Stable diff…

04-RocketMQ源码解读

目录汇总&#xff1a;RocketMQ从入门到精通汇总 上一篇&#xff1a;03-RocketMQ高级原理 这一部分&#xff0c;我们开始深入RocketMQ的源码。源码的解读是个非常困难的过程&#xff0c;每个人的理解程度都会不一样&#xff0c;也不太可能通过讲解把其中的细节全部讲明白。我们今…

13私有化属性的一些注意点

目录 一、解释一下几个私有化访问的方式&#xff01;&#xff01;&#xff01; 1.类内部访问&#xff1a; 2.子类访问 3.模块内其他位置访问 4.跨模块访问 二、_y受保护属性的访问 只有跨模块这块需要注意一下&#xff01; 这个是可以访问的 但是下面这个是不可以的 …

Python皮卡丘

系列文章 序号文章目录直达链接1浪漫520表白代码https://want595.blog.csdn.net/article/details/1306668812满屏表白代码https://want595.blog.csdn.net/article/details/1297945183跳动的爱心https://want595.blog.csdn.net/article/details/1295031234漂浮爱心https://want…

Element UI怎么安装呢?

安装 :::warning 注意 后续演示将会在 Vue CLI 搭建的 Vue 项目上进行操作。如需要请查看 Vue CLI 安装 ::: 通过 YARN 命令安装 $ yarn add element-ui完整引入 代表一次性引入所有组件&#xff0c;比较省心省事&#xff0c;但是项目的打包体积也会跟着变大。 // main.js…

3.简单场景构建

在新建的项目中&#xff0c;默认存在 Main Camera 和 Directional Light两个对象。若是缺失&#xff0c;可通过选择菜单中的 Game Object->Camera 和 Geme Object->Light->Directional Light进行创建。 1.添加地形及底图 通过在Cesium面板中选择 Cesium World Terrai…

VAE模型(详细推导+实例代码)

文章目录 EM算法思路E步M步直观感觉 GMM模型VAEVAE思想从GMM到VAE公式推导重参数VAE神经网络另一个视角的VAE思想为什么引入encoder为什么要重参数噪声与重建 Discrete VAE 本文会从EM算法&#xff0c;GMM模型一步一步的的推导&#xff0c;在过渡到VAE模型&#xff0c;如果有熟…

Excel·VBA使用ADO合并工作簿

之前文章《ExcelVBA合并工作簿&#xff08;7&#xff0c;合并子文件夹同名工作簿中同名工作表&#xff0c;纵向汇总数据&#xff09;》处理合并工作簿问题&#xff0c;代码运行速度比较慢 而《ExcelVBA使用ADO读取工作簿工作表数据》读取数据非常快&#xff0c;那么是否可以使用…

1200*C1. k-LCM (easy version)(找规律)

Problem - 1497C1 - Codeforces 解析&#xff1a; 找规律即可&#xff0c;分为偶数的一半是偶数、偶数的一半是奇数、奇数三种情况 分别为 &#xff08;n/2&#xff0c;n/4&#xff0c;n/4&#xff09;&#xff08;n/2-1&#xff0c;n/2-1&#xff0c;2&#xff09;&#xff08…

常见算法-双骰子游戏(Craps)

常见算法-双骰子游戏&#xff08;Craps&#xff09; 1、说明 一个简单的双骰子游戏&#xff0c;游戏规则如下&#xff1a; 玩家掷两个骰子&#xff0c;点数为1到6&#xff0c; 如果第一次点数和为7或11&#xff0c;则玩家胜&#xff0c;如果点数和为2、3或12&#xff0c;则…

Nlopt在matlab中的配置教程

step1&#xff1a;克隆代码并编译 编译的前提是已经安装好MinGW64 # 使用镜像加速 git clone https://gitclone.com/github.com/stevengj/nloptcd nlopt mkdir build cd build cmake -G"MinGW Makefiles" .. cmake --build .# 注意此处博主在mingw安装目录将mingw3…

windows10下 iperf3测试带宽

iperf3下载网址&#xff1a;iPerf - Download iPerf3 and original iPerf pre-compiled binaries 可以用来测试TCP以及UDP带宽质量 通俗来说是用来测试网速的 准备&#xff1a;两台设备 1. 根据自己的设备选择下载工具&#xff08;两台都要有&#xff0c;这里我用的Window…