在 Amazon Bedrock 中使用 AWS Step Functions 工作流自动化模型自定

2026-01-27 13:24:28

使用 AWS Step Functions 自动化 Amazon Bedrock 模型自定义

关键要点

大语言模型在处理多种商业场景中提供智能和细致的响应,企业往往需要在原始功能基础上进行定制。Amazon Bedrock 是一个全托管服务,为客户提供来自多家领先 AI 公司的高性能基础模型。AWS 在 reInvent 2023 上宣布支持在 Amazon Bedrock 中定制模型,使客户能够使用专有数据进一步预训练模型。本文中将介绍如何利用 AWS Step Functions 自动化模型定制流程,以减少开发时间并提高效率。

随着大语言模型在为各种商业用例生成智能和丰富的响应中变得不可或缺,企业面临着如何在现有模型能力上进行自定义的挑战。为了解决这一问题,AWS 最近在 Amazon Bedrock 中引入了原生与 AWS Step Functions 的集成。这使得客户能够自动化自定义工作流,从而高效地满足他们独特的业务需求。

架构

我们将使用 Amazon Bedrock 中的 Cohere Command Light 模型进行摘要示例。本工作流也可以用于其他模型的摘要,只需传递基础模型 ID 和所需的超参数。相关阅读者,请参考 Amazon Bedrock 用户指南 以获取支持的模型列表。

架构功能概述:

功能说明用户上传训练数据用户将训练数据以 JSON Line 格式上传至 Amazon S3 存储桶启动自定义模型流程Step Functions 流程使用上传的数据和参数启动自定义模型创建和监控模型流程调用 Amazon Bedrock API 来微调模型,并监测其性能

前提条件

创建 AWS 账户。访问 AWS 管理控制台及 AWS 命令行界面AWS CLI,并确保相关 IAM 用户具备必要的权限。安装 Git。安装 AWS Serverless Application Model (AWS SAM)。安装并运行 Docker。在 Amazon Bedrock 控制台中启用 Cohere Command Light 模型访问。

演示准备

这次演示将在美国东部北维吉尼亚AWS 区域useast1进行。我们将按照以下步骤实施模型自定义工作流:

使用 AWS SAM 模板部署解决方案。上传私人训练数据至 S3 存储桶。运行 Step Functions 工作流并进行监控。查看基础模型训练结果。清理资源。

步骤 1:使用 AWS SAM 模板部署解决方案

请参考 GitHub 仓库页面获取最新的说明。按照以下步骤使用 AWS SAM 模板部署 Step Functions 工作流:

创建新目录,导航到该目录并克隆 GitHub 仓库: bash git clone https//githubcom/awssamples/amazonbedrockmodelcustomizationgit

切换至解决方案目录: bash cd amazonbedrockmodelcustomization

运行 buildsh 创建容器映像: bash bash buildsh

按提示输入参数值: markdown imagename=modelevaluation reponame=bedrockmodelcustomization awsaccount={yourAWSaccountid} awsregion={yourregion}

在 Amazon Bedrock 中使用 AWS Step Functions 工作流自动化模型自定

使用 AWS SAM 部署为模式指定的 AWS 资源: bash sam deploy guided

按提示提供以下输入: markdown Enter a stack name Enter useast1 or your AWS Region where you enabled Amazon Bedrock Cohere Command Light Model Enter SenderEmailId Once the model customization is complete email will come from this email id Enter RecipientEmailId User will be notified to this email id Enter ContainerImageURI ContainerImageURI is available from the output of the bash buildsh step

记录 SAM 部署过程的输出,这包含资源名称和/或 ARN。

步骤 2:上传私人训练数据至 S3 存储桶

将私人训练数据上传至之前创建的 S3 存储桶,并用于微调 Amazon Bedrock Cohere Command Light 模型。训练数据需采用 JSON Line 格式,每行包含有效的 JSON,具有两个属性:prompt 和 completion。

使用以下命令上传训练数据文件: bash aws s3 cp trainingdatajsonl s3//{TrainingDataBucket}/trainingdatajsonl region {yourregion}

上传验证数据至 S3 存储桶: bash aws s3 cp validationdatajson s3//{ValidationDataBucket}/validationdatajson region {yourregion}

上传参考推理数据至 S3 存储桶: bash aws s3 cp referenceinferencejson s3//{ValidationDataBucket}/referenceinferencejson region {yourregion}

确认发送者电子邮件ID的验证邮件,并按照邮件中的说明进行操作。

步骤 3:运行 Step Functions 工作流并进行监控

启动 Step Functions 状态机,以基于上传的训练数据微调 Cohere Command Light 模型,并传递超参数。

运行以下 AWS CLI 命令启动 Step Functions 工作流: bash aws stepfunctions startexecution statemachinearn {StateMachineCustomizeBedrockModelArn} input {BaseModelIdentifier coherecommandlighttextv1474kCustomModelName {UniqueModelName}JobName {UniqueJobName} HyperParameters {evalPercentage 200 epochCount 1 batchSize 8 earlyStoppingPatience 6 earlyStoppingThreshold 001 learningRate 000001}TrainingDataFileName trainingdatajsonl} region {yourregion}

使用以下命令或登录到 AWS Step Functions 控制台 检查工作流状态: bash aws stepfunctions describeexecution executionarn {executionArn} query status region {yourregion}

步骤 4:查看基础模型训练结果

在 Step Functions 工作流成功完成后,您将收到关于自定义模型质量的通知邮件。

若自定义模型质量不达标,需根据更新的训练数据或超参数重新训练基础模型。请查看 ModelInferenceBucket 内生成的推理结果。

步骤 5:清理资源

妥善停用 AWS 资源是优化成本及提升安全性的最佳实践,以下步骤将移除之前部署的基础架构组件:

删除自定义模型的 Amazon Bedrock 提供的吞吐量: bash aws bedrock deleteprovisionedmodelthroughput provisionedmodelid {ProvisionedModelArn} region {yourregion}

加速器试用1天

删除 Amazon Bedrock 自定义模型: bash aws bedrock deletecustommodel modelidentifier {CustomModelName} region {yourregion}

删除 S3 存储桶中的内容: bash aws s3 rm s3//{TrainingDataBucket} recursive region {yourregion} aws s3 rm s3//{ValidationDataBucket} recursive region {yourregion} aws s3 rm s3//{ModelInferenceBucket} recursive region {yourregion}

删除通过 AWS SAM 在 AWS 账户中部署的资源: bash sam delete

结论

本文概述了如何使用 AWS Step Functions 作为编排引擎来定制 Amazon Bedrock 模型的端到端工作流。该自动化工作流根据定制数据训练基础模型并调整超参数,最终通过电子邮件通知用户训练结果。

定制大语言模型不仅需要专业的机器学习知识,还需合适的基础设施。AWS 的 Amazon Bedrock 和 Step Functions 服务抽象了这些复杂性,使企业能够专注于他们独特的数据和用例。通过实现自动化定制和评估工作流,客户能够更快、更高效地定制符合自身需求的模型。

进一步学习

Amazon Bedrock 用户指南。AWS Step Functions 用户指南。AWS 博客关于 Amazon Bedrock 模型定制。

关于作者

Biswanath Mukherjee 是亚马逊网络服务AWS的高级解决方案架构师。他为 AWS 的大型战略客户提供技术指导,以帮助他们迁移和现代化应用。他在云架构和迁移方面有着丰富的经验,协助客户开发利用 AWS 的可扩展性、可靠性和敏捷性的创新解决方案。

最新文章