rag / README.md
Kareman's picture
udate readme.md
34241fb
metadata
title: ContextAI
emoji: πŸ€–
colorFrom: blue
colorTo: green
sdk: docker
sdk_version: '1.0'
app_file: app.main
pinned: false

ContextAI

A FastAPI-based RAG application that lets users upload documents (PDF/TXT) and ask questions.
Powered by LangChain, ChromaDB, and LLMs for context-aware answers.

πŸ“š FastAPI RAG App with LangChain, ChromaDB & Authentication

This project is a Retrieval-Augmented Generation (RAG) web application built with FastAPI. It allows users to:

  • πŸ”‘ Sign up / Sign in (JWT-based authentication)

  • πŸ“‚ Upload PDF or text documents

  • 🧠 Store document embeddings in ChromaDB (vector database)

  • πŸ’¬ Ask questions about uploaded documents

  • ⚑ Get context-aware answers powered by LangChain + LLMs (via OpenRouter )

πŸš€ Features

  • User authentication with access & refresh tokens

  • Secure file uploads (.pdf, .txt)

  • Automatic text chunking & embedding with HuggingFace models

  • Persistent vector store using ChromaDB

  • RAG pipeline with LangChain’s RetrievalQA

  • OpenRouter integration for running LLM queries

  • CORS configured for frontend integration

πŸ› οΈ Tech Stack

  • FastAPI

  • LangChain

  • ChromaDB

  • SQLModel

    • for user database
  • HuggingFace Embeddings

  • OpenRouter

    • (for LLM access)

πŸ“‚ Project Structure

app/
 β”œβ”€β”€ main.py          # FastAPI routes & entrypoint
 β”œβ”€β”€ rag.py           # RAG pipeline (embeddings, vector store, QA chain)
 β”œβ”€β”€ models.py        # User models & schemas
 β”œβ”€β”€ auth.py          # Auth logic (hashing, tokens, verification)
 β”œβ”€β”€ database.py      # SQLModel setup
 └── config.py        # Settings & constants
uploads/              # User uploaded files (ignored in Git)
chroma_db/            # Vector DB storage (ignored in Git)

βš™οΈ Setup & Installation

1️⃣ Clone the repo

2️⃣ Create & activate virtual environment

  • python -m venv venv
  • source venv/bin/activate # Linux/Mac
  • venv\Scripts\activate # Windows

3️⃣ Install dependencies

  • pip install -r requirements.txt

4️⃣ Configure environment variables

  • Create a .env file in the project root (or copy from .env.example):

  • OpenRouter

  • OPENROUTER=your_openrouter_api_key_here

  • JWT secret

  • SECRET_KEY=your_super_secret_key

⚠️ Never commit your real .env file.

▢️ Run the App

  • Start the FastAPI server:

  • uvicorn app.main:app --reload

  • The API will be available at:

πŸ”‘ Authentication Flow

  • Signup β†’ POST /signup with username & password

  • Signin β†’ POST /signin to receive access_token & refresh_token

  • Use Authorization: Bearer for protected endpoints

πŸ“‚ Document Workflow

  • User logs in

  • Upload document β†’ POST /upload (PDF or TXT)

  • Ask a question β†’ GET /ask?q=your+question

  • The system searches your embeddings in ChromaDB and queries the LLM with context

πŸ“ Notes

  • uploads/ and chroma_db/ are auto-created at runtime if they don’t exist.

  • Both folders are ignored by Git (runtime data only).

  • Contributions & pull requests are welcome πŸš€