天天看點

LangChain手冊(JS/TS版)17代理:代理概述

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 be

    agent 決定使用哪個 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 next

    tool 、 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 as

Web浏覽器工具使您的代理能夠通路網站并提取資訊。它被描述為代理

️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的功能和特定于聊天模型