AI Chatbot build with OpenAI & FastAPI running on AWS Elastic Container Service Fargate

Shadab Mohammad
Towards Dev
Published in
5 min readApr 9, 2024

--

A conversational AI Chatbot build using Python3, OpenAI and FastAPI

Generative AI has been all the rage since ChatGPT was launched in late-2022. OpenAI provides Python SDK to interact with the ChatGPT service in addition to the console. We will use those API’s and the Python SDK for OpenAI to build a chatbot.

The static front-end of the chatbot will be build using Javascript, HTML and CSS and the backend will be build using FastAPI, a very popular microframework in Python3. All of this will be build as a docker image and deployed on AWS Elastic Container Service using Fargate, which is the serverless offering of ECS.

Before we start a few pre-requisites :

  1. You have basic knowledge of Python programming, Docker and AWS
  2. You have access to a paid OpenAI account which gives you an API Key
  3. You have an AWS account (obviously!)
  4. You have an hour of fun-time parked aside to do something cool
Image Credit : Amazon Web Services

All of the code in this blog post is available in this Github Repo : https://github.com/shadabshaukat/AI-Chatbot.git

We will build this in 3stages :

Stage 1 : Build a local Docker image on your workstation and Test

Stage 2 : Build a Elastic Container Registry Repository and Push to your AWS Account

Stage 3 : Build the ECR Cluster, Service, Task Definition and launch the container in your AWS account

So let’s dive in…

Stage 1: Test and Build Docker Container Locally

Clone Repository

git clone https://github.com/shadabshaukat/AI-Chatbot.git && cd AI-Chatbot/

Edit the .env file and Add your GPT model and OpenAI API Key

vim .env

OPENAI_API_KEY=Paste-Your-OpenAI-API-Key-Here
OPENAI_MODEL=gpt-3.5-turbo

Build the Container Image

docker build -t my-ai-chatbot-app .

Run the Container

docker run -d -p 8000:8000 --env-file .env my-ai-chatbot-app

Open AI Chatbot from Browser

http://localhost:8000/static/index.html

Check Status of the Container (for troubleshooting)

docker container ls
docker logs container_id
docker inspect container_id
docker stats container_id
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id

Now that local deployment is successful, lets move to stage 2 and configure the Elastic Container Registry

Stage 2: Deploy Docker Container to your AWS Account Elastic Container Registry

Configure aws-cli credentials for your AWS account and create a public repository in Elastic Container Registry in your region (First 50GB of storage is free in us-east-1)

If you want to create a private repository you can use aws-cli:

aws ecr create-repository --repository-name ai-chatbot --region us-east-1

Else for this demo, we will create a public repository from the AWS console called ai-demo

View Push Commands:

1. Authenticate with Amazon ECR

First, you need to authenticate your Docker client with the ECR registry to push and pull images.


git clone https://github.com/shadabshaukat/AI-Chatbot.git && cd AI-Chatbot/
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/h9e6h3n3

WARNING! Your password will be stored unencrypted in /Users/shadab/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Replace with your-region and your-account-id with your AWS region and account ID as per your push commands.

2. Build the Docker Image

docker build -t ai-demo .

3. Tag your image so you can push the image to this repository:

docker tag ai-demo:latest public.ecr.aws/h9e6h3n3/ai-demo:latest

4. Run the following command to push this image to your newly created AWS repository:

docker push public.ecr.aws/h9e6h3n3/ai-demo:latest

Stage 3: Deploy ECR Fargate Cluster, Service, Tasks and Run the Container

1. Create ECS Cluster from AWS Console

2. Register a Task Definition

Go to > AWS Console > Elastic Container Service > Task Definition > Create a new task definition

Add 2 environment variables to the task definition : OPENAI_API_KEY, OPENAI_MODEL

You can leave the other options as default and create the task definition.

Click on deploy > create service

3. Create a Service using the Task Definition

4. Create the service and view the service after creation

Check Service Tasks to see if the Service running, this is available from the Tasks tab

Verify from theLogs tab to check container is running without any issues

5. Check the task to get the public IP of the running container

6. Open the AI Chatbot URL with the public IP

http://54.206.91.62:8000/static/index.html

If everything went smoothly then your Chatbot should appear on the screen.

Now it’s time to chat to the bot with my favorite question :)

Bellisimo!

--

--

Cloud Solutions Architect@Oracle (The statements and opinions expressed here are my own & do not necessarily represent those of my employer)