File size: 3,257 Bytes
34241fb
 
 
 
 
 
 
 
 
 
14faba3
 
 
 
 
be9c365
14faba3
 
 
 
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
 
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
14faba3
be9c365
ade181c
14faba3
be9c365
14faba3
be9c365
ade181c
14faba3
be9c365
d50d28b
14faba3
 
 
 
 
 
 
 
 
d50d28b
14faba3
be9c365
ef21bff
 
 
14faba3
ef21bff
 
 
 
14faba3
ef21bff
 
14faba3
ef21bff
14faba3
ef21bff
14faba3
ef21bff
 
14faba3
ef21bff
 
14faba3
d50d28b
14faba3
ef21bff
14faba3
ef21bff
14faba3
ef21bff
14faba3
 
ef21bff
 
14faba3
 
55f362f
14faba3
ef21bff
14faba3
ef21bff
14faba3
ef21bff
14faba3
55f362f
14faba3
55f362f
14faba3
55f362f
14faba3
55f362f
14faba3
55f362f
14faba3
55f362f
14faba3
55f362f
14faba3
55f362f
14faba3
55f362f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
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
  - git clone https://github.com/your-username/fastapi-rag-app.git
  - cd fastapi-rag-app

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:
    - πŸ‘‰ http://127.0.0.1:8000


## πŸ”‘ Authentication Flow

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

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

  - Use Authorization: Bearer <access_token> 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 πŸš€