langchain详细介绍 (langchain开发手册)

Some applications will require not just a predetermined chain of calls to LLMs/other tools, but potentially an unknown chain that depends on the user's input. In these types of chains, there is a “agent” which has access to a suite of tools. Depending on the user input, the agent can then decide which, if any, of these tools to call.某些应用程序不仅需要对LLM/其他工具的预定调用链,还需要依赖于用户输入的未知链。在这些类型的链中,有一个“代理”可以访问一套工具。然后,根据用户输入,代理可以决定调用这些工具中的哪一个(如果有)。

At the moment, there are two main types of agents:目前,有两种主要类型的代理:

  1. Action Agents : these agents decide an action to take and take that action one step at a time操作代理:这些代理决定要执行的操作,并一次一步地执行该操作
  2. Plan-and-Execute Agents : these agents first decide a plan of actions to take, and then execute those actions one at a time.计划和执行代理:这些代理首先确定要执行的行动计划,然后一次执行一个操作。

When should you use each one?什么时候应该使用每一个?

  • Action Agents are more conventional, and good for small tasks.操作代理更传统,适用于小任务。
  • For more complex or long running tasks, the initial planning step of Plan-and-Execute Agents helps to maintain long term objectives and focus, at the expense of generally more calls and higher latency.对于更复杂或长时间运行的任务,计划执行代理的初始规划步骤有助于保持长期目标和重点,但代价通常是更多的调用和更高的延迟。

These two agents are also not mutually exclusive - in fact, it is often best to have an Action Agent be in charge of the execution for the Plan and Execute agent.这两个代理也不是互斥的 - 事实上,通常最好让一个操作代理负责计划和执行代理的执行。

Action Agents 操作代理

The high-level pseudocode of an Action Agent looks something like:操作代理的高级伪代码如下所示:

  • Some user input is received 收到一些用户输入
  • The agent decides which tool - if any - to use, and what the input to that tool should beagent 决定使用哪个 tool (如果有),以及该工具的输入应该是什么
  • That tool is then called with that tool input, and an observation is recorded (this is just the output of calling that tool with that tool input).然后用那个 tool input 调用该 tool ,并记录一个 observation (这只是使用该工具输入调用该工具的输出)。
  • That history of tool, tool input, and observation is passed back into the agent, and it decides what steps to take nexttool 、 tool input 和 observation 的历史记录将传递回 agent ,并决定下一步要采取的步骤
  • This is repeated until the agent decides it no longer needs to use a tool, and then it responds directly to the user.重复此操作,直到 agent 决定不再需要使用 tool ,然后直接响应用户。
interface AgentStep {
  action: AgentAction;
  observation: string;
}

interface AgentAction {
  tool: string; // Tool.name
  toolInput: string; // Tool.call argument
}

interface AgentFinish {
  returnValues: object;
}

class Agent {
  plan(steps: AgentStep[], inputs: object): Promise<AgentAction | AgentFinish>;
}

Plan-and-Execute Agents​ 计划和执行代理

The high level pseudocode of a Plan-and-Execute Agent looks something like:计划和执行代理的高级伪代码如下所示:

  • Some user input is received 收到一些用户输入
  • The planner lists out the steps to take计划者列出要采取的步骤
  • The executor goes through the list of steps, executing them one-by-one until outputting the final result执行者浏览步骤列表,逐个执行,直到输出最终结果

The current implementation is to use an LLMChain as the planner and an Action Agent as the executor.当前的实现是使用 LLMChain 作为计划者,使用 Action Agent 作为执行者。

Go deeper​ 更深入

️Agents ️代理

️Action Agents ️操作代理

️Plan-and-Execute Agent ️计划与执行代理

This example shows how to use an agent that uses the Plan-and-Execute framework to answer a query.此示例演示如何使用使用计划和执行框架来回答查询的代理。

️Custom Agents ️定制代理

️Agent Executors ️代理执行人

️Tools ️工具

️Integrations ️集成

LangChain provides the following tools you can use out of the box:LangChain提供了以下开箱即用的工具:

️Agents with Vector Stores ️具有矢量存储的代理

This notebook covers how to combine agents and vector stores. The use case for this is that you’ve ingested your data into a vector store and want to interact with it in an agentic manner.本笔记本介绍了如何组合代理和载体存储。这样做的用例是,您已将数据引入矢量存储,并希望以代理方式与之交互。

️ChatGPT Plugins ️聊天插件

This example shows how to use ChatGPT Plugins within LangChain abstractions.这个例子展示了如何在LangChain抽象中使用ChatGPT插件。

️Custom Tools ️自定义工具

One option for creating a tool that runs custom code is to use a DynamicTool.创建运行自定义代码的工具的一个选项是使用 DynamicTool。

️Agent with AWS Lambda ️使用 AWS Lambda 的代理

Full docs here//docs.aws.amazon.com/lambda/index.html完整文档 here//docs.aws.amazon.com/lambda/index.html

️Web Browser Tool ️网页浏览器工具

The Webbrowser Tool gives your agent the ability to visit a website and extract information. It is described to the agent asWeb浏览器工具使您的代理能够访问网站并提取信息。它被描述为代理

️Agent with Zapier NLA Integration️使用Zapier NLA集成的代理

Full docs here//nla.zapier.com/api/v1/dynamic/docs完整的文档在这里//nla.zapier.com/api/v1/dynamic/docs

️Toolkits ️工具包

️JSON Agent Toolkit ️JSON 代理工具包

This example shows how to load and use an agent with a JSON toolkit.此示例演示如何通过 JSON 工具包加载和使用代理。

️OpenAPI Agent Toolkit ️OpenAPI 代理工具包

This example shows how to load and use an agent with a OpenAPI toolkit.此示例演示如何通过 OpenAPI 工具包加载和使用代理。

️SQL Agent Toolkit ️SQL 代理工具包

This example shows how to load and use an agent with a SQL toolkit.此示例演示如何通过 SQL 工具包加载和使用代理。

️VectorStore Agent Toolkit ️矢量存储代理工具包

This example shows how to load and use an agent with a vectorstore toolkit.此示例演示如何加载和使用带有矢量存储工具包的代理。

️Additional Functionality ️附加功能

We offer a number of additional features for Agents. You should also look at the LLM-specific features and Chat Model-specific features.我们为代理提供了许多附加功能。您还应该查看特定于LLM的功能和特定于聊天模型