Source code for tests.system.providers.amazon.aws.example_bedrock
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsimportjsonfromdatetimeimportdatetimefromosimportenvironimportboto3fromairflow.decoratorsimporttask,task_groupfromairflow.models.baseoperatorimportchainfromairflow.models.dagimportDAGfromairflow.operators.emptyimportEmptyOperatorfromairflow.providers.amazon.aws.hooks.bedrockimportBedrockHookfromairflow.providers.amazon.aws.operators.bedrockimport(BedrockCreateProvisionedModelThroughputOperator,BedrockCustomizeModelOperator,BedrockInvokeModelOperator,)fromairflow.providers.amazon.aws.operators.s3import(S3CreateBucketOperator,S3CreateObjectOperator,S3DeleteBucketOperator,)fromairflow.providers.amazon.aws.sensors.bedrockimport(BedrockCustomizeModelCompletedSensor,BedrockProvisionModelThroughputCompletedSensor,)fromairflow.utils.edgemodifierimportLabelfromairflow.utils.trigger_ruleimportTriggerRulefromtests.system.providers.amazon.aws.utilsimportSystemTestContextBuilder# Externally fetched variables:
# Creating a custom model takes nearly two hours. If SKIP_LONG_TASKS# is True then these tasks will be skipped. This way we can still have# the code snippets for docs, and we can manually run the full tests.
# No-commitment Provisioned Throughput is currently restricted to external# customers only and will fail with a ServiceQuotaExceededException if run# on the AWS System Test stack.
env_id=test_context["ENV_ID"]bucket_name=f"{env_id}-bedrock"input_data_s3_key=f"{env_id}/train.jsonl"training_data_uri=f"s3://{bucket_name}/{input_data_s3_key}"custom_model_name=f"CustomModel{env_id}"custom_model_job_name=f"CustomizeModelJob{env_id}"provisioned_model_name=f"ProvisionedModel{env_id}"model_arn_prefix=f"arn:aws:bedrock:{boto3.session.Session().region_name}::foundation-model/"create_bucket=S3CreateBucketOperator(task_id="create_bucket",bucket_name=bucket_name,)upload_training_data=S3CreateObjectOperator(task_id="upload_data",s3_bucket=bucket_name,s3_key=input_data_s3_key,data=json.dumps(TRAIN_DATA),)# [START howto_operator_invoke_llama_model]invoke_llama_model=BedrockInvokeModelOperator(task_id="invoke_llama",model_id=LLAMA_SHORT_MODEL_ID,input_data={"prompt":PROMPT},)# [END howto_operator_invoke_llama_model]# [START howto_operator_invoke_titan_model]invoke_titan_model=BedrockInvokeModelOperator(task_id="invoke_titan",model_id=TITAN_SHORT_MODEL_ID,input_data={"inputText":PROMPT},)# [END howto_operator_invoke_titan_model]delete_bucket=S3DeleteBucketOperator(task_id="delete_bucket",trigger_rule=TriggerRule.ALL_DONE,bucket_name=bucket_name,force_delete=True,)chain(# TEST SETUPtest_context,create_bucket,upload_training_data,# TEST BODY[invoke_llama_model,invoke_titan_model],customize_model_workflow(),provision_throughput_workflow(),# TEST TEARDOWNdelete_bucket,)fromtests.system.utils.watcherimportwatcher# This test needs watcher in order to properly mark success/failure# when "tearDown" task with trigger rule is part of the DAGlist(dag.tasks)>>watcher()fromtests.system.utilsimportget_test_run# noqa: E402# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)