-
Notifications
You must be signed in to change notification settings - Fork 135
Add Bedrock Agent example #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 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 | ||
|
||
WORKDIR /app | ||
ADD . . | ||
RUN pip install -r requirements.txt | ||
|
||
CMD ["python", "main.py"] | ||
``` | ||
|
||
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 | ||
``` | ||
|
||
Create a bedrock agent. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html | ||
)) | ||
|
||
## Test | ||
|
||
Test your agent. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-test.html)) | ||
|
||
## Run the docker locally | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section could be removed and replaced with how to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove "Run the docker locally" section. |
||
|
||
We can run the same docker image locally, so that we know it can be deployed to ECS Fargate and EKS EC2 without code changes. | ||
|
||
```shell | ||
$ docker run -d -p 8000:8000 {ECR Image} | ||
|
||
``` | ||
|
||
Use curl to verify the docker container works. | ||
|
||
```shell | ||
$ curl localhost:8000/ | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add ENV PORT=8000 AWS_LWA_READINESS_CHECK_PROTOCOL=tcp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
WORKDIR /var/task | ||
COPY requirements.txt ./ | ||
RUN python -m pip install -r requirements.txt | ||
COPY *.py ./ | ||
CMD exec uvicorn --port=$PORT main:app |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before testing, we should show how to get the openapi spec from the FastAPI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add "Generate OpenAPI schema" section