Langchain Agents

Langchain Agents

文档位置:https://js.langchain.com/docs/modules/agents/

Agents

    某些应用程序需要根据用户输入对 LLM 和其他工具进行灵活的调用链。 代理接口为此类应用程序提供了灵活性。 代理可以访问一套工具,并根据用户输入确定使用哪些工具。 代理可以使用多种工具,并使用一个工具的输出作为下一个工具的输入。
    代理主要有两种类型:
  • 动作代理(action agents):在每个时间步,使用所有先前动作的输出来决定下一个动作
  • 计划并执行代理(plan-and-execute agents):预先决定完整的操作顺序,然后执行所有操作而不更新计划

动作代理适合小型任务,而计划和执行代理更适合需要维护长期目标和焦点的复杂或长期运行的任务。 通常,最好的方法是通过让计划和执行代理使用行动代理来执行计划,将行动代理的活力与计划和执行代理的规划能力结合起来。

有关代理类型的完整列表,请参阅代理类型。 代理涉及的其他抽象包括:

工具:代理可以采取的操作。 您为代理提供哪些工具很大程度上取决于您希望代理做什么
工具包:可在特定用例中一起使用的工具集合的包装。 例如,为了让代理与 SQL 数据库交互,它可能需要一个工具来执行查询,另一个工具来检查表。

Actions agents

在高层次上,行动代理:

1.接收用户输入
2.决定使用哪个工具(如果有)以及工具输入
3.调用该工具并记录输出(也称为“观察”)
4.使用工具历史记录、工具输入和观察结果决定下一步
5.重复3-4,直到确定可以直接响应用户

动作代理被包装在agent executors、链中,它们负责调用代理、返回动作和动作输入、使用生成的输入调用动作引用的工具、获取工具的输出,然后传递所有这些信息 返回代理以获取应采取的下一步操作。

尽管代理可以通过多种方式构建,但它通常涉及以下组件:

Prompt template:负责获取用户输入和之前的步骤并构建prompt,发送到语言模型.
Language model:接受使用输入和操作历史记录的提示,并决定下一步做什么
Output parser:获取语言模型的输出并将其解析为下一个动作或最终答案

Plan-and-execute agents

在高层次上,计划和执行代理:

接收用户输入
计划要采取的完整步骤顺序
按顺序执行步骤,将过去步骤的输出作为未来步骤的输入传递
最典型的实现是让规划器(planner)成为语言模型,执行器(executor)成为动作代理。 在这里阅读更多内容。

Get started

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);

/*
  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.
*/

API Reference:
initializeAgentExecutorWithOptions from langchain/agents
ChatOpenAI from langchain/chat_models/openai
SerpAPI from langchain/tools
Calculator from langchain/tools/calculator

以下是 记录的详细输出:

[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."
}

Agent types(代理类型)

代理使用 LLM 来确定采取哪些行动以及采取什么顺序。 操作可以是使用工具并观察其输出,也可以是向用户返回响应。 以下是LangChain可用的代理。

zero-shot ReAct

该代理使用 ReAct 框架仅根据工具的描述来确定要使用哪个工具。 可以提供任意数量的工具。 该代理要求为每个工具提供描述。

注意:这是最通用的动作代理。

OpenAI Functions

某些 OpenAI 模型(如 gpt-3.5-turbo-0613 和 gpt-4-0613)已经过明确微调,以检测何时应调用函数并响应应传递给函数的输入。 OpenAI Functions Agent 旨在与这些模型配合使用。

Conversational

该代理设计用于对话设置。 该提示旨在让客服人员提供帮助并进行对话。 它使用ReAct框架来决定使用哪个工具,并使用内存来记住之前的对话交互。

Plan-and-execute agents

计划和执行代理通过首先计划要做什么,然后执行子任务来实现目标。 这个想法很大程度上受到 BabyAGI 和随后的“Plan-and-Solve”论文的启发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值