/aws-cdk-rag-fargate

Serverless Application with AWS CDK, LlamaIndex and Qdrant

Primary LanguagePython

Creating a RAG application with AWS CDK as IaC, Qdrant and LlamaIndex ☁️

cover_gke_medium

This repository contains a full RAG application using AWS CDK as IaC, LlamaIndex framework, Qdrant as a vector database, and deployment on AWS using a FastAPI app and Dockerfile.

For detailed project descriptions, refer to this Medium article.

Main Steps

  • Data Ingestion: Load data to an s3 bucket
  • Indexing: Use SentenceSplitter for indexing in nodes
  • Embedding and Model: OpenAI
  • Vector Store: Use Qdrant for inserting metadata
  • FastAPI and AWS: Handle requests via the FastAPI app deployed on AWS
  • IaC: AWS CDK

Feel free to ⭐ and clone this repo 😉

Tech Stack

Visual Studio Code Python OpenAI Anaconda Linux Ubuntu FastAPI Git Docker AWS

Project Structure

The project has been structured with the following files:

  • app: app logic with Dockerfile
  • tests: unittest
  • app.py: AWS CDK synthesizer
  • aws_cdk_rag_fargate: script to create the constructs and stack
  • scripts: scripts to puch the documents to s3 and create the Qdrant collection
  • .env: environmental variables
  • requirements.txt: project requirements

Project Set Up

The Python version used for this project is Python 3.11. You can follow along the medium article.

  1. Create an empty respository locally. This is necessary to initialize CDK. Afterwards you can copy the files from this repository.

  2. Initialize AWS CDK to create the project structure:

    cdk init app --language python
  3. Create the virtual environment named main-env using Conda with Python version 3.11:

    conda create -n main-env python=3.11
    conda activate main-env
  4. Install the requirements.txt:

    pip install -r requirements.txt
  5. Make sure the .env file is complete:

     QDRANT_URL=
     QDRANT_API_KEY=
     DOCUMENT_BUCKET=
     OPENAI_API_KEY=
  6. Synthesize the app:

    cdk synth
  7. Bootstrap the app to provision s3, ECR and IAM roles:

    cdk bootstrap aws://{Account ID:}/{region}
  8. Create Qdrant Collection by running the script under scripts:

    python s3_uploader.py
    python qdrant_setup.py
  9. Deploy the app on AWS CDK

    cdk deploy
  10. Get the DNS Name

    aws cloudformation describe-stacks --stack-name AwsCdkRagFargateStack --query "Stacks[0].Outputs[?        OutputKey=='LoadBalancerDNS'].OutputValue" --output text
    
    # Output
    AwsCdk-RagFa-a1TpSwcX5iaS-653394042.eu-central-1.elb.amazonaws.com 
  11. Send a POST request to test the app

    curl -X POST -H "Content-Type: application/json" -d '{"question":"What is a transformer?"}' http://AwsCdk-RagFa-a1TpSwcX5iaS-653394042.eu-central-1.elb.amazonaws.com/query
  12. Clean up

    cdk destroy