diff --git a/backend/backend_stack.py b/backend/backend_stack.py index 7b62091..9c8a74c 100644 --- a/backend/backend_stack.py +++ b/backend/backend_stack.py @@ -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 @@ -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", @@ -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") diff --git a/backend/lambda/backend_lambda.py b/backend/lambda/backend_lambda.py index cdf8e95..4c5432c 100644 --- a/backend/lambda/backend_lambda.py +++ b/backend/lambda/backend_lambda.py @@ -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']) } diff --git a/backend/model_stack.py b/backend/model_stack.py deleted file mode 100644 index ee485ea..0000000 --- a/backend/model_stack.py +++ /dev/null @@ -1,9 +0,0 @@ -from aws_cdk import Stack -from constructs import Construct - - -class ModelStack(Stack): - def __init__(self, scope: Construct, id: str, **kwargs): - super().__init__(scope, id, **kwargs) - - \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index b9c83d2..c057c6c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -86,6 +86,44 @@ jsii = ">=1.93.0,<2.0.0" publication = ">=0.0.3" typeguard = ">=2.13.3,<2.14.0" +[[package]] +name = "boto3" +version = "1.34.9" +description = "The AWS SDK for Python" +optional = false +python-versions = ">= 3.8" +files = [ + {file = "boto3-1.34.9-py3-none-any.whl", hash = "sha256:8e48343d52389041af053992decf651bc4fc7b2d65eca12acdcff62d446ecdf4"}, + {file = "boto3-1.34.9.tar.gz", hash = "sha256:18c386a55e461749e6c9c8a10627a230db18a20dd72f2950ce19546974f15cd5"}, +] + +[package.dependencies] +botocore = ">=1.34.9,<1.35.0" +jmespath = ">=0.7.1,<2.0.0" +s3transfer = ">=0.10.0,<0.11.0" + +[package.extras] +crt = ["botocore[crt] (>=1.21.0,<2.0a0)"] + +[[package]] +name = "botocore" +version = "1.34.9" +description = "Low-level, data-driven core of boto 3." +optional = false +python-versions = ">= 3.8" +files = [ + {file = "botocore-1.34.9-py3-none-any.whl", hash = "sha256:b40f027f371a1bd211ef67a3727c74bc3713af5fc5d830d4587abda296ebb19e"}, + {file = "botocore-1.34.9.tar.gz", hash = "sha256:2cf43fa5b5438a95fc466c700f3098228b45df38e311103488554b2334b42ee3"}, +] + +[package.dependencies] +jmespath = ">=0.7.1,<2.0.0" +python-dateutil = ">=2.1,<3.0.0" +urllib3 = {version = ">=1.25.4,<2.1", markers = "python_version >= \"3.10\""} + +[package.extras] +crt = ["awscrt (==0.19.19)"] + [[package]] name = "cattrs" version = "23.2.3" @@ -162,6 +200,17 @@ files = [ {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, ] +[[package]] +name = "jmespath" +version = "1.0.1" +description = "JSON Matching Expressions" +optional = false +python-versions = ">=3.7" +files = [ + {file = "jmespath-1.0.1-py3-none-any.whl", hash = "sha256:02e2e4cc71b5bcab88332eebf907519190dd9e6e82107fa7f83b1003a6252980"}, + {file = "jmespath-1.0.1.tar.gz", hash = "sha256:90261b206d6defd58fdd5e85f478bf633a2901798906be2ad389150c5c60edbe"}, +] + [[package]] name = "jsii" version = "1.93.0" @@ -253,6 +302,23 @@ files = [ [package.dependencies] six = ">=1.5" +[[package]] +name = "s3transfer" +version = "0.10.0" +description = "An Amazon S3 Transfer Manager" +optional = false +python-versions = ">= 3.8" +files = [ + {file = "s3transfer-0.10.0-py3-none-any.whl", hash = "sha256:3cdb40f5cfa6966e812209d0994f2a4709b561c88e90cf00c2696d2df4e56b2e"}, + {file = "s3transfer-0.10.0.tar.gz", hash = "sha256:d0c8bbf672d5eebbe4e57945e23b972d963f07d82f661cabf678a5c88831595b"}, +] + +[package.dependencies] +botocore = ">=1.33.2,<2.0a.0" + +[package.extras] +crt = ["botocore[crt] (>=1.33.2,<2.0a.0)"] + [[package]] name = "six" version = "1.16.0" @@ -290,7 +356,24 @@ files = [ {file = "typing_extensions-4.9.0.tar.gz", hash = "sha256:23478f88c37f27d76ac8aee6c905017a143b0b1b886c3c9f66bc2fd94f9f5783"}, ] +[[package]] +name = "urllib3" +version = "2.0.7" +description = "HTTP library with thread-safe connection pooling, file post, and more." +optional = false +python-versions = ">=3.7" +files = [ + {file = "urllib3-2.0.7-py3-none-any.whl", hash = "sha256:fdb6d215c776278489906c2f8916e6e7d4f5a9b602ccbcfdf7f016fc8da0596e"}, + {file = "urllib3-2.0.7.tar.gz", hash = "sha256:c97dfde1f7bd43a71c8d2a58e369e9b2bf692d1334ea9f9cae55add7d0dd0f84"}, +] + +[package.extras] +brotli = ["brotli (>=1.0.9)", "brotlicffi (>=0.8.0)"] +secure = ["certifi", "cryptography (>=1.9)", "idna (>=2.0.0)", "pyopenssl (>=17.1.0)", "urllib3-secure-extra"] +socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] +zstd = ["zstandard (>=0.18.0)"] + [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "ff9ef0386cafd33b24732b7ed6029b48f9df41b66582825fded1c1ace0eb8129" +content-hash = "22d554b65f2701a6ef6f30428b6da4a38e7c4b0ca82d1da39a8522fc29847ff1" diff --git a/pyproject.toml b/pyproject.toml index 0982107..5eb00d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]