A practical example of building a chatbot service using Amazon Bedrock.
The service exposes a REST API backed by Python and FastAPI, which can be embedded as a chatbot engine for your front-ends. For example, it can be used in the react-tailwind-chatbot-client
that is the part of this project.
Serverless architecture of the service allows you to pay for the usage as you go. Amazon Bedrock LLM models charge per tokens, AWS Lambda charges per request.
The API retains chat history by storing each new chat session in DynamoDB and associating sessions with an ID. Using the ID, users can reference the context of previous messages.
Additionally, each new chat session receives a short title that gives an idea about the topic.
After launching the service, you should be able to access the beautiful auto-generated Swagger UI Docs for the REST API:
Projects with boto3
usually require configured AWS credentials. You can install the AWS CLI and set up an AWS profile on your machine to make the project work locally. Refer to the AWS CLI Installation Guide and the AWS CLI Configuration Guide for more details.
Otherwise, the SAM project should automatically create the necessary roles to access AWS resources when it comes to deployment. Refer to the Deployment section for more details.
-
Request Access to Amazon Bedrock Foundation Models: Navigate to the Amazon Bedrock Getting Started Guide. Follow the instructions on the page to request access to the following models:
- Amazon Titan Text G1 - Lite
- Amazon Titan Text G1 - Express
- Mistral AI Mistral Large (Used by this project)
- Mistral AI Mistral Small
-
Clone the repository:
git clone https://github.com/mrnexeon/amazon-bedrock-fastapi.git
-
Set up the virtual environment: It is recommended to set up a Python virtual environment.
python3 -m venv venv source venv/bin/activate # On Windows use `.\venv\Scripts\activate`
-
Install dependencies: It is recommended to setup the Python virtual enviroment.
python3 -m pip install -r app/requirements.txt
-
Launch the server:
fastapi dev
-
Test the API: Open your browser and go to http://127.0.0.1:8000/docs to access the interactive API documentation provided by FastAPI Swagger UI.
The service uses AWS SAM to deploy the API as a serverless AWS Lambda function. SAM is an extension of the AWS CLI that adds functionality for deploying applications and services. The Lambda function resource is defined in the template.yaml
file in this project.
-
Install AWS CLI: Follow the instructions to install the AWS CLI from this link.
-
Configure AWS CLI: Refer to the AWS CLI Configuration Guide to configure your AWS CLI.
-
Install SAM CLI: Follow the instructions to install the SAM CLI from this link.
-
Build the Service: Open your shell and run:
sam build
-
Deploy the Service: In your shell, run:
sam deploy --guided
-
Test the API: Watch out in the SAM deployment logs for the URL endpoints, e.g.:
Key Endpoint Description URL of the REST API endpoint Value https://mf6fo...pztt.lambda-url.us-east-1.on.aws/ Key RestApiDocs Description Swagger UI REST API documentation Value https://mf6fo...pztt.lambda-url.us-east-1.on.aws/docs