A production-ready FastAPI server with a RAG (Retrieval-Augmented Generation) pipeline for scientific document search and question answering.
โ
Document Ingestion: Upload PDFs, DOCX, TXT, MD,
CSV, JSON
โ
Semantic Search: Vector-based similarity search with
metadata filtering
โ
Question Answering: Context-aware responses with
source citations
โ
Knowledge Management: Statistics, exports, document
deletion
โ
RESTful API: Clean, well-documented endpoints
โ
Production Ready: Docker support, logging, testing,
monitoring
scientific-chatbot/
โ
โโโ ๐ง Core Application Files
โ โโโ api.py [23KB] FastAPI server with 12+ endpoints
โ โโโ rag_pipeline.py [22KB] RAG implementation with ChromaDB
โ โโโ requirements.txt [1.5KB] Python dependencies
โ
โโโ ๐ Documentation
โ โโโ README.md [18KB] Architecture & implementation guide
โ โโโ API_DOCUMENTATION.md [16KB] Complete API reference
โ โโโ QUICKSTART.md [8.5KB] Step-by-step setup guide
โ โโโ PROJECT_OVERVIEW.md [THIS] System summary
โ
โโโ ๐ Deployment & Setup
โ โโโ setup.sh [6KB] Automated setup script
โ โโโ Dockerfile [1KB] Container configuration
โ โโโ docker-compose.yml [1KB] Docker orchestration
โ โโโ .env [AUTO] Environment configuration
โ
โโโ ๐ ๏ธ Tools & Testing
โ โโโ api_client.py [15KB] CLI client for API interaction
โ โโโ tests/
โ โโโ test_api.py [16KB] Comprehensive test suite
โ
โโโ ๐ Data Directories (auto-created)
โโโ data/
โ โโโ chroma_db/ Vector database storage
โ โโโ uploads/ Uploaded documents
โ โโโ temp/ Temporary processing
โ โโโ exports/ Knowledge base exports
โโโ logs/
โโโ api.log Application logs
Total Size: ~135KB code + dependencies
Lines of Code: ~2,800+ (excluding dependencies)
Test Coverage: 25+ test cases
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Layer โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ cURL โ โ Python โ โ R Shiny โ โ React โ โ
โ โ Client โ โ Client โ โ UI โ โ Frontendโ โ
โ โโโโโโฌโโโโโโ โโโโโโฌโโโโโโ โโโโโโฌโโโโโโ โโโโโโฌโโโโโโ โ
โโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ โ โ โ
โโโโโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FastAPI Server (api.py) โ
โ โข Document Ingestion Endpoints โ
โ โข Search & Retrieval Endpoints โ
โ โข Question Answering Endpoints โ
โ โข Knowledge Base Management โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ RAG Pipeline (rag_pipeline.py) โ
โ โ
โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ
โ โ Document โ โ Embedding โ โ
โ โ Loaders โ โ Model โ โ
โ โ PDF,DOCX,TXT โ โ SentenceT. โ โ
โ โโโโโโโโโฌโโโโโโโโโ โโโโโโโโโฌโโโโโโโโโ โ
โ โ โ โ
โ โโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโ โ
โ โ Text Splitter โ โ
โ โ Chunk Size: 1000 / Overlap: 200 โ โ
โ โโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Vector Store (ChromaDB) โ โ
โ โ โข Semantic Search โ โ
โ โ โข Metadata Filtering โ โ
โ โ โข Similarity Scoring โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Storage Layer โ
โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ ChromaDB โ โ File System โ โ
โ โ (Vectors) โ โ (Documents) โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Document Ingestion Flow:
Upload โ Hash Check โ Load โ Split โ Embed โ Store โ Log
โ โ โ โ โ โ โ
File Duplicate Parse Chunk Vector ChromaDB JSON
PDF Detection Text 1000 384dim Storage Log
Search/Retrieval Flow:
Query โ Embed โ Similarity Search โ Filter โ Rank โ Return
โ โ โ โ โ โ
User Vector ChromaDB Lookup Metadata Score Results
Input 384dim Cosine Distance Filters Sort +Cites
GET / - API informationGET /health - Health checkPOST /ingest/document - Upload single documentPOST /ingest/directory - Batch ingest from
directoryPOST /batch/ingest - Upload multiple filesPOST /search - Semantic searchPOST /search/advanced - Search with multiple
filtersPOST /qa - Question answering with citationsGET /stats - Knowledge base statisticsGET /documents - List all documentsGET /document-types - List document typesDELETE /documents/{hash} - Delete documentGET /export - Export knowledge baseTotal: 13 REST endpoints
# Automated setup
./setup.sh
# Manual setup
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Development
python api.py
# Production (Uvicorn)
uvicorn api:app --host 0.0.0.0 --port 8000 --workers 4
# Docker
docker-compose up -d
# Health check
python api_client.py health
# Ingest document
python api_client.py ingest paper.pdf
# Search
python api_client.py search "chronic pain treatment"
# Ask question
python api_client.py qa "What are effective treatments?"
# Statistics
python api_client.py stats
# List documents
python api_client.py list
# Search
curl -X POST http://localhost:8000/search \
-H "Content-Type: application/json" \
-d '{"query":"pain management","k":5}'
# Upload
curl -X POST http://localhost:8000/ingest/document \
-F "file=@paper.pdf"
# Stats
curl http://localhost:8000/stats
# Install in development mode
pip install -e .
# Run with auto-reload
API_RELOAD=True python api.py
# Run tests
pytest tests/ -v
# All tests
pytest tests/
# With coverage
pytest tests/ --cov=api --cov=rag_pipeline
# Specific test
pytest tests/test_api.py::test_search_basic -v
# Docker build
docker build -t scientific-chatbot-api .
# Docker Compose
docker-compose up -d
# Check logs
docker-compose logs -f api
library(httr)
library(jsonlite)
search <- function(query, k = 5) {
POST(
"http://localhost:8000/search",
body = list(query = query, k = k),
encode = "json"
) %>% content()
}
async function searchDocuments(query, k = 5) {
const response = await fetch('http://localhost:8000/search', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, k })
});
return await response.json();
}
import requests
response = requests.post(
"http://localhost:8000/search",
json={"query": "chronic pain", "k": 5}
)
results = response.json()
api.py - API Endpointsrag_pipeline.py - RAG Logic.env - Configurationdocker-compose.yml -
DeploymentTo extend this system:
git checkout -b feature/new-featureMIT License - See LICENSE file for details
You now have a complete, production-ready scientific chatbot API with:
โ
Full document ingestion pipeline
โ
Semantic search with metadata filtering
โ
Question answering with citations
โ
RESTful API with 13 endpoints
โ
Comprehensive documentation
โ
Docker deployment
โ
Test suite with 25+ tests
โ
CLI client for easy interaction
โ
Ready for R Shiny integration
Total Development Time: Equivalent to several weeks
of manual coding
Code Quality: Production-grade with error handling and
logging
Extensibility: Easy to customize and scale
Youโre ready to build powerful AI-driven research tools! ๐
Version: 1.0.0
Last Updated: November 2024
Maintainer: Scientific Chatbot Development Team