Multi-Agent Chatbot System for Insomnia
An advanced multi-agent chatbot system designed to assist users with insomnia-related queries
- π Quick Start
- π― Introduction
- β¨ Key Features
- ποΈ Architecture
- π¦ Installation
- π§ Configuration
- π Usage
- π Documentation
Screen.Recording.2025-07-24.at.21.57.44.1.1.mov
-
Clone the repository
git clone https://github.com/TaiQuach123/SomniFlow.git cd SomniFlow -
Start required services with Docker
# Start Redis docker run -p 6379:6379 -d redis # Start Qdrant vector database docker run -p 6333:6333 -p 6334:6334 -v "$(pwd)/qdrant_storage:/qdrant/storage:z" qdrant/qdrant # Start SearXNG search engine docker run --rm -d -p 8080:8080 \ -v ./config/searxng-settings.yml:/etc/searxng/settings.yml \ -e "BASE_URL=http://localhost:8080/" \ -e "INSTANCE_NAME=somniflow" \ searxng/searxng
-
Install and start the backend
uv sync source .venv/bin/activate uvicorn backend.main:app --reload -
Install and start the frontend
cd my-frontend npm install npm run dev -
Open your browser Navigate to http://localhost:3000 to start using SomniFlow!
SomniFlow is an advanced multi-agent chatbot system specifically designed to assist users with insomnia-related queries. Built with cutting-edge AI technologies, it provides comprehensive, evidence-based, and context-aware responses by combining local knowledge, real-time web search, and collaborative agent reasoning.
- π€ Multi-Agent Intelligence: Five specialized AI agents work together to provide comprehensive insomnia assistance
- π Real-Time Research: Combines local knowledge with live web search for up-to-date information
- π¬ Natural Conversations: Perplexity-inspired interface with streaming responses and transparent workflow
- π― Specialized Focus: Expert-level knowledge specifically for sleep disorders and insomnia management
- Perplexity-inspired design with clean, intuitive chat experience
- Real-time streaming of both intermediate steps and final answers
- Visual timeline showing agent actions and retrieval steps
- Tabbed interface for answers, sources, and workflow tasks
- Mobile-responsive design for use on any device
- Supervisor Agent: Orchestrates and delegates tasks to specialized agents
- Suggestion Agent: Provides actionable recommendations for insomnia management
- Harm Assessment Agent: Evaluates potential risks and side effects
- Factor Analysis Agent: Identifies contributing factors to sleep issues
- Response Agent: Synthesizes information into comprehensive answers
- RAG Integration: Local vector database (Qdrant) for curated knowledge retrieval
- Web Search: Real-time information via SearXNG with semantic ranking
- Context Awareness: Agents share context for nuanced, well-rounded responses
- Source Attribution: Transparent citation of information sources
- FastAPI Backend: High-performance, async API with streaming support
- Next.js Frontend: Modern React framework with TypeScript
- LangGraph Workflows: Sophisticated agent orchestration
- PydanticAI: Type-safe LLM interactions
- Docker Infrastructure: Easy deployment and scaling
Figure 1: High-level system architecture
Figure 2: Detailed flow of information within retrieval agents
SomniFlow uses a sophisticated multi-agent architecture where specialized agents collaborate to process user queries, retrieve information, and generate insightful, context-aware responses.
- Frontend Layer: Next.js application with streaming UI
- API Gateway: FastAPI backend handling requests and streaming responses
- Agent Orchestration: LangGraph managing multi-agent workflows
- Knowledge Base: Qdrant vector database for local knowledge retrieval
- Web Search: SearXNG for real-time information gathering
- Caching: Redis for session management and response caching
- Docker (for Qdrant, SearXNG, and Redis)
- Python 3.12+ (for backend)
- Node.js 18+ (for frontend)
- Git (for cloning the repository)
git clone https://github.com/yourusername/somniflow.git
cd somniflow# Install Python dependencies using uv
uv sync
# Activate virtual environment
source .venv/bin/activatecd my-frontend
npm install# Start Redis for caching
docker run -p 6379:6379 -d redis
# Start Qdrant vector database
docker run -p 6333:6333 -p 6334:6334 \
-v "$(pwd)/qdrant_storage:/qdrant/storage:z" \
qdrant/qdrant
# Start SearXNG search engine
docker run --rm -d -p 8080:8080 \
-v ./config/searxng-settings.yml:/etc/searxng/settings.yml \
-e "BASE_URL=http://localhost:8080/" \
-e "INSTANCE_NAME=somniflow" \
searxng/searxng# Terminal 1: Start backend
source .venv/bin/activate
uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000
# Terminal 2: Start frontend
cd my-frontend
npm run devOpen your browser and navigate to http://localhost:3000
Create a .env file in the root directory:
# Backend Configuration
SEARXNG_API_URL="http://localhost:8080"
HF_TOKEN=
GROQ_API_KEY=
LOGFIRE_API_KEY=
JINA_API_KEY=
GEMINI_API_KEY=
JWT_SECRET=
JWT_ALGORITHM="HS256"
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7
DATABASE_NAME=
HOSTNAME=
USERNAME=
PASSWORD=
POSTGRES_DB_URL=
ASYNC_POSTGRES_DB_URL=
REDIS_HOST="localhost"
REDIS_PORT=6379
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_ACCESS_TOKEN_URL=https://oauth2.googleapis.com/toke
GOOGLE_AUTHORIZE_URL=https://accounts.google.com/o/oauth2/auth
GOOGLE_API_BASE_URL=https://www.googleapis.com/oauth2/v2/
GOOGLE_USERINFO_ENDPOINT=https://openidconnect.googleapis.com/v2/userinfo
SESSION_SECRET_KEY=The vector database is automatically configured when started with Docker. Data is persisted in the qdrant_storage directory.
Search engine settings are configured in config/searxng-settings.yml. Modify this file to customize search behavior.
- Start a Conversation: Type your insomnia-related question in the chat interface
- Watch the Process: Observe the real-time workflow as agents collaborate
- Review Results: Check the tabs for answers, sources, and detailed workflow
- Ask Follow-ups: Continue the conversation with related questions
- "What are the best natural remedies for insomnia?"
- "How does blue light affect sleep quality?"
- "What are the side effects of melatonin supplements?"
- "How can I create a better sleep environment?"
- "What causes sleep anxiety and how can I manage it?"
SomniFlow includes a comprehensive document processing pipeline for adding custom knowledge:
python run_doc_pipeline.py --step parse-pdfs \
--input_dir ./documents \
--output_dir ./processedpython run_doc_pipeline.py --step generate-metadata \
--input_dir ./processed \
--output_dir ./metadatapython run_doc_pipeline.py --step chunk-documents \
--input_dir ./metadata \
--output_dir ./chunkspython run_doc_pipeline.py --step all \
--input_dir ./documents \
--output_dir ./processedpython bulk_upload_chunks.py ./chunks- Session Management: Conversations are automatically saved and can be resumed
- Source Verification: All information is attributed to reliable sources
- Custom Knowledge: Add your own documents to the knowledge base
- Export Conversations: Download chat history for reference
Once the backend is running, visit http://localhost:8000/docs for interactive API documentation.
somniflow/
βββ π assets/ # Images and static assets
βββ π backend/ # FastAPI backend application
β βββ π api/ # API routes and handlers
β βββ π auth/ # Authentication system
β βββ main.py # FastAPI application entry point
βββ π config/ # Configuration files
β βββ logging_dict_config.json # Logging configuration
β βββ searxng-settings.yml # SearXNG search engine settings
βββ π my-frontend/ # Next.js frontend application
β βββ π app/ # Next.js app directory
β βββ π components/ # React components
β βββ π hooks/ # Custom React hooks
β βββ π lib/ # Utility libraries
βββ π src/ # Core application logic
β βββ π agents/ # Multi-agent system components
β β βββ π base/ # Base classes and shared models
β β βββ π factor/ # Factor analysis agent
β β βββ π harm/ # Harm assessment agent
β β βββ π response/ # Response generation agent
β β βββ π suggestion/ # Suggestion generation agent
β β βββ π supervisor/ # Main supervisor agent
β βββ π common/ # Shared utilities
β β βββ π llm/ # LLM integration
β β βββ π logging/ # Logging setup
β βββ π doc_pipeline/ # Document processing pipeline
β βββ π graph/ # LangGraph workflow definitions
β βββ π tools/ # Various tools and utilities
β βββ π rag/ # Retrieval-augmented generation
β βββ π utils/ # Utility functions
β βββ π web/ # Web scraping and search tools
βββ π pyproject.toml # Python project configuration
βββ π uv.lock # Dependency lock file
βββ π run_doc_pipeline.py # Document processing script
βββ π bulk_upload_chunks.py # Vector store upload script
βββ π README.md # This file
- LangGraph for the multi-agent framework
- Perplexity AI for UI inspiration
- FastAPI for the backend framework
- Next.js for the frontend framework