# yahorbarkouski/semantic-mail architecture

> Architecture diagram of the GitHub repository yahorbarkouski/semantic-mail, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yahorbarkouski/semantic-mail
- Repository: https://github.com/yahorbarkouski/semantic-mail

## Overview

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: [`src/cli.py`](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/cli.py) (CLI), [`src/embedding/embedder_factory.py`](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/embedder_factory.py) (Embed Factory), [`src/search/searcher.py`](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/search/searcher.py) (Searcher), [`src/answering/llm_factory.py`](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/llm_factory.py) (LLM Factory)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["CLI"]
  node_main_py["main.py<br/>entrypoint<br/>[main.py]"]
  node_cli_py["CLI<br/>commands<br/>[cli.py]"]
end

subgraph group_gmail["Gmail ingest"]
  node_gmail_auth["Auth<br/>[gmail_auth.py]"]
  node_gmail_sync["Sync<br/>ingest<br/>[gmail_sync.py]"]
  node_models_py["Models<br/>domain<br/>[models.py]"]
end

subgraph group_index["Indexing"]
  node_base_embedder["Embedder<br/>interface<br/>[base_embedder.py]"]
  node_embed_factory["Embed Factory"]
  node_ollama_embedder["Ollama E<br/>embedder<br/>[ollama_embedder.py]"]
  node_openai_embedder["OpenAI E<br/>embedder<br/>[openai_embedder.py]"]
  node_chroma_client[("Chroma Client<br/>db client<br/>[chroma_client.py]")]
  node_vector_store[("Vector Store<br/>storage<br/>[vector_store.py]")]
end

subgraph group_query["Querying"]
  node_searcher["Searcher<br/>retrieval<br/>[searcher.py]"]
  node_base_llm["LLM<br/>interface<br/>[base_llm.py]"]
  node_llm_factory["LLM Factory<br/>[llm_factory.py]"]
  node_ollama_llm["Ollama LLM<br/>generator<br/>[ollama_llm.py]"]
  node_openai_llm["OpenAI LLM<br/>generator<br/>[openai_llm.py]"]
end

subgraph group_runtime["Runtime"]
  node_config_py["Config<br/>settings<br/>[config.py]"]
end

node_main_py -->|"starts"| node_cli_py
node_cli_py -->|"setup"| node_gmail_auth
node_cli_py -->|"sync"| node_gmail_sync
node_cli_py -->|"search"| node_searcher
node_cli_py -->|"ask"| node_searcher
node_cli_py -->|"stats"| node_vector_store
node_cli_py -->|"models"| node_embed_factory
node_config_py -->|"credentials"| node_gmail_auth
node_config_py -->|"embedding cfg"| node_embed_factory
node_config_py -->|"llm cfg"| node_llm_factory
node_gmail_auth -->|"auth context"| node_gmail_sync
node_gmail_sync -->|"normalizes"| node_models_py
node_gmail_sync -->|"embeds"| node_embed_factory
node_embed_factory -->|"implements"| node_base_embedder
node_embed_factory -->|"selects"| node_ollama_embedder
node_embed_factory -->|"selects"| node_openai_embedder
node_ollama_embedder -->|"writes"| node_vector_store
node_openai_embedder -->|"writes"| node_vector_store
node_vector_store -->|"uses"| node_chroma_client
node_searcher -->|"queries"| node_vector_store
node_searcher -->|"ask path"| node_llm_factory
node_llm_factory -->|"implements"| node_base_llm
node_llm_factory -->|"selects"| node_ollama_llm
node_llm_factory -->|"selects"| node_openai_llm
node_ollama_llm -->|"responds with context"| node_searcher
node_openai_llm -->|"responds with context"| node_searcher

click node_main_py "https://github.com/yahorbarkouski/semantic-mail/blob/main/main.py"
click node_cli_py "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/cli.py"
click node_config_py "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/config.py"
click node_gmail_auth "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/auth/gmail_auth.py"
click node_gmail_sync "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/sync/gmail_sync.py"
click node_models_py "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/models.py"
click node_base_embedder "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/base_embedder.py"
click node_embed_factory "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/embedder_factory.py"
click node_ollama_embedder "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/ollama_embedder.py"
click node_openai_embedder "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/openai_embedder.py"
click node_chroma_client "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/search/chroma_client.py"
click node_vector_store "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/search/vector_store.py"
click node_searcher "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/search/searcher.py"
click node_base_llm "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/base_llm.py"
click node_llm_factory "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/llm_factory.py"
click node_ollama_llm "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/ollama_llm.py"
click node_openai_llm "https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/openai_llm.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main_py,node_cli_py toneBlue
class node_gmail_auth,node_gmail_sync,node_models_py toneAmber
class node_base_embedder,node_embed_factory,node_ollama_embedder,node_openai_embedder,node_chroma_client,node_vector_store toneMint
class node_searcher,node_base_llm,node_llm_factory,node_ollama_llm,node_openai_llm toneRose
class node_config_py toneIndigo
```

## Components

### CLI

User commands and executable entrypoints

- [main.py](https://github.com/yahorbarkouski/semantic-mail/blob/main/main.py): entrypoint · Thin executable wrapper · `main.py`
- [CLI](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/cli.py): commands · Command dispatcher for setup, sync, search, ask, stats, models · `src/cli.py`

### Gmail ingest

Authentication and email synchronization from Gmail

- [Auth](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/auth/gmail_auth.py): oauth · Gmail OAuth credentials and token handling · `src/auth/gmail_auth.py`
- [Sync](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/sync/gmail_sync.py): ingest · Fetches, filters, and normalizes Gmail messages · `src/sync/gmail_sync.py`
- [Models](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/models.py): domain · Internal email record structures · `src/models.py`

### Indexing

Embedding generation and local vector storage

- [Embedder](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/base_embedder.py): interface · Common embedding interface · `src/embedding/base_embedder.py`
- [Embed Factory](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/embedder_factory.py): factory · Selects embedding backend · `src/embedding/embedder_factory.py`
- [Ollama E](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/ollama_embedder.py): embedder · Local embedding backend · `src/embedding/ollama_embedder.py`
- [OpenAI E](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/embedding/openai_embedder.py): embedder · Hosted embedding backend · `src/embedding/openai_embedder.py`
- [Chroma Client](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/search/chroma_client.py): db client · Persistent local vector database client · `src/search/chroma_client.py`
- [Vector Store](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/search/vector_store.py): storage · Model-specific collections and persistence boundary · `src/search/vector_store.py`

### Querying

Semantic search and LLM answering

- [Searcher](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/search/searcher.py): retrieval · Semantic lookup over indexed emails · `src/search/searcher.py`
- [LLM](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/base_llm.py): interface · Common answer generation interface · `src/answering/base_llm.py`
- [LLM Factory](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/llm_factory.py): factory · Selects answer model backend · `src/answering/llm_factory.py`
- [Ollama LLM](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/ollama_llm.py): generator · Local answer model · `src/answering/ollama_llm.py`
- [OpenAI LLM](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/answering/openai_llm.py): generator · Hosted answer model · `src/answering/openai_llm.py`

### Runtime

Configuration and external services

- [Config](https://github.com/yahorbarkouski/semantic-mail/blob/main/src/config.py): settings · Environment-driven runtime configuration · `src/config.py`

## Connections

- 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
