Buat model kustom (AWS SDKs) - Amazon Bedrock

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Buat model kustom (AWS SDKs)

Untuk membuat model kustom dari model Amazon Nova SageMaker terlatih AI yang disimpan di Amazon S3, Anda menggunakan operasi API CreateCustomModel. Anda dapat menggunakan kode berikut untuk membuat model kustom dengan SDK for Python (Boto3). Kode membuat model khusus dan kemudian memeriksa statusnya hingga model siap ACTIVE digunakan.

Untuk menggunakan kode, perbarui parameter berikut. Contoh kode juga mencakup parameter opsional seperti clientRequestToken untuk idempotensi dan modelTags untuk penandaan sumber daya.

  • ModelName — Berikan model nama yang unik.

  • S3uri — Tentukan jalur ke bucket Amazon S3 yang dikelola Amazon yang menyimpan artefak model Anda. SageMaker AI menciptakan bucket ini saat Anda menjalankan pekerjaan pelatihan SageMaker AI pertama Anda.

  • RoLearn — Tentukan Nama Sumber Daya Amazon (ARN) dari peran layanan IAM yang diasumsikan Amazon Bedrock untuk melakukan tugas atas nama Anda. Untuk informasi lebih lanjut tentang pembuatan peran, lihat Buat peran layanan untuk mengimpor model pra-terlatih.

  • modelKmsKeyArn (opsional) - Tentukan AWS KMS kunci untuk mengenkripsi model di Amazon Bedrock. Jika Anda tidak memberikan AWS KMS kunci, Amazon Bedrock menggunakan AWS KMS kunci AWS-managed untuk mengenkripsi model. Untuk informasi tentang enkripsi, lihatEnkripsi model kustom yang diimpor.

Setelah Anda membuat model kustom, model muncul dalam ListCustomModelsrespons dengan a customizationType dariimported. Untuk melacak status model baru, Anda menggunakan operasi GetCustomModelAPI.

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()