模型自訂的程式碼範例 - Amazon Bedrock

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

模型自訂的程式碼範例

下列程式碼範例示範如何準備基本資料集、設定許可、建立自訂模型、檢視輸出檔案、購買模型的輸送量,以及在模型上執行推論。您可以將這些程式碼片段修改為特定的使用案例。

  1. 準備訓練資料集。

    1. 建立包含以下一行的訓練資料集檔案,並命名為 train.jsonl

      {"prompt": "what is AWS", "completion": "it's Amazon Web Services"}
    2. 為您的訓練資料建立 S3 儲存貯體,並為輸出資料建立另一個儲存貯體 (名稱必須是唯一的)。

    3. train.jsonl 上傳至訓練資料儲存貯體。

  2. 建立政策以存取您的訓練,並將其連接至具有 Amazon Bedrock 信任關係的 IAM 角色。選擇您偏好方法的索引標籤,然後遵循下列步驟:

    Console
    1. 建立 S3 政策。

      1. 導覽至位於 https://console.aws.amazon.com/iam:// 的 IAM 主控台,然後從左側導覽窗格中選擇政策

      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,然後選取建立角色

    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 請求,以建立名為 MyCustomizationRole 的 IAM 角色,並連接您建立的 BedrockTrust.json 信任政策。

      aws iam create-role \ --role-name MyCustomizationRole \ --assume-role-policy-document file://BedrockTrust.json
    5. 使用您建立的 MyFineTuningDataAccess.json 檔案提出 CreatePolicy 請求,以建立 S3 資料存取政策。回應會傳回政策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 請求,以建立名為 MyCustomizationRole 的 IAM 角色,並發出 CreatePolicy 請求,以建立名為 MyFineTuningDataAccess 的 S3 資料存取政策。針對 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=.0001,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

回應會傳回 provisionedModelArn。預留一些時間來建立佈建輸送量。若要檢查其狀態,請在下列命令provisioned-model-id中提供佈建模型的名稱或 ARN 做為 。

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

status為 時InService,您可以使用下列命令,使用自訂模型執行推論。您必須提供佈建模型的 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

執行下列程式碼片段以提交微調任務。將 ${your-customization-role-arn} 取代為您設定的 MyCustomizationRole 的 ARN,並將 ${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": ".0001", "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')

回應會傳回 provisionedModelArn。預留一些時間來建立佈建輸送量。若要檢查其狀態,請在下列命令provisionedModelId中提供佈建模型的名稱或 ARN,做為 。

bedrock.get_provisioned_model_throughput(provisionedModelId=provisionedModelArn)

status為 時InService,您可以使用下列命令,使用自訂模型執行推論。您必須提供佈建模型的 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()