Skip to content

Commit

Permalink
feat: Call Bedrock model from backend lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysiekb committed Dec 28, 2023
1 parent 6e8a818 commit 6e7aafb
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 15 deletions.
11 changes: 9 additions & 2 deletions backend/backend_stack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from aws_cdk import Stack, aws_lambda, aws_apigateway
from aws_cdk import Stack, aws_lambda, aws_apigateway, aws_iam
from constructs import Construct


Expand All @@ -14,6 +14,14 @@ def __init__(self, scope: Construct, id: str, **kwargs):
code=aws_lambda.Code.from_asset("backend/lambda"),
)

backend_lambda.add_to_role_policy(
aws_iam.PolicyStatement(
actions=["bedrock:InvokeModel"],
resources=["*"],
effect=aws_iam.Effect.ALLOW,
)
)

backend_apigw = aws_apigateway.LambdaRestApi(
scope=self,
id="ChatbotBackendAPI",
Expand All @@ -22,5 +30,4 @@ def __init__(self, scope: Construct, id: str, **kwargs):

chatbot_res = backend_apigw.root.add_resource("chatbot")

chatbot_res.add_method("GET")
chatbot_res.add_method("POST")
17 changes: 14 additions & 3 deletions backend/lambda/backend_lambda.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import json

import boto3


def handler(event, context):
print('request: {}'.format(json.dumps(event)))
body = event['body']
print('request: {}'.format(json.dumps(body)))

bedrock = boto3.client('bedrock-runtime')
response = bedrock.invoke_model(
modelId='amazon.titan-text-lite-v1',
contentType='application/json',
body=body
)

return {
'statusCode': 200,
'headers': {
'Content-Type': 'text/plain'
'Content-Type': response['contentType']
},
'body': 'Hello, CDK! You have hit {}\n'.format(event['path'])
'body': json.loads(response['body'])
}
9 changes: 0 additions & 9 deletions backend/model_stack.py

This file was deleted.

85 changes: 84 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readme = "README.md"
python = "^3.11"
aws-cdk-lib = "^2.116.1"
constructs = "^10.3.0"
boto3 = "^1.34.9"


[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 6e7aafb

Please sign in to comment.