Backend service for retrieval-augmented generation (RAG) using AWS Bedrock, Superduper, and MongoDB Atlas Vector Search, implemented in Python.
This project leverages MongoDB Atlas Vector Search to efficiently index and search unstructured data, providing fast and relevant retrieval of information. MongoDB Atlas offers robust and scalable database solutions, making it ideal for handling large volumes of data and complex queries.
- Python
>=3.10,<3.11
for the Backend implementation. - MongoDB Atlas Vector Search for efficient data indexing and retrieval.
- AWS Bedrock models for embeddings and querying.
- Cohere English V3
cohere.embed-english-v3
model for embeddings. - Anthropic Claude 3 Haiku
anthropic.claude-3-haiku-20240307-v1:0
model for chat completions. - Using Superduper for simplifying the integration of AI with MongoDB databases.
Before you begin, ensure you have met the following requirements:
- MongoDB Atlas account, you can create one here. Free tier is sufficient for this project.
- Python 3.10 or higher
>=3.10,<3.11
(but less than 3.11). - AWS Account with access to mentioned Bedrock models.
- Poetry (install via Poetry's official documentation).
- Log in to MongoDB Atlas and create a new database named
your_database_pdf_rag
. You can use another name if you prefer, but make sure to update the environment variables accordingly. You can use the free tier for this project. - Inside this database, create a collection called
default
.
Note: Create a .env file within the backend directory.
MONGODB_URI = "mongodb+srv://<REPLACE_USERNAME>:<REPLACE_PASSWORD>@<REPLACE_CLUSTER_NAME>.mongodb.net/<REPLACE_DATABASE_NAME>"
ARTIFACT_STORE = "data/your_project/your_demo/artifacts"
AWS_REGION = "<REPLACE_AWS_REGION>"
AWS_ACCESS_KEY_ID = "<REPLACE_AWS_ACCESS_KEY>"
AWS_SECRET_ACCESS_KEY = "<REPLACE_AWS_SECRET_ACCESS_KEY>"
AWS_S3_ENABLED = False
AWS_S3_BUCKET =
AWS_S3_PDF_FOLDER =
PDF_FOLDER = "data/your_project/your_demo/pdfs"
PDFS = ["personal-banking-terms-conditions.pdf"] # Add the PDFs you want to index, make sure they are in the PDF_FOLDER
EMBEDDING_MODEL = "cohere.embed-english-v3"
CHAT_COMPLETION_MODEL = "anthropic.claude-3-haiku-20240307-v1:0"
ORIGINS=http://localhost:3000
- Create an AWS account if you don't have one.
- Add the AWS Access Key ID and Secret Access Key to the environment variables in the
.env
file. - Grant the necessary permissions to the AWS account:
AmazonBedrockFullAccess
, but in case you enable S3, you will need to addAmazonS3FullAccess
andSecretsManagerReadWrite
permissions. There are some varibles in the.env
file that you can use to enable or disable the S3 integration.
- (Optional) Set your project description and author information in the
pyproject.toml
file:description = "Your Description" authors = ["Your Name <you@example.com>"]
- Open the project in your preferred IDE (the standard for the team is Visual Studio Code).
- Open the Terminal within Visual Studio Code.
- Ensure you are in the root project directory where the
makefile
is located. - Execute the following commands:
- Poetry start
make poetry_start
- Poetry install
make poetry_install
- Verify that the
.venv
folder has been generated within the/backend
directory. - Make sure to select the Python interpreter from the
.venv
folder. You can change this in Visual Studio Code by clicking on the Python version in the bottom left corner, or searching byPython: Select Interpreter
in the command palette. For this project, the Python interpreter should be located at./backend/.venv/bin/python
.
Start the server by running the following commands:
- Make sure to be over
/backend
directory.cd backend
- Start the server.
poetry run uvicorn main:app --host 0.0.0.0 --port 8000
Note: The server will be running on http://localhost:8000
.
Ensure that you leave the following folders empty: data/your_project/your_demo/artifacts
and data/your_project/your_demo/pdf-images
. Delete any existing folders and files in these folders.
-
Use the following endpoints to interact with the API:
POST /cleandb
: Make a request to clean the database.- Body:
{ "industry": "your_project", "demo_name": "your_demo" }
.
- Body:
POST /setuprag
: Make a request to set up the RAG model.- Body:
{ "industry": "your_project", "demo_name": "your_demo" }
.
- Body:
POST /querythepdf
: Once the RAG model is set up, make a request to query the PDFs.- Body:
{ "industry": "your_project", "demo_name": "your_demo", "guidelines": "personal-banking-terms-conditions.pdf", "query": "Am I going to be notified when overdraft interests will be charged?" }
.
- Body:
Note: Above requests are made to the local server, so make sure the server is running. Make sure to execute the requests in the order mentioned above. Once the RAG model is set up, you can query the PDFs multiple times.
- Where you have the frontend code, create a
.env.local
file. - In the
frontend
folder, create a.env.local
file. - Add the URL for the API using the following format:
NEXT_PUBLIC_ASK_THE_PDF_API_URL="http://localhost:8000/querythepdf"
Prerequisites:
- Docker Desktop installed on your machine.
- Docker Desktop running on your machine.
- To run with Docker use the following command:
make build
- To delete the container and image run:
make clean
You can imitate the same steps as mentioned above to interact with the API.
- Check that you've created an
.env
file that contains your valid (and working) API keys, environment and index variables.