当前位置: 首页 > news >正文

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 开发者技术交流社群

相关文章:

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

原文:Langchain 代理 (Agents) ,赋能超级 LLMs - 知乎 大型语言模型(LLMs) 非常强大,但它们缺乏“最笨”的计算机程序可以轻松处理的特定能力。逻辑、计算和搜索是计算机通常擅长的领域,但 LLMs 却遇到了困…...

华为OD机考算法题:阿里巴巴找黄金宝箱(1)

题目部分 题目阿里巴巴找黄金宝箱(1)难度易题目说明一贫如洗的樵夫阿里巴巴在去砍柴的路上,无意中发现了强盗集团的藏宝地,藏宝地有编号从 0 ~ N 的箱子,每个箱子上面贴有一个数字,箱子中可能有一个黄金宝…...

【JAVA版本】websocket获取B站直播弹幕——基于直播开放平台

教程 B站直播间弹幕Websocket获取 — 哔哩哔哩直播开放平台 基于B站直播开放平台开放且未上架时,只能个人使用。 代码实现 1、相关依赖 fastjson2用于解析JSON字符串,可自行替换成别的框架。 hutool-core用于解压zip数据,可自行替换成别的…...

Vue中的监视属性

一、监视属性的使用 (一)配置watch进行监视 当我们想要监视一个属性改变的时候就可以使用监视属性监视其变化并进行操作。 语法格式如下: watch:{ 监视属性名称 : { // 监视属性的配置项 } } 1. handler函数 当监视的属性发生变化时就调…...

汽车一键启动点火开关按键一键启动按钮型号规格

汽车点火开关/移动管家一键启动按键/汽车改装引擎启动按钮型号:YD828溥款开关 一键启动按钮(适用于配套启动主机使用或原车一键启动开关更换) 1.适合配套专用板板安装 2.开孔器开孔安装 3.原车钥匙位安装 外观:黑色 按钮上有3种不…...

快速学习微服务保护框架--Sentinel

学习一个框架最好的方式就是查看官方地址,sentinel是国内阿里巴巴公司的,官网更方便官网 官网 微服务保护框架 Sentinel 1.初识Sentinel 1.1.雪崩问题及解决方案 1.1.1.雪崩问题 微服务中,服务间调用关系错综复杂,一个微服务往往依赖于多个其它微…...

bootz启动 Linux内核过程总结

一. bootz启动Linux uboot 启动 Linux内核使用bootz命令。当然还有其它的启动命令,例如,bootm命令等等。 前面几篇文章分析 bootz命令启动 Linux内核的过程中涉及的几个重要函数。 bootz启动 Linux内核过程中涉及的全局变量images_凌肖战的博客-CSDN博…...

前端项目--尚医通学习分享

这段时间跟着线上课程完成了一个项目:商医通(基于Vue3TypeScript的医院挂号平台)。具体我就不过多地介绍其具体功能以及详细的实现步骤了,感兴趣的小伙伴直接:传送门 。该文章我就分享一下在该项目中学习到的一些知识点…...

【Python】QTreeWidget树形结构添加

源码: # 参考网址: https://blog.csdn.net/weixin_42286052/article/details/129532631 import os.path import sys from PySide6.QtWidgets import QApplication,QMainWindow,QHBoxLayout,QVBoxLayout,QPushButton,QTreeWidget,QTreeWidgetItem,QTreeW…...

day 2 2.3.2 类和对象

具有相同或相似性质的一组对象的抽象就是类 null只能被转换成引用类型,不能转换成基本类型,因此不要把一个null值赋给基本数据类型的变量 Java语言支持的类型分为两类:基本类型和引用类型 基本类型包括boolean类型和数值类型。数值类型有整…...

vscode虚拟环境使用jupyter

在某虚拟环境内安装torch,但是ipyn文件保存后无法正常导入torch 1.conda环境下安装Jupyter等一切配置,进入虚拟环境 2.conda install nb_conda_kernels 3.安装完成后重新打开VSCode,在运行Jupyter notebook中的代码之前,在右上…...

Maven 依赖管理

Maven 一个核心的特性就是依赖管理。当我们处理多模块的项目(包含成百上千个模块或者子项目),模块间的依赖关系就变得非常复杂,管理也变得很困难。针对此种情形,Maven 提供了一种高度控制的方法。 可传递性依赖发现 …...

【踩坑】hive脚本笛卡尔积严重降低查询效率问题

前一阵子查看我们公司的大数据平台的离线脚本运行情况, 结果发现有一个任务居然跑了一天多, 要知道这还只是几千万量级的表, 且这个任务是每天需要执行的 于是我把hive脚本捞出来看了下, 发现无非多join了几个复杂的子查询, 应该不至于这么久, 包括我又检查了是不是没有加上每…...

【C进阶】内存函数

strcpy拷贝的仅仅是字符串,但是内存中的数据不仅仅是字符,所以就有了memcpy函数 1. memcpy void *memcpy (void * destination ,const void * source , size_t num) 函数memcpy从source的位置开始向后拷贝num个字节的数据到desti…...

h2database BTree 设计实现与查询优化思考

h2database 是使用Java 编写的开源数据库,兼容ANSI-SQL89。 即实现了常规基于 BTree 的存储引擎,又支持日志结构存储引擎。功能非常丰富(死锁检测机制、事务特性、MVCC、运维工具等),数据库学习非常好的案例。 本文理论…...

Linux命令(100)之sz

linux命令之sz 1.sz介绍 linux命令sz是用来把文件从Linux平台下载到Windows上 2.sz用法 sz [参数] file sz参数 参数说明-b使用binary的方式下载,不解释字符为ascii-y相同文件名,覆盖-E相同文件名,不会将其覆盖,而是会在所上传…...

Insight h2database SQL like 查询

我们认为的 SQL like 查询和优化技巧,设计的初衷和真正的实现原理是什么。 在 h2database SQL like 查询实现类中(CompareLike),可以看到 SQL 语言到具体执行的实现、也可以看到数据库尝试优化语句的过程,以及查询优化…...

wpf中listview内容居中显示

在WPF中使用ListView经常会用到GridView作为视图&#xff0c;但是却碰到GridViewColumn不能居中对齐的问题&#xff0c; 实现方法 给ListViewItem设置Style,让ListViewItem在水平方向拉伸填充&#xff1a; <Setter Property"HorizontalContentAlignment" Value&…...

第二章 C++的输出

系列文章目录 第一章 C的输入 文章目录 系列文章目录前言一、个人名片二、cout三、printf总结 前言 今天来学C的输出吧&#xff01; 一、个人名片 二、cout cout 三、printf printf 总结 最近懒得写博客怎么办&#xff1f;...

Qt中常用容器组控件介绍和实操

目录 常用容器组控件(Containers)&#xff1a; 1.Group Box 2.Scroll Area 3.Tab Widget 4.Frame 5.Dock Widget 常用容器组控件(Containers)&#xff1a; 控件名称依次解释如下(常用的用红色标出&#xff09;: Group Box: 组合框: 提供带有标题的组合框框架Scroll Area…...

Cinnamon修改面板小工具图标

Cinnamon开始菜单-CSDN博客 设置模块都是做好的&#xff0c;比GNOME简单得多&#xff01; 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...

零基础设计模式——行为型模式 - 责任链模式

第四部分&#xff1a;行为型模式 - 责任链模式 (Chain of Responsibility Pattern) 欢迎来到行为型模式的学习&#xff01;行为型模式关注对象之间的职责分配、算法封装和对象间的交互。我们将学习的第一个行为型模式是责任链模式。 核心思想&#xff1a;使多个对象都有机会处…...

C# 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

Golang——9、反射和文件操作

反射和文件操作 1、反射1.1、reflect.TypeOf()获取任意值的类型对象1.2、reflect.ValueOf()1.3、结构体反射 2、文件操作2.1、os.Open()打开文件2.2、方式一&#xff1a;使用Read()读取文件2.3、方式二&#xff1a;bufio读取文件2.4、方式三&#xff1a;os.ReadFile读取2.5、写…...

R 语言科研绘图第 55 期 --- 网络图-聚类

在发表科研论文的过程中&#xff0c;科研绘图是必不可少的&#xff0c;一张好看的图形会是文章很大的加分项。 为了便于使用&#xff0c;本系列文章介绍的所有绘图都已收录到了 sciRplot 项目中&#xff0c;获取方式&#xff1a; R 语言科研绘图模板 --- sciRplothttps://mp.…...

Chrome 浏览器前端与客户端双向通信实战

Chrome 前端&#xff08;即页面 JS / Web UI&#xff09;与客户端&#xff08;C 后端&#xff09;的交互机制&#xff0c;是 Chromium 架构中非常核心的一环。下面我将按常见场景&#xff0c;从通道、流程、技术栈几个角度做一套完整的分析&#xff0c;特别适合你这种在分析和改…...

Xela矩阵三轴触觉传感器的工作原理解析与应用场景

Xela矩阵三轴触觉传感器通过先进技术模拟人类触觉感知&#xff0c;帮助设备实现精确的力测量与位移监测。其核心功能基于磁性三维力测量与空间位移测量&#xff0c;能够捕捉多维触觉信息。该传感器的设计不仅提升了触觉感知的精度&#xff0c;还为机器人、医疗设备和制造业的智…...

【Kafka】Kafka从入门到实战:构建高吞吐量分布式消息系统

Kafka从入门到实战:构建高吞吐量分布式消息系统 一、Kafka概述 Apache Kafka是一个分布式流处理平台,最初由LinkedIn开发,后成为Apache顶级项目。它被设计用于高吞吐量、低延迟的消息处理,能够处理来自多个生产者的海量数据,并将这些数据实时传递给消费者。 Kafka核心特…...

《Offer来了:Java面试核心知识点精讲》大纲

文章目录 一、《Offer来了:Java面试核心知识点精讲》的典型大纲框架Java基础并发编程JVM原理数据库与缓存分布式架构系统设计二、《Offer来了:Java面试核心知识点精讲(原理篇)》技术文章大纲核心主题:Java基础原理与面试高频考点Java虚拟机(JVM)原理Java并发编程原理Jav…...

聚六亚甲基单胍盐酸盐市场深度解析:现状、挑战与机遇

根据 QYResearch 发布的市场报告显示&#xff0c;全球市场规模预计在 2031 年达到 9848 万美元&#xff0c;2025 - 2031 年期间年复合增长率&#xff08;CAGR&#xff09;为 3.7%。在竞争格局上&#xff0c;市场集中度较高&#xff0c;2024 年全球前十强厂商占据约 74.0% 的市场…...