代理(agent)介绍
某些应用程序根据用户输入需要灵活地调用LLMs和其他工具的链。代理接口为这类应用程序提供了灵活性。代理可以访问一套工具,并根据用户输入决定使用哪些工具。代理可以使用多个工具,并将一个工具的输出用作下一个工具的输入。
代理主要有两种类型:
- 行动代理:在每个时间步骤中,使用所有先前行动的输出来决定下一个行动
- 计划并执行代理:预先决定完整的行动序列,然后按顺序执行,而不更新计划
行动代理适用于小任务,而计划并执行代理适用于需要保持长期目标和专注的复杂或长时间运行的任务。通常,最好的方法是通过允许计划并执行代理使用行动代理执行计划,将行动代理的灵活性与计划并执行代理的规划能力相结合。
有关代理类型的完整列表,请参见代理类型。与代理相关的其他抽象包括:
- 工具:代理可以采取的操作。给代理提供什么工具非常取决于您希望代理执行什么任务
- 工具包:工具集合的包装器,可以一起用于特定的用例。例如,为了使代理能够与SQL数据库进行交互,它可能需要一个执行查询的工具和一个查看表的工具
行动代理
在高层次上,一个行动代理:
- 接收用户输入
- 决定是否使用工具以及工具输入
- 调用工具并记录输出(也称为“观察”)
- 使用工具、工具输入和观察历史记录决定下一步
- 重复步骤3-4,直到确定能够直接回复用户
行动代理包装在代理执行器中,该执行器负责调用代理,获取动作和动作输入,使用动作引用的工具和生成的输入调用工具,获取工具的输出,然后将所有信息传回代理以获取应采取的下一个动作。
尽管代理可以以多种方式构建,但通常包括以下组件:
- 提示模板:负责接收用户输入和先前步骤,并构建发送到语言模型的提示
- 语言模型:按照使用输入和行动历史记录,决定下一步做什么
- 输出解析器:接收语言模型的输出,并将其解析为下一个行动或最终答案
计划并执行代理
在高层次上,计划并执行代理:
- 接收用户输入
- 规划要执行的完整步骤序列
- 按顺序执行步骤,将过去步骤的输出作为未来步骤的输入传递
最典型的实现方式是将规划器作为语言模型,将执行器作为行动代理。
入门指南
LangChain提供了几种类型的智能代理。下面是一个使用基于OpenAI的函数的示例:
import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { SerpAPI } from "langchain/tools";
import { Calculator } from "langchain/tools/calculator";
const tools = [new Calculator(), new SerpAPI()];
const chat = new ChatOpenAI({ modelName: "gpt-4", temperature: 0 });
const executor = await initializeAgentExecutorWithOptions(tools, chat, {
agentType: "openai-functions",
verbose: true,
});
const result = await executor.run("What is the weather in New York?");
console.log(result);
/*
纽约目前的天气为72°F,风速为1 mph,来自SSW方向。湿度为89%,紫外线指数为0 / 11。云层覆盖率为79%,没有降雨。
*/
这是打印的详细输出结果:
...
[chain/start] [1:chain:AgentExecutor] Entering Chain run with input: {
"input": "What is the weather in New York?",
"chat_history": []
}
[llm/start] [1:chain:AgentExecutor > 2:llm:ChatOpenAI] Entering LLM run with input: {
"messages": [
[
{
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"SystemMessage"
],
"kwargs": {
"content": "You are a helpful AI assistant.",
"additional_kwargs": {}
}
},
{
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"HumanMessage"
],
"kwargs": {
"content": "What is the weather in New York?",
"additional_kwargs": {}
}
}
]
]
}
[llm/end] [1:chain:AgentExecutor > 2:llm:ChatOpenAI] [1.97s] Exiting LLM run with output: {
"generations": [
[
{
"text": "",
"message": {
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"AIMessage"
],
"kwargs": {
"content": "",
"additional_kwargs": {
"function_call": {
"name": "search",
"arguments": "{\n \"input\": \"current weather in New York\"\n}"
}
}
}
}
}
]
],
"llmOutput": {
"tokenUsage": {
"completionTokens": 18,
"promptTokens": 121,
"totalTokens": 139
}
}
}
[agent/action] [1:chain:AgentExecutor] Agent selected action: {
"tool": "search",
"toolInput": {
"input": "current weather in New York"
},
"log": ""
}
[tool/start] [1:chain:AgentExecutor > 3:tool:SerpAPI] Entering Tool run with input: "current weather in New York"
[tool/end] [1:chain:AgentExecutor > 3:tool:SerpAPI] [1.90s] Exiting Tool run with output: "1 am · Feels Like72° · WindSSW 1 mph · Humidity89% · UV Index0 of 11 · Cloud Cover79% · Rain Amount0 in ..."
[llm/start] [1:chain:AgentExecutor > 4:llm:ChatOpenAI] Entering LLM run with input: {
"messages": [
[
{
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"SystemMessage"
],
"kwargs": {
"content": "You are a helpful AI assistant.",
"additional_kwargs": {}
}
},
{
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"HumanMessage"
],
"kwargs": {
"content": "What is the weather in New York?",
"additional_kwargs": {}
}
},
{
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"AIMessage"
],
"kwargs": {
"content": "",
"additional_kwargs": {
"function_call": {
"name": "search",
"arguments": "{\"input\":\"current weather in New York\"}"
}
}
}
},
{
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"FunctionMessage"
],
"kwargs": {
"content": "1 am · Feels Like72° · WindSSW 1 mph · Humidity89% · UV Index0 of 11 · Cloud Cover79% · Rain Amount0 in ...",
"name": "search",
"additional_kwargs": {}
}
}
]
]
}
[llm/end] [1:chain:AgentExecutor > 4:llm:ChatOpenAI] [3.33s] Exiting LLM run with output: {
"generations": [
[
{
"text": "The current weather in New York is 72°F with a wind speed of 1 mph coming from the SSW. The humidity is at 89% and the UV index is 0 out of 11. The cloud cover is 79% and there has been no rain.",
"message": {
"lc": 1,
"type": "constructor",
"id": [
"langchain",
"schema",
"AIMessage"
],
"kwargs": {
"content": "The current weather in New York is 72°F with a wind speed of 1 mph coming from the SSW. The humidity is at 89% and the UV index is 0 out of 11. The cloud cover is 79% and there has been no rain.",
"additional_kwargs": {}
}
}
}
]
],
"llmOutput": {
"tokenUsage": {
"completionTokens": 58,
"promptTokens": 180,
"totalTokens": 238
}
}
}
[chain/end] [1:chain:AgentExecutor] [7.73s] Exiting Chain run with output: {
"output": "The current weather in New York is 72°F with a wind speed of 1 mph coming from the SSW. The humidity is at 89% and the UV index is 0 out of 11. The cloud cover is 79% and there has been no rain."
}