Pour tester votre mise à jour, exécutez le code suivant pour invoquer Anthropic Claude 2.1 :
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)}
Remarque : Remplacez region_name et endpoint_url par les informations relatives à la région AWS pour votre Amazon Bedrock.