Try the application live at: https://rag.avenueit.be
RAG Parrot (no API key required): https://fakerag.avenueit.be - Mock version
A modern AI-driven customer support assistant that leverages Retrieval-Augmented Generation (RAG) to provide accurate, context-aware responses. Built with FastAPI and following hexagonal architecture principles for maintainability and testability.
- RAG-Powered Responses: Combines document retrieval with AI generation for accurate, context-aware answers
- Hexagonal Architecture: Clean separation of concerns with pluggable interfaces for agents and databases
- Vector Database: ChromaDB integration for efficient similarity search and document retrieval
- AI Integration: Cohere Command-R-Plus model for natural language generation
- Real-time Chat: FastAPI-powered REST API with streaming support
- Document Management: Upload and manage knowledge base documents (max 100KB per file)
- Modern Web UI: NiceGUI-powered reactive interface with real-time updates
- Session Isolation: Per-user document storage with cookie-based sessions (30-day persistence)
- Testing: Comprehensive test suite with mock implementations
- Development Tools: Pre-commit hooks, linting, and type checking
The application follows hexagonal architecture principles with clear separation between core business logic and external adapters:
-
Ports (
app/ports/): Abstract contracts defining behaviorAIAgentInterface: Contract for AI agents (query_with_context, get_stream_response)DatabaseManagerInterface: Contract for vector databases (add_text_to_db, get_context, etc.)
-
Implementations:
- Agents (
app/agents/): AI agent implementationsCohereAgent: Production implementation using Cohere's Command-R-Plus modelFakeAgent: Mock implementation for testing
- Databases (
app/databases/): Vector database implementationsChromaDatabase: Production implementation using ChromaDB with Cohere embeddingsFakeDatabase: Mock implementation for testing
- Agents (
-
Use Casess (
app/usecases): Business logic orchestration -
API Layer (
app/api/): FastAPI routers and HTTP handling -
UI Layer (
app/ui/): NiceGUI pages, components, and servicesservices/: Pure business logic (testable without UI framework)ChatService: Chat history managementActivityService: Activity tracking
components/: UI handlers that delegate to servicespages/: Page implementations (chat, documents)
- Clone the repository:
git clone https://github.com/giunio-prc/rag-powered-chatbot
cd rag-powered-chatbot- Install dependencies:
uv install- Install pre-commit hooks:
uv run pre-commit install- Create environment configuration:
cp .env.example .env- Edit
.envfile with your API keys:
COHERE_API_KEY=your-cohere-api-key-here
# Optional: External Chroma server settings
CHROMA_SERVER_HOST=localhost
CHROMA_SERVER_PORT=8001uv run fastapi devuv run fastapi runThe application will be available at http://localhost:8000
Create a .env file in the project root with the following variables:
| Variable | Required | Description |
|---|---|---|
COHERE_API_KEY |
Yes | Your Cohere API key for embeddings and language models |
CHROMA_SERVER_HOST |
No | Host for external Chroma server (defaults to in-memory) |
CHROMA_SERVER_PORT |
No | Port for external Chroma server |
NICEGUI_STORAGE_SECRET |
No | Secret key for NiceGUI session storage (defaults to built-in key) |
To use an external Chroma server instead of in-memory database:
uv run chroma run --path ./db_chroma --port 8001Update your .env file accordingly:
CHROMA_SERVER_HOST=localhost
CHROMA_SERVER_PORT=8001# Run linting with auto-fix
uv run ruff check --fix
# Run type checking
uv run ty check
# Run pre-commit hooks manually
uv run pre-commit run# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov
# Run specific test file
uv run pytest tests/controller/test_controller.pyapp/
βββ agents/ # AI agent implementations
β βββ cohere_agent.py # Production Cohere implementation
β βββ fake_agent.py # Mock implementation for testing
βββ api/ # FastAPI routers and endpoints
β βββ database.py # Document upload and stats endpoints
β βββ dependencies.py # Dependency injection helpers
β βββ prompting.py # Chat query endpoints
βββ usecases/ # Business logic orchestration
βββ databases/ # Vector database implementations
β βββ chroma_database.py # Production ChromaDB implementation
β βββ fake_database.py # Mock implementation for testing
βββ ports/ # Abstract base classes (contracts)
β βββ agent.py # AIAgentInterface
β βββ database.py # DatabaseManagerInterface
β βββ errors.py # Custom exceptions
βββ ui/ # NiceGUI web interface
β βββ services/ # Pure business logic (testable)
β β βββ chat.py # ChatService - chat history management
β β βββ activity.py # ActivityService - activity tracking
β βββ components/ # UI handlers (thin layer over services)
β β βββ chat.py # ChatHandler - UI for chat
β β βββ documents.py # Document-related UI handlers
β β βββ layout.py # Page layout component
β βββ pages/ # Page implementations
β β βββ chat.py # Real-time chat interface
β β βββ documents.py # Document management page
β βββ http_client.py # HTTP client for API calls
β βββ utils.py # UI utility functions
βββ middleware.py # Session cookie middleware
βββ main.py # FastAPI application entry point
tests/ # Test suite mirroring app structure
βββ api/ # API endpoint tests
βββ usecases/ # Use case tests
βββ databases/ # Database tests
βββ ui/ # UI service tests (pure logic, no mocks)
static/ # Static assets (favicon)
docs/ # Sample documents for testing
stack_logos/ # Technology stack logos
db_chroma/ # ChromaDB storage (persistent mode)
POST /query- Send a query and get responsePOST /query-stream- Send a query and get streaming response (SSE)
POST /add-document- Upload document to knowledge base (max 100KB, .txt only)GET /get-vectors-data- Get database statistics (vector count, longest vector)DELETE /empty-database- Clear all documents for current session
GET /- Chat interface with real-time streaming responsesGET /documents- Document upload, statistics, and database management
- FastAPI: Modern, fast web framework for building APIs
- NiceGUI: Python-based reactive web UI framework
- LangChain: Framework for developing applications with large language models
- ChromaDB: Open-source embedding database for vector similarity search
- Cohere: AI platform providing embeddings and language generation models
- Ruff: Fast Python linter and code formatter
- pytest: Testing framework with asyncio support
- uv: Modern Python package management
- Unit Tests: Located in
tests/directory mirroringapp/structure - Mock Implementations:
FakeAgentandFakeDatabasefor isolated testing - UI Services: Pure business logic in
app/ui/services/tested without mocksChatServiceandActivityServiceuse dependency injection for time providers- Tests use simple dict storage, no UI framework dependencies
- Test Data: Sample documents in
tests/data/ - Async Support: Tests support FastAPI's async operations
- Coverage: 100% coverage enforced (UI components/pages excluded)
- Ensure all tests pass:
uv run pytest - Run code quality checks:
uv run ruff check --fix - Pre-commit hooks will run automatically before commits
- Follow the existing code structure and patterns
- Add tests for new functionality
For questions, issues, or contributions, please contact:
- Author: Giunio De Luca
- GitHub: Open an issue in this repository for bugs or feature requests
- Email: giunio@avenueit.be
For technical support:
- Check existing GitHub issues first
- Create a new issue with detailed information about your problem
- Include relevant logs and environment details when reporting bugs