跳到主要内容
AWS Step Functions 是一种可视化工作流服务,可帮助开发人员使用 AWS 服务构建分布式应用程序、自动化流程、编排微服务以及创建数据和机器学习 (ML) 管道。 通过在提供给 Agent 的工具列表中包含 AWSSfn 工具,您可以授权 Agent 调用在 AWS 云中运行的异步工作流。 当 Agent 使用 AWSSfn 工具时,它将提供一个 string 类型的参数,该参数将传递给该工具支持的某个操作。支持的操作包括:StartExecutionDescribeExecutionSendTaskSuccess

设置

您需要安装 Node AWS Step Functions SDK
npm
npm install @aws-sdk/client-sfn

用法

有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/openai @langchain/community @langchain/core

关于凭据的注意事项

  • 如果您尚未通过 AWS CLI 运行 aws configure,则必须向 AWSSfn 构造函数提供 regionaccessKeyIdsecretAccessKey
  • 与这些凭据对应的 IAM 角色必须具有调用 Step Function 的权限。
import { OpenAI } from "@langchain/openai";
import {
  AWSSfnToolkit,
  createAWSSfnAgent,
} from "@langchain/community/agents/toolkits/aws_sfn";

const _EXAMPLE_STATE_MACHINE_ASL = `
{
  "Comment": "A simple example of the Amazon States Language to define a state machine for new client onboarding.",
  "StartAt": "OnboardNewClient",
  "States": {
    "OnboardNewClient": {
      "Type": "Pass",
      "Result": "Client onboarded!",
      "End": true
    }
  }
}`;

/**
 * This example uses a deployed AWS Step Function state machine with the above Amazon State Language (ASL) definition.
 * You can test by provisioning a state machine using the above ASL within your AWS environment, or you can use a tool like LocalStack
 * to mock AWS services locally. See https://localstack.cloud/ for more information.
 */
export const run = async () => {
  const model = new OpenAI({ temperature: 0 });
  const toolkit = new AWSSfnToolkit({
    name: "onboard-new-client-workflow",
    description:
      "Onboard new client workflow. Can also be used to get status of any excuting workflow or state machine.",
    stateMachineArn:
      "arn:aws:states:us-east-1:1234567890:stateMachine:my-state-machine", // Update with your state machine ARN accordingly
    region: "<your Sfn's region>",
    accessKeyId: "<your access key id>",
    secretAccessKey: "<your secret access key>",
  });
  const executor = createAWSSfnAgent(model, toolkit);

  const input = `Onboard john doe (john@example.com) as a new client.`;

  console.log(`Executing with input "${input}"...`);

  const result = await executor.invoke({ input });

  console.log(`Got output ${result.output}`);

  console.log(
    `Got intermediate steps ${JSON.stringify(
      result.intermediateSteps,
      null,
      2
    )}`
  );
};

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