模型定制的代码示例 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

模型定制的代码示例

以下代码示例展示了如何准备基本数据集、设置权限、创建自定义模型、查看输出文件、购买模型吞吐量以及对模型运行推理。您可以根据自己的特定用例修改这些代码片段。

  1. 准备训练数据集。

    1. 创建一个包含以下一行的训练数据集文件并将其命名 train.jsonl.

      {"prompt": "what is AWS", "completion": "it's Amazon Web Services"}
    2. 为训练数据创建一个 S3 存储桶,为输出数据创建另一个 S3 存储桶(名称必须是唯一的)。

    3. 上传 train.jsonl 进入训练数据桶。

  2. 创建访问您的培训的策略,并将其附加到具有 Amazon Bedrock 信任关系的IAM角色上。选择与您选择的方法相对应的选项卡,然后按照步骤操作。

    Console
    1. 创建 S3 策略。

      1. 导航到 https://console.aws.amazon.com/ia IAM m 上的控制台,然后从左侧导航窗格中选择策略

      2. 选择创建策略,然后JSON选择打开策略编辑器

      3. 粘贴以下策略,替换 ${training-bucket} 以及 ${output-bucket} 使用您的存储桶名称,然后选择下一步

        { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::${training-bucket}", "arn:aws:s3:::${training-bucket}/*" ] }, { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::${output-bucket}", "arn:aws:s3:::${output-bucket}/*" ] } ] }
      4. 为策略命名 MyFineTuningDataAccess 然后选择创建策略

    2. 创建IAM角色并附加策略。

      1. 从左侧导航窗格中选择 “角色”,然后选择 “创建角色”。

      2. 选择 “自定义信任策略”,粘贴以下策略,然后选择 “下一步”。

        { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "bedrock.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
      3. 搜索 MyFineTuningDataAccess 您创建的策略,选中该复选框,然后选择下一步

      4. 为角色命名 MyCustomizationRole 然后选择 Create role.

    CLI
    1. 创建一个名为的文件 BedrockTrust.json 并将以下策略粘贴到其中。

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "bedrock.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
    2. 创建另一个名为的文件 MyFineTuningDataAccess.json 并将以下策略粘贴到其中,替换 ${training-bucket} 以及 ${output-bucket} 使用您的存储桶名称。

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::${training-bucket}", "arn:aws:s3:::${training-bucket}/*" ] }, { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::${output-bucket}", "arn:aws:s3:::${output-bucket}/*" ] } ] }
    3. 在终端中,导航到包含您创建的策略的文件夹。

    4. CreateRole请求创建一个名为的IAM角色 MyCustomizationRole 并附上 BedrockTrust.json 您创建的信任策略。

      aws iam create-role \ --role-name MyCustomizationRole \ --assume-role-policy-document file://BedrockTrust.json
    5. 使用CreatePolicy请求创建 S3 数据访问策略 MyFineTuningDataAccess.json 您创建的文件。该策略Arn的响应会返回一个。

      aws iam create-policy \ --policy-name MyFineTuningDataAccess \ --policy-document file://myFineTuningDataAccess.json
    6. AttachRolePolicy请求将 S3 数据访问策略附加到您的角色,将policy-arn替换为上一步响应ARN中的内容:

      aws iam attach-role-policy \ --role-name MyCustomizationRole \ --policy-arn ${policy-arn}
    Python
    1. 运行以下代码以CreateRole请求创建名为的IAM角色 MyCustomizationRoleCreatePolicy请求创建名为的 S3 数据访问策略 MyFineTuningDataAccess。 对于 S3 数据访问策略,请替换 ${training-bucket} 以及 ${output-bucket} 使用您的 S3 存储桶名称。

      import boto3 import json iam = boto3.client("iam") iam.create_role( RoleName="MyCustomizationRole", AssumeRolePolicyDocument=json.dumps({ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "bedrock.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }) ) iam.create_policy( PolicyName="MyFineTuningDataAccess", PolicyDocument=json.dumps({ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::${training-bucket}", "arn:aws:s3:::${training-bucket}/*" ] }, { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::${output-bucket}", "arn:aws:s3:::${output-bucket}/*" ] } ] }) )
    2. 响应Arn中返回。运行以下代码片段发出AttachRolePolicy请求,替换 ${policy-arn} 带着返回的Arn

      iam.attach_role_policy( RoleName="MyCustomizationRole", PolicyArn="${policy-arn}" )
  3. 选择一种语言以查看调用模型自定义API操作的代码示例。

CLI

首先,创建一个名为的文本文件 FineTuningData.json。 将下面的JSON代码复制到文本文件中,替换 ${training-bucket} 以及 ${output-bucket} 使用您的 S3 存储桶名称。

{ "trainingDataConfig": { "s3Uri": "s3://${training-bucket}/train.jsonl" }, "outputDataConfig": { "s3Uri": "s3://${output-bucket}" } }

要提交模型定制作业,请导航到包含以下内容的文件夹 FineTuningData.json 在终端中并在命令行中运行以下命令,替换 ${your-customization-role-arn} 使用您设置的模型自定义角色。

aws bedrock create-model-customization-job \ --customization-type FINE_TUNING \ --base-model-identifier arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-text-express-v1 \ --role-arn ${your-customization-role-arn} \ --job-name MyFineTuningJob \ --custom-model-name MyCustomModel \ --hyper-parameters epochCount=1,batchSize=1,learningRate=.0005,learningRateWarmupSteps=0 \ --cli-input-json file://FineTuningData.json

响应返回一个 jobArn。 留出一些时间来完成任务。您可以使用以下命令检查其状态。

aws bedrock get-model-customization-job \ --job-identifier "jobArn"

status为时COMPLETE,你可以在响应trainingMetrics中看到。您可以通过运行以下命令将构件下载到当前文件夹,替换 aet.et-bucket 使用您的输出存储桶名称和 jobId 带有自定义任务的 ID(中最后一个斜杠之后的序列jobArn)。

aws s3 cp s3://${output-bucket}/model-customization-job-jobId . --recursive

使用以下命令为您的自定义模型购买无需承诺的预配置吞吐量。

注意

此次购买将按小时向您收费。使用控制台查看不同选项的估算价格。

aws bedrock create-provisioned-model-throughput \ --model-id MyCustomModel \ --provisioned-model-name MyProvisionedCustomModel \ --model-units 1

响应返回 a provisionedModelArn。留出一段时间来创建预配置吞吐量。要检查其状态,请在以下命令ARN中提供已置备模型provisioned-model-id的名称或。

aws bedrock get-provisioned-model-throughput \ --provisioned-model-id ${provisioned-model-arn}

如果statusInService,则可以使用以下命令对自定义模型进行推理。您必须提供预配置模型ARN的作为。model-id输出将写入名为的文件 output.txt 在你当前的文件夹中。

aws bedrock-runtime invoke-model \ --model-id ${provisioned-model-arn} \ --body '{"inputText": "What is AWS?", "textGenerationConfig": {"temperature": 0.5}}' \ --cli-binary-format raw-in-base64-out \ output.txt
Python

运行以下代码片段以提交微调作业。Replace(替换) ${your-customization-role-arn} 和 ARN of MyCustomizationRole 你设置并替换的 ${training-bucket} 以及 ${output-bucket} 使用您的 S3 存储桶名称。

import boto3 bedrock = boto3.client(service_name='bedrock') # Set parameters customizationType = "FINE_TUNING" baseModelIdentifier = "arn:aws:bedrock:us-east-1::foundation-model/amazon.titan-text-express-v1" roleArn = "${your-customization-role-arn}" jobName = "MyFineTuningJob" customModelName = "MyCustomModel" hyperParameters = { "epochCount": "1", "batchSize": "1", "learningRate": ".0005", "learningRateWarmupSteps": "0" } trainingDataConfig = {"s3Uri": "s3://${training-bucket}/myInputData/train.jsonl"} outputDataConfig = {"s3Uri": "s3://${output-bucket}/myOutputData"} # Create job response_ft = bedrock.create_model_customization_job( jobName=jobName, customModelName=customModelName, roleArn=roleArn, baseModelIdentifier=baseModelIdentifier, hyperParameters=hyperParameters, trainingDataConfig=trainingDataConfig, outputDataConfig=outputDataConfig ) jobArn = response_ft.get('jobArn')

响应返回一个 jobArn。 留出一些时间来完成任务。您可以使用以下命令检查其状态。

bedrock.get_model_customization_job(jobIdentifier=jobArn).get('status')

status为时COMPLETE,你可以在GetModelCustomizationJob响应trainingMetrics中看到。您也可以按照下载对象中的步骤下载指标。

使用以下命令为您的自定义模型购买无需承诺的预配置吞吐量。

response_pt = bedrock.create_provisioned_model_throughput( modelId="MyCustomModel", provisionedModelName="MyProvisionedCustomModel", modelUnits="1" ) provisionedModelArn = response_pt.get('provisionedModelArn')

响应返回 a provisionedModelArn。留出一段时间来创建预配置吞吐量。要检查其状态,请在以下命令ARN中提供已置备模型provisionedModelId的名称或。

bedrock.get_provisioned_model_throughput(provisionedModelId=provisionedModelArn)

如果statusInService,则可以使用以下命令对自定义模型进行推理。您必须提供预配置模型ARN的作为。modelId

import json import logging import boto3 from botocore.exceptions import ClientError class ImageError(Exception): "Custom exception for errors returned by the model" def __init__(self, message): self.message = message logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_text(model_id, body): """ Generate text using your provisioned custom model. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: response (json): The response from the model. """ logger.info( "Generating text with your provisioned custom model %s", model_id) brt = boto3.client(service_name='bedrock-runtime') accept = "application/json" content_type = "application/json" response = brt.invoke_model( body=body, modelId=model_id, accept=accept, contentType=content_type ) response_body = json.loads(response.get("body").read()) finish_reason = response_body.get("error") if finish_reason is not None: raise ImageError(f"Text generation error. Error is {finish_reason}") logger.info( "Successfully generated text with provisioned custom model %s", model_id) return response_body def main(): """ Entrypoint for example. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = provisionedModelArn body = json.dumps({ "inputText": "what is AWS?" }) response_body = generate_text(model_id, body) print(f"Input token count: {response_body['inputTextTokenCount']}") for result in response_body['results']: print(f"Token count: {result['tokenCount']}") print(f"Output text: {result['outputText']}") print(f"Completion reason: {result['completionReason']}") except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) except ImageError as err: logger.error(err.message) print(err.message) else: print( f"Finished generating text with your provisioned custom model {model_id}.") if __name__ == "__main__": main()