Skip to main content

集成 AWS Lambda 的代理

¥Agent with AWS Lambda Integration

完整文档在此:https://docs.aws.amazon.com/lambda/index.html

¥Full docs here: https://docs.aws.amazon.com/lambda/index.html

AWS Lambda 是由 Amazon Web Services (AWS) 提供的无服务器计算服务,旨在让开发者无需配置或管理服务器即可构建和运行应用和服务。这种无服务器架构让你能够专注于编写和部署代码,而 AWS 会自动负责扩展、修补和管理运行应用所需的基础设施。

¥AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS), designed to allow developers to build and run applications and services without the need for provisioning or managing servers. This serverless architecture enables you to focus on writing and deploying code, while AWS automatically takes care of scaling, patching, and managing the infrastructure required to run your applications.

通过在提供给代理的工具列表中包含 AWSLambda,你可以授予代理调用在 AWS 云中运行的代码的能力,以满足你的任何需求。

¥By including a AWSLambda in the list of tools provided to an Agent, you can grant your Agent the ability to invoke code running in your AWS Cloud for whatever purposes you need.

当代理使用 AWSLambda 工具时,它将提供一个 string 类型的参数,该参数又将通过 event 参数传递给 Lambda 函数。

¥When an Agent uses the AWSLambda tool, it will provide an argument of type string which will in turn be passed into the Lambda function via the event parameter.

本快速入门将演示代理如何使用 Lambda 函数通过 Amazon 简单电子邮件服务 发送电子邮件。未提供发送电子邮件的 lambda 代码,但如果你想了解如何执行此操作,请参阅 此处。请注意,这是一个故意设计的简单示例;Lambda 可用于执行几乎无限数量的其他目的的代码(包括执行更多 Langchain)!

¥This quick start will demonstrate how an Agent could use a Lambda function to send an email via Amazon Simple Email Service. The lambda code which sends the email is not provided, but if you'd like to learn how this could be done, see here. Keep in mind this is an intentionally simple example; Lambda can used to execute code for a near infinite number of other purposes (including executing more Langchains)!

关于凭证的说明:

¥Note about credentials:

  • 如果你尚未通过 AWS CLI 运行 aws configure,则必须将 regionaccessKeyIdsecretAccessKey 提供给 AWSLambda 构造函数。

    ¥If you have not run aws configure via the AWS CLI, the region, accessKeyId, and secretAccessKey must be provided to the AWSLambda constructor.

  • 与这些凭证对应的 IAM 角色必须具有调用 lambda 函数的权限。

    ¥The IAM role corresponding to those credentials must have permission to invoke the lambda function.

npm install @langchain/openai @langchain/core
import { OpenAI } from "@langchain/openai";
import { SerpAPI } from "langchain/tools";
import { AWSLambda } from "langchain/tools/aws_lambda";
import { initializeAgentExecutorWithOptions } from "langchain/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);

¥Related