diff --git a/apigw-lambda-bedrock-cdk-python/README.md b/apigw-lambda-bedrock-cdk-python/README.md index 214f2fc75..6890c7864 100644 --- a/apigw-lambda-bedrock-cdk-python/README.md +++ b/apigw-lambda-bedrock-cdk-python/README.md @@ -1,4 +1,4 @@ -# Text generation via ApiGateway -> Lambda -> Bedrock +# Text generation via Amazon API Gateway -> AWS Lambda -> Amazon Bedrock ![architecture](architecture/architecture.png) @@ -18,27 +18,27 @@ Important: this application uses various AWS services and there are costs associ ## Amazon Bedrock setup instructions You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html). -1. In the AWS console, select the region from which you want to access Amazon Bedrock. At the time of writing, Amazon Bedrock is available in us-east-1 (N. Virginia) and us-west-2 (Oregon) regions. +1. In the AWS console, select the region from which you want to access Amazon Bedrock. At the time of writing, Amazon Bedrock is available in most major regions. ![Region Selection](bedrock_setup/region-selection.png) 1. Find **Amazon Bedrock** by searching in the AWS console. - ![Bedrock Search](bedrock_setup/bedrock-search.png) + ![Amazon Bedrock Search](bedrock_setup/bedrock-search.png) 1. Expand the side menu. - ![Bedrock Expand Menu](bedrock_setup/bedrock-menu-expand.png) + ![Amazon Bedrock Expand Menu](bedrock_setup/bedrock-menu-expand.png) 1. From the side menu, select **Model access**. - ![Model Access](bedrock_setup/model-access-link.png) + ![Amazon Bedrock Model Access](bedrock_setup/model-access-link.png) 1. Select the **Edit** button. - ![Model Access View](bedrock_setup/model-access-view.png) + ![Amazon Bedrock Model Access View](bedrock_setup/model-access-view.png) -6. Use the checkboxes to select the models you wish to enable. Review the applicable EULAs as needed. Click **Save changes** to activate the models in your account. For this pattern we only need Anthropic/Claude but feel free to experiment with others. +6. Use the checkboxes to select the models you wish to enable. Review the applicable EULAs as needed. Click **Save changes** to activate the models in your account. For this pattern we only need Anthropic Claude 3 Haiku but feel free to experiment with others. ## Deployment Instructions @@ -74,14 +74,14 @@ You must request access to a model before you can use it. If you try to use the ``` cdk deploy ``` -1. After deployment completes, take a look at the Outputs section. There will be an entry containing the URL of the API Gateway resource you just created. Copy that URL as you'll need it for your tests. +1. After deployment completes, take a look at the Outputs section. There will be an entry containing the URL of the Amazon API Gateway resource you just created. Copy that URL as you'll need it for your tests. The format of the URL will be something like `https://{id}.execute-api.{region}.amazonaws.com/prod` ## How it works -CDK will create an Api Gateway, along with a resource and a POST method. There's a AWS Lambda function that will be taking the prompt and invoking an Amazon Bedrock model (anthropic.claude-v2) synchronously. If you wish to try other models, make sure to modify the policy attached to the Lambda function and invoke the right model. +CDK will create an Amazon API Gateway, along with a resource and a POST method. There is an AWS Lambda function that will be taking the prompt and invoking an Amazon Bedrock model (anthropic.claude-3-haiku-20240307-v1:0) synchronously. If you wish to try other models, make sure to modify the policy attached to the Lambda function and invoke the right model. This pattern is a synchronous pattern. For an asynchronous approach, please check [this](../apigw-rest-api-sqs-lambda-bedrock-cdk) pattern that involves the usage of Amazon SQS. @@ -107,7 +107,7 @@ Follow the example below and replace `{your-api-url}` with your api url from ste ``` ## Extra Resources -* [Bedrock Api Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) +* [Amazon Bedrock Api Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) ---- Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. diff --git a/apigw-lambda-bedrock-cdk-python/apigw_lambda_bedrock/apigw_lambda_bedrock_stack.py b/apigw-lambda-bedrock-cdk-python/apigw_lambda_bedrock/apigw_lambda_bedrock_stack.py index 57460f019..77812964a 100644 --- a/apigw-lambda-bedrock-cdk-python/apigw_lambda_bedrock/apigw_lambda_bedrock_stack.py +++ b/apigw-lambda-bedrock-cdk-python/apigw_lambda_bedrock/apigw_lambda_bedrock_stack.py @@ -12,23 +12,23 @@ class ApigwLambdaBedrockStack(Stack): def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) - #lambda layer containing boto + #AWS Lambda Layer containing boto layer = _lambda.LayerVersion(self, "Boto3Layer", code=_lambda.Code.from_asset("./boto_layer.zip"), compatible_runtimes=[_lambda.Runtime.PYTHON_3_10] ) - #add policy to invoke bedrock model + #add policy to invoke Amazon Bedrock model invoke_model_policy = iam.Policy(self, "InvokeModelPolicy", statements=[ iam.PolicyStatement( actions=["bedrock:InvokeModel"], - resources=[f"arn:aws:bedrock:{self.region}::foundation-model/anthropic.claude-v2"] + resources=[f"arn:aws:bedrock:{self.region}::foundation-model/*"] ) ] ) - # Create the Lambda function and attach the layer + # Create AWS Lambda function and attach the layer lambda_function = _lambda.Function(self, "MyFunction", runtime=_lambda.Runtime.PYTHON_3_10, handler="index.handler", @@ -39,7 +39,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: invoke_model_policy.attach_to_role(lambda_function.role) - #create api gateway + #create Amazon API Gateway api = apigw.RestApi(self, "ServerlessLandGenAI",) #create a new resource diff --git a/apigw-lambda-bedrock-cdk-python/architecture/architecture.png b/apigw-lambda-bedrock-cdk-python/architecture/architecture.png index 88ee44796..aff4e32f4 100644 Binary files a/apigw-lambda-bedrock-cdk-python/architecture/architecture.png and b/apigw-lambda-bedrock-cdk-python/architecture/architecture.png differ diff --git a/apigw-lambda-bedrock-cdk-python/bedrock_setup/model-access-view.png b/apigw-lambda-bedrock-cdk-python/bedrock_setup/model-access-view.png index 9a6ee372f..dddc1e08b 100644 Binary files a/apigw-lambda-bedrock-cdk-python/bedrock_setup/model-access-view.png and b/apigw-lambda-bedrock-cdk-python/bedrock_setup/model-access-view.png differ diff --git a/apigw-lambda-bedrock-cdk-python/function_code/index.py b/apigw-lambda-bedrock-cdk-python/function_code/index.py index ab9dccf0c..8254a4284 100644 --- a/apigw-lambda-bedrock-cdk-python/function_code/index.py +++ b/apigw-lambda-bedrock-cdk-python/function_code/index.py @@ -10,15 +10,17 @@ def handler(event, context): prompt = body.get('prompt', 'Write a text to be posted on my social media channels about how Amazon Bedrock works') body = json.dumps({ - 'prompt': "\n\nHuman:" + prompt + "\n\nAssistant:", - "temperature": 0.5, - "top_p": 1, - "top_k": 250, - "max_tokens_to_sample": 200, - "stop_sequences": ["\n\nHuman:"] + 'anthropic_version': 'bedrock-2023-05-31', + 'messages': [ + {'role': 'user', 'content': prompt} + ], + 'max_tokens': 200, + 'temperature': 0.5, + 'top_p': 1, + 'top_k': 250 }) #all parameters (except for prompt) are set to default values - modelId = 'anthropic.claude-v2' + modelId = 'anthropic.claude-3-haiku-20240307-v1:0' accept = 'application/json' contentType = 'application/json' @@ -28,6 +30,6 @@ def handler(event, context): return { 'statusCode': 200, 'body': json.dumps({ - 'generated-text': response_body + 'generated-text': response_body.get('content', '') }) - } \ No newline at end of file + }