How do I upgrade boto3 and botocore in AWS Lambda to access newer AI models?
I want to install the latest version of boto3 and botocore in AWS Lambda to access newer AI models in Amazon Bedrock.
Short description
If you use a Lambda function with Python to invoke an Amazon Bedrock model, then you might receive the following error: Error: "errorMessage": "Unknown service: 'bedrock'. To resolve this issue, you must upgrade the boto3 and botocore libraries to the newest versions in Lambda.
Resolution
Prerequisites
Before you begin, make sure you can access the Amazon Bedrock foundation models.
Note: When you use Amazon Bedrock foundation models, you are subject to the seller's pricing terms.
Create a Lambda layer
The following AWS Command Line Interface (AWS CLI) commands work for Linux, Unix, and macOS operating systems.
Note: If you receive errors when you run AWS CLI commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
-
Create a temporary folder:
LIB_DIR=boto3-mylayer/python mkdir -p $LIB_DIR
Note: Replace boto3-mylayer with your temporary folder name.
-
Install the boto3 library to **LIB_DIR:
**pip3 install boto3==1.34.44 -t $LIB_DIR pip3 install botocore==1.34.44 -t $LIB_DIR
-
Zip all dependencies to /tmp/boto3-mylayer.zip:
cd boto3-mylayer zip -r /tmp/boto3-mylayer.zip .
Note: Replace boto3-mylayer with your temporary folder name.
-
To publish the layer, run the publish-layer-version command:
aws lambda publish-layer-version --layer-name boto3-mylayer --zip-file fileb:///tmp/boto3-mylayer.zip
Note: Replace layer-name with your Lambda layer name and boto3-mylayer with your temporary folder name.
-
When you publish the layer, you receive the layer's ARN. Copy the ARN into a text file so that you can use it later in this procedure.
Create a Lambda function
- Create a Lambda function.
- To attach the layer you created to the Lambda function, run the update-function-configuration command:
Note: Replace layer_ARN with the layer ARN that you received.aws lambda update-function-configuration --function-name MY_FUNCTION --layer_ARN
- To test your update, run the following code to invoke Anthropic Claude 2.1:
Note: Replace region_name and endpoint_url with the information for the AWS Region for your Amazon Bedrock.import boto3 import json import os def lambda_handler(event, context): print("Boto3 version:", boto3.__version__) bedrock = boto3.client(service_name='bedrock', region_name='us-east-1', endpoint_url='https://bedrock.us-east-1.amazonaws.com') bedrock_runtime = boto3.client(service_name='bedrock-runtime', region_name='us-east-1', endpoint_url='https://bedrock-runtime.us-east-1.amazonaws.com') models=bedrock.list_foundation_models() modelIds = [model['modelId'] for model in models['modelSummaries']] print("Models: ", modelIds) for required_field in ["model"]: if required_field not in event: return {'statusCode': 400, 'body': f'ERROR: MISSING REQUEST PARAMETER {required_field}'} #event = {"model":"anthropic.claude-v2:1", "prompt": "Why is the sky blue?", "max_tokens_to_sample": 4000, "temperature": 0.5, "top_k": 250, "top_p": 1, "stop_sequences": ["Command:"]} print(f"EVENT: {event}") bedrock_model = event.pop("model") print(f"BEDROCK_MODEL: {bedrock_model}") if bedrock_model not in modelIds: return {'statusCode': 400, 'body': f'ERROR: INVALID MODEL {bedrock_model} REQUESTED. SUPPORTED MODELS: {modelIds}'} if "claude" in bedrock_model: event["prompt"] = f'Human: {event["prompt"]}\n\nAssistant:' bedrock_str = json.dumps(event) print(f"BEDROCK_STR: {bedrock_str}") modelId = 'anthropic.claude-v2:1' bodyprompt = {"prompt":"\n\nHuman:who is the prime minister of India\n\nAssistant:","max_tokens_to_sample":42,"temperature":0.5,"top_k":250,"top_p":1,"anthropic_version":"bedrock-2023-05-31"} response = bedrock_runtime.invoke_model(body=bedrock_str, modelId=modelId, accept='application/json', contentType='application/json') #response = bedrock.invoke_model(body= json.dumps(bodyprompt), modelId=bedrock_model, accept='application/json', contentType='application/json') response_body = json.loads(response.get('body').read()) print(response_body) return {'statusCode': 200, 'body': json.dumps(response_body)}
相關內容
- 已提問 1 年前lg...
- AWS 官方已更新 22 天前
- AWS 官方已更新 1 年前
- AWS 官方已更新 4 年前