翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
カスタムモデルを作成する (AWS SDKs)
Amazon S3 に保存されている SageMaker AI でトレーニングされた Amazon Nova モデルからカスタムモデルを作成するには、CreateCustomModel API オペレーションを使用します。 Amazon S3 次のコードを使用して、 SDK for Python (Boto3) でカスタムモデルを作成できます。コードはカスタムモデルを作成し、モデルがACTIVE
使用可能になるまでそのステータスをチェックします。
コードを使用するには、次のパラメータを更新します。コードサンプルには、べき等性clientRequestToken
用やリソースタグ付けmodelTags
用の などのオプションパラメータも含まれています。
-
modelName – モデルに一意の名前を付けます。
-
s3Uri – モデルアーティファクトを保存する Amazon 管理の Amazon S3 バケットへのパスを指定します。SageMaker AI は、最初の SageMaker AI トレーニングジョブを実行するときにこのバケットを作成します。
-
roleArn – Amazon Bedrock がユーザーに代わってタスクを実行するために引き受ける IAM サービスロールの Amazon リソースネーム (ARN) を指定します。このロールの作成に関する詳細については、「事前トレーニング済みモデルをインポートするためのサービスロールを作成する」をご参照ください。
-
modelKmsKeyArn (オプション) – Amazon Bedrock でモデルを暗号化する AWS KMS キーを指定します。 AWS KMS キーを指定しない場合、Amazon Bedrock は AWSマネージド AWS KMS キーを使用してモデルを暗号化します。暗号化の詳細については、「」を参照してくださいインポートされたカスタムモデルの暗号化。
カスタムモデルを作成すると、モデルは ListCustomModels レスポンスに customizationType
の が表示されますimported
。新しいモデルのステータスを追跡するには、GetCustomModel API オペレーションを使用します。
import boto3 import uuid from botocore.exceptions import ClientError import time def create_custom_model(bedrock_client): """ Creates a custom model in Amazon Bedrock from a SageMaker AI-trained Amazon Nova model stored in Amazon S3. Args: bedrock_client: The Amazon Bedrock client instance Returns: dict: Response from the CreateCustomModel API call """ try: # Create a unique client request token for idempotency client_request_token = str(uuid.uuid4()) # Define the model source configuration model_source_config = { 's3DataSource': { 's3Uri': '
s3://amzn-s3-demo-bucket/folder/
', } } # Create the custom model response = bedrock_client.create_custom_model( # Required parameters modelName='modelName
', roleArn='serviceRoleArn
', modelSourceConfig=model_source_config, # Optional parameters clientRequestToken=client_request_token, modelKmsKeyArn='keyArn
', modelTags=[ { 'key': 'Environment', 'value': 'Production' }, { 'key': 'Project', 'value': 'AIInference' } ] ) print(f"Custom model creation initiated. Model ARN: {response['modelArn']}") return response except ClientError as e: print(f"Error creating custom model: {e}") raise def list_custom_models(bedrock_client): """ Lists all custom models in Amazon Bedrock. Args: bedrock_client: An Amazon Bedrock client. Returns: dict: Response from the ListCustomModels API call """ try: response = bedrock_client.list_custom_models() print(f"Total number of custom models: {len(response['modelSummaries'])}") for model in response['modelSummaries']: print("ARN: " + model['modelArn']) print("Name: " + model['modelName']) print("Status: " + model['modelStatus']) print("Customization type: " + model['customizationType']) print("------------------------------------------------------") return response except ClientError as e: print(f"Error listing custom models: {e}") raise def check_model_status(bedrock_client, model_arn): """ Checks the status of a custom model creation. Args: model_arn (str): The ARN of the custom model bedrock_client: An Amazon Bedrock client. Returns: dict: Response from the GetCustomModel API call """ try: max_time = time.time() + 60 * 60 # 1 hour while time.time() < max_time: response = bedrock_client.get_custom_model(modelIdentifier=model_arn) status = response.get('modelStatus') print(f"Job status: {status}") if status == 'Failed': print(f"Failure reason: {response.get('failureMessage')}") break if status == 'Active': print("Model is ready for use.") break time.sleep(60) except ClientError as e: print(f"Error checking model status: {e}") raise def main(): bedrock_client = boto3.client(service_name='bedrock', region_name='REGION
') # Create the custom model model_arn = create_custom_model(bedrock_client)["modelArn"] # Check the status of the model if model_arn: check_model_status(bedrock_client, model_arn) # View all custom models list_custom_models(bedrock_client) if __name__ == "__main__": main()