The goal of developing this repository is to create a scalable project based on RAG operations of a vector database (Postgres with pgvector), and to expose a question-answering system developed with LangChain and FastAPI on a Next.js frontend.
The entire system will be deployed in a serverless manner, both on the backend (a Terraform submodule for setting up a cloud run with CloudSQL and Redis) and on the frontend (deployment via Vercel).
Additionally, a layer will be added to limit the app's usage through a subscription plan via Stripe
Refer to the following guide for adding and managing submodules: Adding a Submodule and Committing Changes: Git, Terraform, FastAPI
Steps to Initialize Terraform:
Navigate to the Terraform directory and initialize the configuration:
cd terraform
terraform init
terraform apply
Duplicate the .env.example
file and set the required variables:
cp .env.example .env
- Navigate to the backend directory:
cd backend
- Install dependencies using Poetry:
poetry install
poetry shell
Connect to the database using the Cloud SQL Proxy. Instructions are available in the Terraform README.
./cloud-sql-proxy ...
Run the initialization script to set up the database. This script adds the pgvector extension and creates a superuser:
python app/init_db.py
Place your PDF files in data/raw
and run the following script to populate the database:
python app/ingestion/run.py
Access live-generated API documentation at:
https://cloudrun-service-upr23soxia-uc.a.run.app/api/v1/docs
Generate an access token using the /api/v1/login/access-token
endpoint with credentials specified in your .env
file.
Obtain an access token using the login endpoint:
const token = "your_generated_access_token_here"; // Replace with actual token
Utilize the access token in your Next.js application as follows:
const headers = new Headers({
Authorization: "Bearer " + token,
"Content-Type": "application/json",
});
async function chatAnswer() {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/v1/qa/chat`,
{
method: "POST",
headers: headers,
body: JSON.stringify({ message: "Your query here" }),
}
);
return res.json();
}
Integrate Stripe to manage subscriptions and limit usage based on the chosen plan. Follow Stripe's official documentation to set up the billing and subscription logic.
Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.