diff --git a/examples/bedrock-agent-fastapi/.gitignore b/examples/bedrock-agent-fastapi/.gitignore new file mode 100644 index 00000000..4808264d --- /dev/null +++ b/examples/bedrock-agent-fastapi/.gitignore @@ -0,0 +1,244 @@ + +# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Ruby plugin and RubyMine +/.rakeTasks + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +.pytest_cache/ +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule.* + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +.history + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# Build folder + +*/build/* + +# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode \ No newline at end of file diff --git a/examples/bedrock-agent-fastapi/README.md b/examples/bedrock-agent-fastapi/README.md new file mode 100644 index 00000000..be21a06d --- /dev/null +++ b/examples/bedrock-agent-fastapi/README.md @@ -0,0 +1,76 @@ +# Agents for Amazon Bedrock with FastAPI example + +This project demonstrates the integration of "Agents for Amazon Bedrock" with a FastAPI application on AWS Lambda. It showcases how to effectively manage and process Agents for Amazon Bedrock within an serverless FastAPI application environment. + +The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yaml` file in the root folder contains the application definition. + +The top level folder is a typical AWS SAM project. The `app` directory is an FastAPI application with a [Dockerfile](app/Dockerfile). + +```dockerfile +FROM public.ecr.aws/docker/library/python:3.12.0-slim +COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.1 /lambda-adapter /opt/extensions/lambda-adapter +ENV PORT=8000 AWS_LWA_READINESS_CHECK_PROTOCOL=tcp +WORKDIR /var/task +COPY requirements.txt ./ +RUN python -m pip install -r requirements.txt +COPY *.py ./ +CMD exec uvicorn --port=$PORT main:app +``` + +Line 2 copies lambda adapter binary into /opt/extenions. This is the only change to run the FastAPI application on Lambda. + +```dockerfile +COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.1 /lambda-adapter /opt/extensions/lambda-adapter +``` + +## Pre-requisites + +The following tools should be installed and configured. +* [AWS CLI](https://aws.amazon.com/cli/) +* [SAM CLI](https://github.com/awslabs/aws-sam-cli) +* [Python](https://www.python.org/) +* [Docker](https://www.docker.com/products/docker-desktop) + +## Deploy to Lambda +Navigate to the sample's folder and use the SAM CLI to build a container image +```shell +$ sam build +``` + +This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory. + +To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen + +```shell +$ sam deploy --guided +``` + +## Generate OpenAPI schema + +Before you create your agent, you should set up action groups that you want to add to your agent. When you create an action group, you must define the APIs that the agent can invoke with an OpenAPI schema in JSON or YAML format. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-api-schema.html)) + +FastAPI can generate OpenAPI schema. + +(in app directory) + +```shell +python -c "import main;import json; print(json.dumps(main.app.openapi()))" > openapi.json +``` + + +## Create an agent. + +see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html) + +## Test locally + +Sample event exists in events directory. You can test locally bellow command. + +```shell +sam local invoke --event events/s3_bucket_count.json +``` + + +## Test + +Test your agent on Management Console. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-test.html)) diff --git a/examples/bedrock-agent-fastapi/app/Dockerfile b/examples/bedrock-agent-fastapi/app/Dockerfile new file mode 100755 index 00000000..67e37cfd --- /dev/null +++ b/examples/bedrock-agent-fastapi/app/Dockerfile @@ -0,0 +1,8 @@ +FROM public.ecr.aws/docker/library/python:3.12.0-slim +COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.1 /lambda-adapter /opt/extensions/lambda-adapter +ENV PORT=8000 AWS_LWA_READINESS_CHECK_PROTOCOL=tcp +WORKDIR /var/task +COPY requirements.txt ./ +RUN python -m pip install -r requirements.txt +COPY *.py ./ +CMD exec uvicorn --port=$PORT main:app diff --git a/examples/bedrock-agent-fastapi/app/__init__.py b/examples/bedrock-agent-fastapi/app/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/examples/bedrock-agent-fastapi/app/main.py b/examples/bedrock-agent-fastapi/app/main.py new file mode 100755 index 00000000..66690465 --- /dev/null +++ b/examples/bedrock-agent-fastapi/app/main.py @@ -0,0 +1,74 @@ +import datetime + +import boto3 +from middleware import BedrockAgentMiddleware +from fastapi import FastAPI, Query +from pydantic import BaseModel, Field + +app = FastAPI( + description="This agent allows you to query the S3 information in your AWS account.", +) +app.openapi_version = "3.0.2" +app.add_middleware(BedrockAgentMiddleware) + +s3 = boto3.resource("s3") + + +class S3BucketCountResponse(BaseModel): + count: int = Field(description="the number of S3 buckets") + + +@app.get("/s3_bucket_count") +async def get_s3_bucket_count() -> S3BucketCountResponse: + """ + This method returns the number of S3 buckets in your AWS account. + + Return: + S3BucketCountResponse: A json object containing the number of S3 buckets in your AWS account. + """ + + count = len(list(s3.buckets.all())) + + return S3BucketCountResponse(count=count) + + +class S3ObjectCountResponse(BaseModel): + count: int = Field(description="the number of S3 objects") + + +@app.get("/s3_object_count") +async def get_s3_object_count( + bucket_name: str = Query(description="Bucket name"), +) -> S3ObjectCountResponse: + """ + This method returns the number of S3 objects in your specified bucket. + + Return: + S3ObjectCountResponse: A json object containing the number of S3 objects in your specified bucket. + """ + + count = len(list(s3.Bucket(bucket_name).objects.all())) + return S3ObjectCountResponse(count=count) + + +class S3GetObjectRequest(BaseModel): + bucket_name: str = Field(description="Bucket name") + object_key: str = Field(description="Object key") + + +class S3GetObjectResponse(BaseModel): + last_modified: datetime.datetime = Field(description="the last modified date") + + +@app.post("/s3_object") +async def get_s3_object(request: S3GetObjectRequest): + """ + This method returns the last modified date of S3 object. + + Return: + S3GetObjectResponse: A json object containing the last modified date of S3 objects. + """ + + object = s3.Object(request.bucket_name, request.object_key) + last_modified = object.get()["LastModified"] + return S3GetObjectResponse(last_modified=last_modified) diff --git a/examples/bedrock-agent-fastapi/app/middleware.py b/examples/bedrock-agent-fastapi/app/middleware.py new file mode 100644 index 00000000..22f2bb8d --- /dev/null +++ b/examples/bedrock-agent-fastapi/app/middleware.py @@ -0,0 +1,82 @@ +import json +from urllib.parse import urlencode + +from fastapi import Response +from fastapi.responses import JSONResponse +from starlette.middleware.base import BaseHTTPMiddleware + + +class BedrockAgentMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request, call_next): + # pass through any non-events requests + if request.url.path != "/events": + return await call_next(request) + + # convert the request body to json object + req_body = await request.body() + req_body = json.loads(req_body) + + request.scope["path"] = req_body["apiPath"] + request.scope["method"] = req_body["httpMethod"] + + # query params + params = {} + parameters = req_body.get("parameters", []) + for item in parameters: + params[item["name"]] = item["value"] + request.scope["query_string"] = urlencode(params).encode() + + # body + content = req_body.get("requestBody", {}).get("content", None) + if content: + for key in content.keys(): + content_type = key + break + + data = {} + content_val = content.get(content_type, {}) + for item in content_val.get("properties", []): + data[item["name"]] = item["value"] + request._body = json.dumps(data).encode() + + # Pass the request to be processed by the rest of the application + response = await call_next(request) + + if isinstance(response, Response) and hasattr(response, "body"): + res_body = response.body + elif hasattr(response, "body_iterator"): + res_body = b"" + async for chunk in response.body_iterator: + res_body += chunk + response.body_iterator = self.recreate_iterator(res_body) + else: + res_body = None + # Now you have the body, you can do whatever you want with it + print(res_body) + + res_status_code = response.status_code + res_content_type = response.headers["content-type"] + + response = JSONResponse( + content={ + "messageVersion": "1.0", + "response": { + "actionGroup": req_body["actionGroup"], + "apiPath": req_body["apiPath"], + "httpMethod": req_body["httpMethod"], + "httpStatusCode": res_status_code, + "responseBody": { + res_content_type: {"body": res_body.decode("utf-8")} + }, + "sessionAttributes": req_body["sessionAttributes"], + "promptSessionAttributes": req_body["promptSessionAttributes"], + }, + } + ) + + print(response) + return response + + @staticmethod + async def recreate_iterator(body): + yield body diff --git a/examples/bedrock-agent-fastapi/app/requirements.txt b/examples/bedrock-agent-fastapi/app/requirements.txt new file mode 100755 index 00000000..d10ecd6d --- /dev/null +++ b/examples/bedrock-agent-fastapi/app/requirements.txt @@ -0,0 +1,4 @@ +boto3 + +fastapi +uvicorn[standard] diff --git a/examples/bedrock-agent-fastapi/events/s3_bucket_count.json b/examples/bedrock-agent-fastapi/events/s3_bucket_count.json new file mode 100644 index 00000000..65531a18 --- /dev/null +++ b/examples/bedrock-agent-fastapi/events/s3_bucket_count.json @@ -0,0 +1,40 @@ +{ + "messageVersion": "1.0", + "agent": { + "name": "AgentName", + "id": "AgentID", + "alias": "AgentAlias", + "version": "AgentVersion" + }, + "inputText": "InputText", + "sessionId": "SessionID", + "actionGroup": "ActionGroup", + "apiPath": "/s3_bucket_count", + "httpMethod": "GET", + "parameters": [ + { + "name": "param1", + "type": "string", + "value": "value1" + } + ], + "requestBody": { + "content": { + "application/json": { + "properties": [ + { + "name": "prop1", + "type": "string", + "value": "value1" + } + ] + } + } + }, + "sessionAttributes": { + "attr1": "value1" + }, + "promptSessionAttributes": { + "promptAttr1": "value1" + } +} \ No newline at end of file diff --git a/examples/bedrock-agent-fastapi/events/s3_object.json b/examples/bedrock-agent-fastapi/events/s3_object.json new file mode 100644 index 00000000..253fddb9 --- /dev/null +++ b/examples/bedrock-agent-fastapi/events/s3_object.json @@ -0,0 +1,40 @@ +{ + "messageVersion": "1.0", + "agent": { + "name": "AgentName", + "id": "AgentID", + "alias": "AgentAlias", + "version": "AgentVersion" + }, + "inputText": "InputText", + "sessionId": "SessionID", + "actionGroup": "ActionGroup", + "apiPath": "/s3_object", + "httpMethod": "POST", + "parameters": [ + ], + "requestBody": { + "content": { + "application/json": { + "properties": [ + { + "name": "bucket_name", + "type": "string", + "value": "bucket_001" + }, + { + "name": "object_key", + "type": "string", + "value": "object.txt" + } + ] + } + } + }, + "sessionAttributes": { + "attr1": "value1" + }, + "promptSessionAttributes": { + "promptAttr1": "value1" + } +} \ No newline at end of file diff --git a/examples/bedrock-agent-fastapi/events/s3_object_count.json b/examples/bedrock-agent-fastapi/events/s3_object_count.json new file mode 100644 index 00000000..68cdded5 --- /dev/null +++ b/examples/bedrock-agent-fastapi/events/s3_object_count.json @@ -0,0 +1,40 @@ +{ + "messageVersion": "1.0", + "agent": { + "name": "AgentName", + "id": "AgentID", + "alias": "AgentAlias", + "version": "AgentVersion" + }, + "inputText": "InputText", + "sessionId": "SessionID", + "actionGroup": "ActionGroup", + "apiPath": "/s3_object_count", + "httpMethod": "GET", + "parameters": [ + { + "name": "bucket_name", + "type": "string", + "value": "bucket_001" + } + ], + "requestBody": { + "content": { + "application/json": { + "properties": [ + { + "name": "prop1", + "type": "string", + "value": "value1" + } + ] + } + } + }, + "sessionAttributes": { + "attr1": "value1" + }, + "promptSessionAttributes": { + "promptAttr1": "value1" + } +} \ No newline at end of file diff --git a/examples/bedrock-agent-fastapi/template.yaml b/examples/bedrock-agent-fastapi/template.yaml new file mode 100755 index 00000000..d133ebe1 --- /dev/null +++ b/examples/bedrock-agent-fastapi/template.yaml @@ -0,0 +1,37 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + bedrock-agent-fastapi + + FastAPI app that work with Agents for Amazon Bedrock + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 60 + +Resources: + BedrockAgentFastAPIFunction: + Type: AWS::Serverless::Function + Properties: + PackageType: Image + MemorySize: 1024 + Policies: AmazonS3ReadOnlyAccess + Metadata: + Dockerfile: Dockerfile + DockerContext: ./app + DockerTag: python3.12-v1 + + BedrockAgentPermission: + Type: AWS::Lambda::Permission + Properties: + FunctionName: !Ref BedrockAgentFastAPIFunction + Action: lambda:InvokeFunction + Principal: bedrock.amazonaws.com + SourceAccount: !Ref 'AWS::AccountId' + SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/* + +Outputs: + BedrockAgentFastAPIFunction: + Description: "BedrockAgentFastAPIFunction Lambda Function ARN" + Value: !GetAtt BedrockAgentFastAPIFunction.Arn