Production-grade RAG (Retrieval-Augmented Generation) SaaS application.
π DocumentaΓ§Γ£o Completa: Veja project_docs/ para guias detalhados de Docker, testes, arquitetura e mais.
Contexta follows a clean architecture with strict separation of concerns:
- Django (web/): Handles authentication, multi-tenancy, document metadata, file uploads, and admin interface
- FastAPI (api/ & ingest/): Handles RAG pipeline, retrieval, re-ranking, and LLM calls
- Core (core/): Framework-agnostic domain logic, interfaces, and abstractions
contexta/
βββ pyproject.toml
βββ README.md
βββ .env.example
βββ core/ # Framework-agnostic core logic
β βββ llm/ # LLM provider abstractions
β βββ prompts/ # Prompt builders
β βββ reranker/ # Re-ranking strategies
β βββ ...
βββ api/ # FastAPI - Query/Retrieval service
βββ ingest/ # FastAPI - Document ingestion service
β βββ loaders/ # Document loaders (PDF, TXT, DOCX)
β βββ chunking/ # Text chunking strategies
β βββ embeddings/ # Embedding generators
β βββ vectorstore/ # Vector store implementations
βββ web/ # Django - Product backend
βββ documents/ # Document management
- Python 3.12+
- Poetry (for dependency management)
- Qdrant (vector database) - running on localhost:6333 by default
- OpenAI API key
- Docker & Docker Compose (optional, para rodar com containers)
- Clone the repository:
git clone <repository-url>
cd contexta- Install dependencies:
poetry install- Set up environment variables:
cp .env.example .env
# Edit .env and add your configurationRequired environment variables:
OPENAI_API_KEY: Your OpenAI API keyQDRANT_URL: Qdrant server URL (default: http://localhost:6333)INGEST_SERVICE_URL: Ingest service URL (default: http://localhost:8001)DJANGO_SECRET_KEY: Django secret key (for production)
Using Docker:
docker run -p 6333:6333 qdrant/qdrantOr install Qdrant locally following Qdrant documentation.
cd web
python manage.py migrate
python manage.py createsuperuser # Create admin user
python manage.py runserverDjango will run on http://localhost:8000
uvicorn ingest.main:app --reload --port 8001Ingest service will run on http://localhost:8001
uvicorn api.main:app --reload --port 8000Query API will run on http://localhost:8000 (or different port if Django is running)
-
Via Django Admin:
- Access
http://localhost:8000/admin - Login with superuser credentials
- Upload documents through the admin interface
- Access
-
Via Django REST API:
# Authenticate first
curl -X POST http://localhost:8000/api/auth/login/ \
-d "username=your_username&password=your_password"
# Upload document
curl -X POST http://localhost:8000/api/documents/ \
-H "Authorization: Bearer YOUR_TOKEN" \
-F "title=My Document" \
-F "file=@/path/to/document.pdf"The document will be automatically processed:
- Status:
pendingβprocessingβcompleted(orfailedon error)
Query the RAG system:
curl -X POST http://localhost:8000/query \
-H "Content-Type: application/json" \
-d '{
"query": "What is the main topic of the documents?",
"tenant_id": 1,
"top_k": 10,
"rerank_top_k": 5
}'Response:
{
"answer": "The main topic is...",
"sources": [
{
"document_id": 1,
"chunk_index": 0,
"score": 0.95,
"text_preview": "..."
}
],
"query": "What is the main topic?",
"tenant_id": 1
}POST /query: Query documents using RAGGET /health: Health check
POST /ingest: Trigger document ingestionGET /health: Health check
GET /api/documents/: List documentsPOST /api/documents/: Upload documentGET /api/documents/{id}/: Get document detailsPUT /api/documents/{id}/: Update documentDELETE /api/documents/{id}/: Delete document
# Run Django tests
cd web
python manage.py test
# Run API tests (when implemented)
pytest api/tests/This project follows:
- Type hints everywhere
- Clean architecture principles
- Separation of concerns
- Framework-agnostic core logic
- New Document Loader: Add to
ingest/loaders/ - New LLM Provider: Implement
core/llm/base.pyinterface - New Re-ranker: Implement
core/reranker/base.pyinterface - New Vector Store: Implement
ingest/vectorstore/base.pyinterface
- Multi-tenancy: All queries filter by
tenant_id(user.id) - LLM Abstraction: Never call OpenAI directly - use
core/llm/interfaces - Prompt Builder: Use
core/prompts/for all prompt construction - Error Handling: Comprehensive logging and error handling throughout
- Type Safety: Type hints required for all functions
Contexta has a complete suite of unit and integration tests.
# All tests
./run_tests.sh
# Tests with coverage
./run_tests.sh cov
# Only unit tests
./run_tests.sh unitπ Visual Testing Guide - Complete guide with structure, best practices, CI/CD and troubleshooting
π Detailed Documentation - Fixtures, examples, configuration and integration
- Verify Qdrant is running:
curl http://localhost:6333/health - Check
QDRANT_URLin.env - If using Docker:
docker-compose psto see container status
- Verify
OPENAI_API_KEYis set in.env - Check API key validity and quota
- Test API key:
curl https://api.openai.com/v1/models -H "Authorization: Bearer $OPENAI_API_KEY"
- Check ingest service logs:
docker-compose logs ingest - Verify file path is accessible
- Check document format is supported (PDF, TXT)
- Verify tenant_id is correct
# Add to PYTHONPATH
export PYTHONPATH="${PYTHONPATH}:$(pwd)"
poetry run pytest# Update poetry.lock
poetry lock
# Reinstall dependencies
poetry install --no-root# Start all services
make up
# View logs
make logs
# Run tests
make docker-test
# Stop services
make downπ Complete Docker Documentation - Detailed guide on setup, troubleshooting and Docker commands
MIT License
