How yahorbarkouski/semantic-mail works
1. This is a local CLI application for semantic email retrieval and Q&A over Gmail. The core architecture is a pipeline from Gmail OAuth-authenticated email fetches, to embedding generation, to local vector storage, to search and LLM-based answering.
2. The main user entrypoints are the CLI layer in src/cli.py, with main.py likely acting as a thin executable wrapper. The commands exposed by the repo are operational rather than web-facing: setup, sync, search, ask, stats, and models.
3. Gmail access is isolated in src/auth/gmail_auth.py and src/sync/gmail_sync.py. This boundary handles Google OAuth 2.0 credentials, token storage, and message retrieval from the Gmail API. The README indicates it indexes email metadata and body text, not attachments.
4. The sync subsystem is responsible for ingest and normalization. It pulls messages from Gmail, applies query/limit filters, supports incremental updates, and prepares records for indexing. This is the point where raw Gmail data is turned into the repository’s internal email model objects from src/models.py.
5. Embedding generation is abstracted behind src/embedding/base_embedder.py and provider-specific implementations for Ollama and OpenAI. src/embedding/embedder_factory.py selects the active backend, so the rest of the system can treat embeddings as a common interface regardless of whether they come from a local Ollama server or OpenAI’s API.
6. Vector storage and retrieval live in src/search/. src/search/chroma_client.py and src/search/vector_store.py wrap ChromaDB as a persistent local vector database, while src/search/searcher.py performs semantic retrieval against the indexed email corpus. A key architectural detail is that collections are model-specific, so the embedding provider/model pair is part of the storage boundary.
7. The search command is a pure retrieval path: user query -> query embedding -> ChromaDB similarity search -> ranked matching emails. The --detailed option suggests the search layer can return both compact hits and richer previews, but the underlying search boundary remains vector lookup over stored email records.
8. The ask command adds a second-stage LLM reasoning path on top of retrieval. It first searches for relevant emails using the configured embedding model, then passes selected context into an answering model from src/answering/. src/answering/base_llm.py, llm_factory.py, ollama_llm.py, and openai_llm.py form the abstraction layer for generation.
9. The system therefore has two distinct model planes: embeddings for indexing/search and LLMs for natural-language answers. These can be independently configured, but the embedding configuration must match the collection used during sync, which is an important architectural constraint for correctness.
10. Configuration is environment-driven through src/config.py and the .env.example contract. The materially important runtime dependencies are Gmail API credentials, a running Ollama endpoint for local models, and a local Chroma persistence directory. These external services define the app’s operational environment.
11. Persistence is entirely local by design. Email vectors and metadata are stored on the user’s machine in ChromaDB, while Gmail remains the source of truth for message content. That means the app is not syncing to a remote backend; it is building a personal searchable index on disk.
12. At a high level, the architecture is: authenticate to Gmail, fetch and normalize email data, embed it with a chosen model, persist into ChromaDB, then either search semantically or retrieve context for an LLM answer. The separation between auth/sync, embeddings, vector search, and answering is the main boundary map for an architecture diagram.
Main entry points: cli.py CLI · embedder_factory.py Embed Factory · searcher.py Searcher · llm_factory.py LLM Factory
Components 17
CLI
User commands and executable entrypoints
Gmail ingest
Authentication and email synchronization from Gmail
Indexing
Embedding generation and local vector storage
- Embedder · Common embedding interface
src/embedding/base_embedder.py - Embed Factory · Selects embedding backend
src/embedding/embedder_factory.py - Ollama E · Local embedding backend
src/embedding/ollama_embedder.py - OpenAI E · Hosted embedding backend
src/embedding/openai_embedder.py - Chroma Client · Persistent local vector database client
src/search/chroma_client.py - Vector Store · Model-specific collections and persistence boundary
src/search/vector_store.py
Querying
Semantic search and LLM answering
- Searcher · Semantic lookup over indexed emails
src/search/searcher.py - LLM · Common answer generation interface
src/answering/base_llm.py - LLM Factory · Selects answer model backend
src/answering/llm_factory.py - Ollama LLM · Local answer model
src/answering/ollama_llm.py - OpenAI LLM · Hosted answer model
src/answering/openai_llm.py
Runtime
Configuration and external services
- Config · Environment-driven runtime configuration
src/config.py
Connections 26
- main.py → CLI: starts · Executable launches the CLI
- CLI → Auth: setup · Creates or validates Gmail credentials
- CLI → Sync: sync · Triggers mail ingestion
- CLI → Searcher: search · Runs semantic retrieval
- CLI → Searcher: ask · Retrieves context for answering
- CLI → Vector Store: stats · Reads index state and collection info
- CLI → Embed Factory: models · Shows available embedding backends
- Config → Auth: credentials · OAuth and token settings
- Config → Embed Factory: embedding cfg · Selects embedding provider and model
- Config → LLM Factory: llm cfg · Selects answer provider and model
- Auth → Sync: auth context · Provides authenticated Gmail access
- Sync → Models: normalizes · Maps Gmail data into internal email records
- Sync → Embed Factory: embeds · Uses configured embedder during indexing
- Embed Factory → Embedder: implements · Shares a common embedding interface
- Embed Factory → Ollama E: selects · Local embedding backend
- Embed Factory → OpenAI E: selects · Hosted embedding backend
- Ollama E → Vector Store: writes · Persists vectors with model-specific collection keys
- OpenAI E → Vector Store: writes · Persists vectors with model-specific collection keys
- Vector Store → Chroma Client: uses · Wraps ChromaDB persistence and retrieval
- Searcher → Vector Store: queries · Runs similarity search against stored emails
- Searcher → LLM Factory: ask path · Supplies retrieved context to answer generation
- LLM Factory → LLM: implements · Shares a common generation interface
- LLM Factory → Ollama LLM: selects · Local answer backend
- LLM Factory → OpenAI LLM: selects · Hosted answer backend
- Ollama LLM → Searcher: responds with context · Formats an answer from retrieved emails
- OpenAI LLM → Searcher: responds with context · Formats an answer from retrieved emails