The Sleep Assistant is a LangGraph-powered multi-agent experience that routes every user message to the right expertise. It distinguishes casual greetings from sleep-focused questions, enriches sleep conversations with MongoDB Atlas Vector Search context retrieval, and serves responses through a CLI helper and a FastAPI endpoint.
- FastAPI service for real-time chat over HTTP on port 8001.
- CLI companion (
scripts/run_chatbot.py) for local conversational testing. - LangGraph orchestration with an intent router, general chat node, and sleep-specialist node.
- Sleep knowledge base powered by MongoDB Atlas Vector Search and OpenAI embeddings.
- Environment-driven configuration with a simple
.envworkflow. - Optional observability through LangSmith and structured logging.
- The router prompt classifies each incoming message as either
generalorsleep. - Greetings and small talk are answered by the general node via an LLM tuned for casual conversation.
- Sleep-related questions trigger MongoDB vector retrieval, combining matched snippets with a dedicated sleep prompt before responding.
- Python 3.11 (CPython recommended)
- OpenAI API credentials with access to the configured chat and embedding models
- MongoDB Atlas cluster with an enabled vector search index
- Tesseract OCR binary (required if you ingest scanned PDFs with the included script)
- Optional: Conda for
environment.yml, or any other virtual environment manager - Optional: LangSmith account for tracing (
LANGCHAIN_*variables)
Option A - Conda
conda env create -f environment.yml
conda activate sleep-assistantOption B - Python venv
python -m venv .venv
.\.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS/Linux
pip install -r requirements.txtThis installs the runtime dependencies listed in requirements.txt.
Copy .env.example to .env and fill in the required values. Minimum variables include:
OPENAI_API_KEY- API key for OpenAI.OPENAI_BASE_URL- Base URL for the OpenAI-compatible endpoint.CHAT_MODEL- Chat model name (defaults togpt-4o-mini).EMBEDDING_MODEL- Embedding model name (defaults totext-embedding-3-small).MONGODB_URI- Full MongoDB connection string (preferred).- or
MONGODB_USERNAME,MONGODB_PASSWORD,MONGODB_CLUSTER_URL- Provide these if you want the app to build the URI. MONGODB_DBNAME- Database containing your vectorized documents.MONGODB_COLLECTION- Collection used for vector search.MONGODB_VECTOR_INDEX- Atlas vector index name (defaults tovector_index).MONGODB_EMBEDDING_FIELD- Document field that stores embeddings (defaults toembedding).MONGODB_VECTOR_CANDIDATES- (Optional) Override the AtlasnumCandidatessetting for fine control.
Optional extras:
LANGCHAIN_TRACING_V2,LANGCHAIN_ENDPOINT,LANGCHAIN_API_KEY- Enable LangSmith telemetry.
Keep the .env file out of version control.
python -m pip check
python -m fastapi --help # optional sanity checkpython scripts/run_chatbot.pyYou will be dropped into an interactive session. Messages are routed exactly as the API would handle them.
python scripts/run_api.py --reload --port 8001Sample request:
curl -X POST http://127.0.0.1:8001/chat ^
-H "Content-Type: application/json" ^
-d "{\"message\": \"Why do I wake up in the middle of the night?\"}"On Unix shells replace the line continuation character ^ with \.
Launch a lightweight UI with Streamlit once your environment variables are configured:
From the project root directory:
streamlit run streamlit_app.pyThe app will start and automatically open in your default web browser at http://localhost:8501. If it doesn't open automatically, navigate to that URL manually.
Features:
- Interactive chat interface for conversing with the Sleep Assistant
- Full conversation transcript with message history
- Sidebar showing session metrics (user turns, latest route)
- "Start new conversation" button to reset the session without restarting the server
- Display of knowledge sources (MongoDB Atlas snippets) that informed each response
- Relevance scores for retrieved documents
Requirements:
- Ensure your
.envfile is properly configured with OpenAI and MongoDB credentials - All dependencies from
requirements.txtmust be installed (includingstreamlit>=1.37)
Stopping the app:
- Press
Ctrl+Cin the terminal where Streamlit is running
Populate the MongoDB collection referenced by MONGODB_COLLECTION with the documents you want the assistant to cite. A typical workflow is:
- Extract text from your PDFs (PyMuPDF works well for digital text; fall back to Tesseract OCR for scanned pages).
- Chunk each document and call
sleep_assistant.services.llm.build_embedder()to generate OpenAI embeddings. - Insert documents into MongoDB with an
embeddingarray plus metadata fields such astext,source_document, andpage_number. - Create a MongoDB Atlas Vector Search index that targets the embedding field named in
MONGODB_EMBEDDING_FIELD.
Once populated, the sleep node automatically queries this collection and surfaces the snippets with the highest similarity scores.
- Ensure your
.envfile is populated with the required OpenAI and MongoDB credentials. - Build the image:
docker build -t sleep-assistant . - Run the API service (port 8001) with the environment file mounted:
docker run --env-file .env -p 8001:8001 sleep-assistant
Alternatively, you can use the included Compose file:
docker compose up --buildThe Compose service loads variables from .env and exposes the API on http://localhost:8001.
.
|-- scripts/
| |-- run_api.py # FastAPI launcher
| |-- run_chatbot.py # CLI entrypoint
|-- src/
| |-- ingest/ # PDF/OCR utilities for knowledge prep
| |-- sleep_assistant/
| |-- api/ # FastAPI app, routers, schemas, validators
| |-- config/ # Environment helpers and settings
| |-- graph/ # LangGraph wiring (nodes, prompts, state, edges)
| |-- services/ # LLM and vector store factories
| |-- cli.py # CLI runner utilities
|-- .env.example # Template for required secrets
|-- environment.yml # Conda environment specification
|-- requirements.txt # Pip requirements for virtualenv installs
|-- README.md
- Router misclassification - Ensure the router prompt in
src/sleep_assistant/graph/prompts/router.pymatches the latest specification; escape braces for literal JSON examples. - MongoDB connectivity errors - Double-check the URI (or username/password/cluster trio), ensure the database and collection exist, and confirm the vector index has finished building.
- Model errors - The assistant depends on both chat and embedding models. Confirm the environment variables align with your OpenAI deployment.
- LangGraph state issues - Clear the in-memory session by restarting the process; persistent storage is not included in this starter.
- Add evaluation via LangSmith and exported traces.
- Wire in persistent conversation storage or retrieval augmentation beyond MongoDB vector search.
- Extend the graph with new specialist nodes (for example nutrition or mindfulness) by following the patterns in
src/sleep_assistant/graph/nodes/.