跳到主要内容
完整文档请参见此处:https://docs.aws.amazon.com/lambda/index.html AWS Lambda 是亚马逊网络服务 (AWS) 提供的一项无服务器计算服务,旨在让开发人员无需预置或管理服务器即可构建和运行应用程序和服务。这种无服务器架构使您能够专注于编写和部署代码,而 AWS 会自动处理运行应用程序所需的基础设施的扩展、修补和管理。 通过在提供给 Agent 的工具列表中包含 AWSLambda,您可以授予您的 Agent 调用在 AWS 云中运行的代码的能力,以满足您的任何需求。 当 Agent 使用 AWSLambda 工具时,它将提供一个类型为 string 的参数,该参数将通过 event 参数传递到 Lambda 函数中。 本快速入门将演示 Agent 如何使用 Lambda 函数通过 Amazon Simple Email Service 发送电子邮件。发送电子邮件的 Lambda 代码未提供,但如果您想了解如何实现,请参阅 此处。请记住,这是一个刻意简化的示例;Lambda 可以用于执行近乎无限数量的其他目的的代码(包括执行更多的 Langchain)!

关于凭据的说明

  • 如果您尚未通过 AWS CLI 运行 aws configure,则必须向 AWSLambda 构造函数提供 regionaccessKeyIdsecretAccessKey
  • 与这些凭据对应的 IAM 角色必须具有调用 Lambda 函数的权限。
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/openai @langchain/core
import { OpenAI } from "@langchain/openai";
import { SerpAPI } from "@langchain/classic/tools";
import { AWSLambda } from "@langchain/classic/tools/aws_lambda";
import { initializeAgentExecutorWithOptions } from "@langchain/classic/agents";

const model = new OpenAI({ temperature: 0 });
const emailSenderTool = new AWSLambda({
  name: "email-sender",
  // tell the Agent precisely what the tool does
  description:
    "Sends an email with the specified content to testing123@gmail.com",
  region: "us-east-1", // optional: AWS region in which the function is deployed
  accessKeyId: "abc123", // optional: access key id for a IAM user with invoke permissions
  secretAccessKey: "xyz456", // optional: secret access key for that IAM user
  functionName: "SendEmailViaSES", // the function name as seen in AWS Console
});
const tools = [emailSenderTool, new SerpAPI("api_key_goes_here")];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
  agentType: "zero-shot-react-description",
});

const input = `Find out the capital of Croatia. Once you have it, email the answer to testing123@gmail.com.`;
const result = await executor.invoke({ input });
console.log(result);

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.