Advanced AI-powered research service with RAG, document processing, and multi-agent intelligence.
Built with Python, FastAPI, Pydantic AI, PostgreSQL, and OpenRouter - delivering comprehensive research capabilities with cited sources.
π High Performance
- Redis caching for 75% cost reduction ($730/year β $182/year)
- Async processing for 3x faster responses
- HTTP connection pooling for 30-50% faster API calls
- Sub-6 second response times for complex queries
π€ Multi-Agent Intelligence
- Search Agent: Query decomposition and source discovery
- Research Agent: Deep analysis and synthesis
- Synthesis Agent: Citation-rich answer generation
- Temporal Detection: SpaCy-powered time-aware queries
π RAG Pipeline (pgvector)
- Document processing with Dockling
- 384-dimensional embeddings (Sentence Transformers)
- Semantic search with cosine similarity
- Full-text search (FTS) support
π° Cost Optimized
- Free-tier LLM usage (DeepSeek via OpenRouter)
- Intelligent caching with configurable TTLs
- ~$15/month for moderate usage
π Privacy-Focused Search
- SearxNG integration (no tracking)
- Crawl4AI for web crawling
- Document upload support
- Local processing
βββββββββββββββ
β Client β
ββββββββ¬βββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββ
β FastAPI Server (Port 8001) β
β ββββββββββββββββββββββββββββββββ β
β β Research Pipeline β β
β β β’ Multi-agent orchestration β β
β β β’ Query decomposition β β
β β β’ Source validation β β
β β β’ Temporal detection (SpaCy)β β
β ββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββββββββββ β
β β Redis Cache Layer β β
β β β’ Response cache (1d TTL) β β
β β β’ Session storage β β
β β β’ 75% cost reduction β β
β ββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββββββββββ β
β β RAG Pipeline (pgvector) β β
β β β’ Document processing β β
β β β’ Embedding generation β β
β β β’ Semantic search β β
β ββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββ βββββββββββββββ ββββββββββββββββ
β SearxNG β β PostgreSQL β β Redis β
β Port 4000 β β Port 5433 β β Port 6380 β
β β β + pgvector β β β
βββββββββββββββ βββββββββββββββ ββββββββββββββββ
Performance Metrics:
- First query (cold): ~5s
- Cached query: ~1s (5x faster)
- Research mode: <10s with 30+ sources
- Embedding generation: <100ms
π‘ Recommended: Use Docker for the easiest setup experience. All services are pre-configured and will work out of the box.
- Docker and Docker Compose (required)
- OpenRouter API key - Get your free API key at openrouter.ai/keys
- Python 3.11+ (only for local development without Docker)
- Clone the repository
git clone https://github.com/chaitanyame/privachat_agents.git
cd privachat_agents- Set up environment variables
Copy the example environment file and configure it:
cp .env.example .envThen edit .env and configure the following required settings:
# Required: Get your free API key at https://openrouter.ai/keys
OPENROUTER_API_KEY=sk-or-your-actual-key-here
# Required: Set a strong password for PostgreSQL
POSTGRES_PASSWORD=your_secure_password_hereOptional but recommended:
# Enable Langfuse monitoring (optional)
LANGFUSE_ENABLED=true
LANGFUSE_PUBLIC_KEY=pk-lf-...
LANGFUSE_SECRET_KEY=sk-lf-...
# Adjust search behavior (optional)
SEARXNG_PAGES=3
SEARXNG_CATEGORIES=general,news,science,IT.env file with real credentials! The .env file is already in .gitignore.
- Start the services
docker compose up -dThis will start:
- API Server (port 8001) - Research API with FastAPI
- Streamlit UI (port 8503) - Testing interface
- PostgreSQL (port 5433) - Database with pgvector
- Redis (port 6380) - Cache layer
- SearxNG (port 4000) - Privacy-focused search
- Verify it's working
curl http://localhost:8001/healthAccess the Streamlit UI at: http://localhost:8503
curl -X POST http://localhost:8001/api/v1/search \
-H "Content-Type: application/json" \
-d '{
"query": "What are the latest developments in AI?",
"mode": "search",
"max_sources": 20
}'curl -X POST http://localhost:8001/api/v1/research \
-H "Content-Type: application/json" \
-d '{
"query": "Explain quantum computing and its applications",
"mode": "research",
"max_sources": 30
}'{
"session_id": "uuid-here",
"query": "What are the latest developments in AI?",
"answer": "Detailed answer with citations...",
"sources": [
{
"title": "Article Title",
"url": "https://example.com/article",
"relevance_score": 0.95,
"content_snippet": "..."
}
],
"execution_time_seconds": 5.2
}privachat_agents/
βββ privachat_agents/ # Main Python package
β βββ agents/ # AI agents (Pydantic AI)
β β βββ search_agent.py # Search orchestration
β β βββ research_agent.py # Deep research
β β βββ synthesis_agent.py # Answer synthesis
β βββ api/v1/ # FastAPI routes
β β βββ endpoints/
β β βββ search.py # Search endpoints
β β βββ research.py # Research endpoints
β βββ clients/ # External API clients
β β βββ searxng_client.py # SearxNG integration
β β βββ web_crawler.py # Crawl4AI wrapper
β βββ core/ # Core configuration
β β βββ config.py # Settings management
β β βββ pipelines/ # Processing pipelines
β βββ database/ # SQLAlchemy models
β β βββ models.py # Database tables
β β βββ repositories/ # Data access layer
β βββ models/ # Pydantic schemas
β β βββ schemas/ # Request/response models
β βββ rag/ # RAG pipeline
β β βββ vector_store.py # pgvector operations
β β βββ embeddings.py # Sentence transformers
β βββ services/ # Business logic
β β βββ llm/ # LLM integrations
β β βββ cache/ # Redis caching
β βββ main.py # FastAPI application
βββ tests/ # Test suite
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
β βββ e2e/ # End-to-end tests
βββ alembic/ # Database migrations
βββ config/searxng/ # SearxNG settings
βββ docs/ # Documentation
βββ scripts/ # Utility scripts
βββ ui/ # User interfaces
β βββ streamlit_app.py # Testing UI
βββ docker-compose.yml # Service orchestration
βββ Dockerfile # API container
βββ Dockerfile.streamlit # UI container
βββ pyproject.toml # Package configuration
βββ requirements.txt # Dependencies
βββ requirements-dev.txt # Dev dependencies
# Required
OPENROUTER_API_KEY=sk-or-...
OPENROUTER_MODEL=openrouter/auto
POSTGRES_PASSWORD=your_secure_password
# Database
DATABASE_URL=postgresql+asyncpg://research_user:${POSTGRES_PASSWORD}@postgres:5432/research_db
POSTGRES_USER=research_user
POSTGRES_DB=research_db
# Redis Cache
REDIS_URL=redis://redis:6379/0
REDIS_ENABLED=true
# Search
SEARXNG_API_URL=http://searxng:8080
# Server
API_HOST=0.0.0.0
API_PORT=8001
LOG_LEVEL=INFO
# Optional: Langfuse Monitoring
LANGFUSE_ENABLED=false
LANGFUSE_PUBLIC_KEY=pk-...
LANGFUSE_SECRET_KEY=sk-...8001- Research API (FastAPI)8503- Streamlit UI5433- PostgreSQL (with pgvector)6380- Redis cache4000- SearxNG search engine
# Install dependencies
pip install -r requirements.txt
pip install -r requirements-dev.txt
# Download SpaCy model
python -m spacy download en_core_web_sm
# Set environment variables
export OPENROUTER_API_KEY=your_key_here
export DATABASE_URL=postgresql+asyncpg://user:pass@localhost:5433/research
export REDIS_URL=redis://localhost:6380
# Run database migrations
alembic upgrade head
# Run the server
uvicorn privachat_agents.main:app --reload --host 0.0.0.0 --port 8001# All services
docker compose logs -f
# Specific service
docker compose logs -f api
# Search for specific patterns
docker compose logs api | grep -i "search\|error"docker compose build api
docker compose up -d apiRun the comprehensive test suite:
# Run all tests
pytest tests/ -v
# Run with coverage
pytest --cov=privachat_agents --cov-report=html tests/
# Run specific test categories
pytest tests/unit/ -v # Unit tests only
pytest tests/integration/ -v # Integration tests
pytest tests/e2e/ -v # End-to-end tests
# Run marked tests
pytest -m "unit and fast" -v# Windows
run_tests.bat
run_all_tests.bat
# Linux/Mac
./run_tests.sh# Linting
ruff check .
ruff check --fix .
# Type checking
mypy privachat_agents/
# Format code
ruff format .The system includes 4 major performance optimizations:
- Response cache: 1-day TTL
- Session storage: Persistent across restarts
- Result: Identical queries 5x faster (5s β 1s)
- Savings: $730/year β $182/year
- Concurrent source fetching with
asyncio.gather() - Before: 9+ seconds for 3 URLs
- After: ~3 seconds (3x faster)
- Singleton
httpx.AsyncClient - 100 max connections, 20 keep-alive
- Connection reuse across all API calls
- Sentence Transformers (384D)
- Batch processing support
- CPU/GPU adaptive
See docs/PROCESS_FLOWS.md for detailed architecture.
curl http://localhost:8001/health# Interactive API docs (Swagger UI)
open http://localhost:8001/docs
# Alternative docs (ReDoc)
open http://localhost:8001/redocdocker compose psdocker compose exec postgres psql -U research_user -d research_db -c "SELECT count(*) FROM research_sessions;"Access Langfuse dashboard at your configured URL
- Check OpenRouter API key is set correctly:
echo $OPENROUTER_API_KEY - Check logs:
docker compose logs api - Verify health endpoint:
curl http://localhost:8001/health
- Verify Redis is running:
docker compose ps redis - Check Redis connection:
docker compose exec redis redis-cli PING - Review cache logs:
docker compose logs api | grep -i cache
- Check if caching is enabled (
REDIS_ENABLED=true) - Verify async processing is working (logs show "concurrent")
- Monitor Redis:
docker stats
- Check migrations:
alembic current - Run migrations:
alembic upgrade head - Check pgvector extension:
docker compose exec postgres psql -U research_user -d research_db -c "\dx"
- Ensure SearxNG is running:
curl http://localhost:4000 - Check SearxNG logs:
docker compose logs searxng - Verify configuration in
config/searxng/settings.yml
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Follow TDD principles (tests first!)
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Standards:
- β Write tests BEFORE implementation (TDD)
- β Minimum 80% test coverage
- β Type hints on all functions
- β Docstrings on all public APIs
- β Pass ruff and mypy checks
See CONTRIBUTING.md and .github/copilot-instructions.md for detailed guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built upon concepts from Perplexica
- Uses SearxNG for privacy-focused search
- Powered by OpenRouter for affordable LLM access
- Enhanced with Pydantic AI for type-safe AI agents
- Document processing with Dockling
- Web crawling with Crawl4AI
- Issues: GitHub Issues
- Documentation: See the
docs/folder or online docs - Contributing: See CONTRIBUTING.md
- Code of Conduct: See CODE_OF_CONDUCT.md
- Security: See SECURITY.md
Made with β€οΈ for the AI agent community