fix(readme and config)
Browse files- README.md +59 -61
- app/config.py +5 -0
- app/rag.py +3 -2
README.md
CHANGED
|
@@ -3,55 +3,55 @@
|
|
| 3 |
A **FastAPI-based RAG application** that lets users upload documents (PDF/TXT) and ask questions.
|
| 4 |
Powered by **LangChain**, **ChromaDB**, and **LLMs** for context-aware answers.
|
| 5 |
|
| 6 |
-
π FastAPI RAG App with LangChain, ChromaDB & Authentication
|
| 7 |
|
| 8 |
This project is a Retrieval-Augmented Generation (RAG) web application built with FastAPI.
|
| 9 |
It allows users to:
|
| 10 |
|
| 11 |
-
π Sign up / Sign in (JWT-based authentication)
|
| 12 |
|
| 13 |
-
π Upload PDF or text documents
|
| 14 |
|
| 15 |
-
π§ Store document embeddings in ChromaDB (vector database)
|
| 16 |
|
| 17 |
-
π¬ Ask questions about uploaded documents
|
| 18 |
|
| 19 |
-
β‘ Get context-aware answers powered by LangChain + LLMs (via OpenRouter
|
| 20 |
)
|
| 21 |
|
| 22 |
-
π Features
|
| 23 |
|
| 24 |
-
User authentication with access & refresh tokens
|
| 25 |
|
| 26 |
-
Secure file uploads (.pdf, .txt)
|
| 27 |
|
| 28 |
-
Automatic text chunking & embedding with HuggingFace models
|
| 29 |
|
| 30 |
-
Persistent vector store using ChromaDB
|
| 31 |
|
| 32 |
-
RAG pipeline with LangChainβs RetrievalQA
|
| 33 |
|
| 34 |
-
OpenRouter integration for running LLM queries
|
| 35 |
|
| 36 |
-
CORS configured for frontend integration
|
| 37 |
|
| 38 |
-
π οΈ Tech Stack
|
| 39 |
|
| 40 |
-
FastAPI
|
| 41 |
|
| 42 |
-
LangChain
|
| 43 |
|
| 44 |
-
ChromaDB
|
| 45 |
|
| 46 |
-
SQLModel
|
| 47 |
-
for user database
|
| 48 |
|
| 49 |
-
HuggingFace Embeddings
|
| 50 |
|
| 51 |
-
OpenRouter
|
| 52 |
-
(for LLM access)
|
| 53 |
|
| 54 |
-
π Project Structure
|
| 55 |
app/
|
| 56 |
βββ main.py # FastAPI routes & entrypoint
|
| 57 |
βββ rag.py # RAG pipeline (embeddings, vector store, QA chain)
|
|
@@ -62,66 +62,64 @@ app/
|
|
| 62 |
uploads/ # User uploaded files (ignored in Git)
|
| 63 |
chroma_db/ # Vector DB storage (ignored in Git)
|
| 64 |
|
| 65 |
-
βοΈ Setup & Installation
|
| 66 |
-
1οΈβ£ Clone the repo
|
| 67 |
-
git clone https://github.com/your-username/fastapi-rag-app.git
|
| 68 |
-
cd fastapi-rag-app
|
| 69 |
|
| 70 |
-
2οΈβ£ Create & activate virtual environment
|
| 71 |
-
python -m venv venv
|
| 72 |
-
source venv/bin/activate # Linux/Mac
|
| 73 |
-
venv\Scripts\activate # Windows
|
| 74 |
|
| 75 |
-
3οΈβ£ Install dependencies
|
| 76 |
-
pip install -r requirements.txt
|
| 77 |
|
| 78 |
-
4οΈβ£ Configure environment variables
|
| 79 |
|
| 80 |
-
Create a .env file in the project root (or copy from .env.example):
|
| 81 |
|
| 82 |
-
|
| 83 |
-
OPENROUTER=your_openrouter_api_key_here
|
| 84 |
|
| 85 |
-
|
| 86 |
-
SECRET_KEY=your_super_secret_key
|
| 87 |
|
| 88 |
-
β οΈ Never commit your real .env file.
|
| 89 |
|
| 90 |
-
βΆοΈ Run the App
|
| 91 |
|
| 92 |
-
Start the FastAPI server:
|
| 93 |
|
| 94 |
-
uvicorn app.main:app --reload
|
| 95 |
|
| 96 |
|
| 97 |
-
The API will be available at:
|
| 98 |
-
π http://127.0.0.1:8000
|
| 99 |
|
| 100 |
-
Interactive API docs:
|
| 101 |
-
π http://127.0.0.1:8000/docs
|
| 102 |
|
| 103 |
-
π Authentication Flow
|
| 104 |
|
| 105 |
-
Signup β POST /signup with username & password
|
| 106 |
|
| 107 |
-
Signin β POST /signin to receive access_token & refresh_token
|
| 108 |
|
| 109 |
-
Use Authorization: Bearer <access_token> for protected endpoints
|
| 110 |
|
| 111 |
-
π Document Workflow
|
| 112 |
|
| 113 |
-
User logs in
|
| 114 |
|
| 115 |
-
Upload document β POST /upload (PDF or TXT)
|
| 116 |
|
| 117 |
-
Ask a question β GET /ask?q=your+question
|
| 118 |
|
| 119 |
-
The system searches your embeddings in ChromaDB and queries the LLM with context
|
| 120 |
|
| 121 |
-
π Notes
|
| 122 |
|
| 123 |
-
uploads/ and chroma_db/ are auto-created at runtime if they donβt exist.
|
| 124 |
|
| 125 |
-
Both folders are ignored by Git (runtime data only).
|
| 126 |
|
| 127 |
-
Contributions & pull requests are welcome π
|
|
|
|
| 3 |
A **FastAPI-based RAG application** that lets users upload documents (PDF/TXT) and ask questions.
|
| 4 |
Powered by **LangChain**, **ChromaDB**, and **LLMs** for context-aware answers.
|
| 5 |
|
| 6 |
+
## π FastAPI RAG App with LangChain, ChromaDB & Authentication
|
| 7 |
|
| 8 |
This project is a Retrieval-Augmented Generation (RAG) web application built with FastAPI.
|
| 9 |
It allows users to:
|
| 10 |
|
| 11 |
+
- π Sign up / Sign in (JWT-based authentication)
|
| 12 |
|
| 13 |
+
- π Upload PDF or text documents
|
| 14 |
|
| 15 |
+
- π§ Store document embeddings in ChromaDB (vector database)
|
| 16 |
|
| 17 |
+
- π¬ Ask questions about uploaded documents
|
| 18 |
|
| 19 |
+
- β‘ Get context-aware answers powered by LangChain + LLMs (via OpenRouter
|
| 20 |
)
|
| 21 |
|
| 22 |
+
## π Features
|
| 23 |
|
| 24 |
+
- User authentication with access & refresh tokens
|
| 25 |
|
| 26 |
+
- Secure file uploads (.pdf, .txt)
|
| 27 |
|
| 28 |
+
- Automatic text chunking & embedding with HuggingFace models
|
| 29 |
|
| 30 |
+
- Persistent vector store using ChromaDB
|
| 31 |
|
| 32 |
+
- RAG pipeline with LangChainβs RetrievalQA
|
| 33 |
|
| 34 |
+
- OpenRouter integration for running LLM queries
|
| 35 |
|
| 36 |
+
- CORS configured for frontend integration
|
| 37 |
|
| 38 |
+
## π οΈ Tech Stack
|
| 39 |
|
| 40 |
+
- FastAPI
|
| 41 |
|
| 42 |
+
- LangChain
|
| 43 |
|
| 44 |
+
- ChromaDB
|
| 45 |
|
| 46 |
+
- SQLModel
|
| 47 |
+
- - for user database
|
| 48 |
|
| 49 |
+
- HuggingFace Embeddings
|
| 50 |
|
| 51 |
+
- OpenRouter
|
| 52 |
+
- - (for LLM access)
|
| 53 |
|
| 54 |
+
## π Project Structure
|
| 55 |
app/
|
| 56 |
βββ main.py # FastAPI routes & entrypoint
|
| 57 |
βββ rag.py # RAG pipeline (embeddings, vector store, QA chain)
|
|
|
|
| 62 |
uploads/ # User uploaded files (ignored in Git)
|
| 63 |
chroma_db/ # Vector DB storage (ignored in Git)
|
| 64 |
|
| 65 |
+
## βοΈ Setup & Installation
|
| 66 |
+
- 1οΈβ£ Clone the repo
|
| 67 |
+
- - git clone https://github.com/your-username/fastapi-rag-app.git
|
| 68 |
+
- - cd fastapi-rag-app
|
| 69 |
|
| 70 |
+
- 2οΈβ£ Create & activate virtual environment
|
| 71 |
+
- - python -m venv venv
|
| 72 |
+
- - source venv/bin/activate # Linux/Mac
|
| 73 |
+
- - venv\Scripts\activate # Windows
|
| 74 |
|
| 75 |
+
- 3οΈβ£ Install dependencies
|
| 76 |
+
- - pip install -r requirements.txt
|
| 77 |
|
| 78 |
+
- 4οΈβ£ Configure environment variables
|
| 79 |
|
| 80 |
+
- - Create a .env file in the project root (or copy from .env.example):
|
| 81 |
|
| 82 |
+
- - ### OpenRouter
|
| 83 |
+
- - OPENROUTER=your_openrouter_api_key_here
|
| 84 |
|
| 85 |
+
- - ### JWT secret
|
| 86 |
+
- - SECRET_KEY=your_super_secret_key
|
| 87 |
|
| 88 |
+
- - β οΈ Never commit your real .env file.
|
| 89 |
|
| 90 |
+
- βΆοΈ Run the App
|
| 91 |
|
| 92 |
+
- - Start the FastAPI server:
|
| 93 |
|
| 94 |
+
- - uvicorn app.main:app --reload
|
| 95 |
|
| 96 |
|
| 97 |
+
- - The API will be available at:
|
| 98 |
+
- - - π http://127.0.0.1:8000
|
| 99 |
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
- ## π Authentication Flow
|
| 102 |
|
| 103 |
+
- - Signup β POST /signup with username & password
|
| 104 |
|
| 105 |
+
- - Signin β POST /signin to receive access_token & refresh_token
|
| 106 |
|
| 107 |
+
- - Use Authorization: Bearer <access_token> for protected endpoints
|
| 108 |
|
| 109 |
+
- ## π Document Workflow
|
| 110 |
|
| 111 |
+
- - User logs in
|
| 112 |
|
| 113 |
+
- - Upload document β POST /upload (PDF or TXT)
|
| 114 |
|
| 115 |
+
- - Ask a question β GET /ask?q=your+question
|
| 116 |
|
| 117 |
+
- - The system searches your embeddings in ChromaDB and queries the LLM with context
|
| 118 |
|
| 119 |
+
- ## π Notes
|
| 120 |
|
| 121 |
+
- - uploads/ and chroma_db/ are auto-created at runtime if they donβt exist.
|
| 122 |
|
| 123 |
+
- - Both folders are ignored by Git (runtime data only).
|
| 124 |
|
| 125 |
+
- - Contributions & pull requests are welcome π
|
app/config.py
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Vector DB storage
|
| 2 |
CHROMA_DB_DIR = "./chroma_db"
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# Embedding model
|
| 4 |
+
EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 5 |
+
|
| 6 |
# Vector DB storage
|
| 7 |
CHROMA_DB_DIR = "./chroma_db"
|
app/rag.py
CHANGED
|
@@ -8,9 +8,11 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
| 8 |
from langchain_community.document_loaders import TextLoader
|
| 9 |
from langchain.document_loaders import PyPDFLoader
|
| 10 |
from langchain.text_splitter import CharacterTextSplitter
|
| 11 |
-
from app.config import CHROMA_DB_DIR
|
| 12 |
from langchain.chat_models import ChatOpenAI
|
| 13 |
from langchain.chains import RetrievalQA
|
|
|
|
|
|
|
| 14 |
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
load_dotenv()
|
|
@@ -23,7 +25,6 @@ embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
|
|
| 23 |
# Chroma DB
|
| 24 |
db = Chroma(persist_directory=CHROMA_DB_DIR, embedding_function=embeddings)
|
| 25 |
|
| 26 |
-
from langchain.docstore.document import Document
|
| 27 |
|
| 28 |
def add_document(file_path: str, user_id: str):
|
| 29 |
# Load file
|
|
|
|
| 8 |
from langchain_community.document_loaders import TextLoader
|
| 9 |
from langchain.document_loaders import PyPDFLoader
|
| 10 |
from langchain.text_splitter import CharacterTextSplitter
|
| 11 |
+
from app.config import CHROMA_DB_DIR, EMBEDDING_MODEL
|
| 12 |
from langchain.chat_models import ChatOpenAI
|
| 13 |
from langchain.chains import RetrievalQA
|
| 14 |
+
from langchain.docstore.document import Document
|
| 15 |
+
|
| 16 |
|
| 17 |
from dotenv import load_dotenv
|
| 18 |
load_dotenv()
|
|
|
|
| 25 |
# Chroma DB
|
| 26 |
db = Chroma(persist_directory=CHROMA_DB_DIR, embedding_function=embeddings)
|
| 27 |
|
|
|
|
| 28 |
|
| 29 |
def add_document(file_path: str, user_id: str):
|
| 30 |
# Load file
|