# thomasruegg/generative-excel-data-assistant architecture

> Architecture diagram of the GitHub repository thomasruegg/generative-excel-data-assistant, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thomasruegg/generative-excel-data-assistant
- Repository: https://github.com/thomasruegg/generative-excel-data-assistant

## Overview

1. **Overall shape**
   - GEDA is a single Python application, launched from `gui.py`, that combines a Gradio chat UI, Excel ingestion/normalization, an LLM-based function-calling orchestrator, and a library of domain-specific data operations.
   - The repo is organized around one core pattern: the user speaks in natural language, the system selects a safe predefined Python function, executes it on one or more Excel files, and returns text, tables, charts, or a rewritten workbook.

2. **User-facing boundary**
   - The only interactive front door is the Gradio interface.
   - It handles chat messages, file uploads, model selection, and file download of edited Excel outputs.
   - The presence of `.gradio/certificate.pem` suggests the UI can run with local HTTPS support, but the main architectural point is that Gradio is the presentation and session boundary, not a separate backend service.

3. **Excel file ingestion**
   - Uploaded spreadsheets are first inspected to detect the real header row and any title/description text above it.
   - Those files are then loaded into Pandas DataFrames for downstream querying and transformation.
   - `data_loader.py` and `excel_preparations.py` appear to own this preprocessing boundary, turning messy Excel inputs into structured in-memory data that the rest of the system can use.

4. **Metadata extraction and caching**
   - After upload, GEDA generates per-file metadata such as data type, country code, year range, standardized column mapping, and a checksum.
   - This metadata is cached locally as JSON so the system does not need to re-infer file meaning on every request.
   - The checksum is the invalidation mechanism: if the file changes, metadata is refreshed.
   - This layer is important because it is what allows the assistant to identify the right file among many similar spreadsheets.

5. **File mapping for Gradio downloads**
   - Because Gradio stores uploaded files in temp locations, the app maintains a file-path mapping so edited workbooks can be rediscovered and exposed back through the UI.
   - This is a small but critical infrastructure boundary: it connects transient upload paths to persistent user-visible download behavior.

6. **Function catalog as the core capability layer**
   - The real “product logic” lives in `functions.py`.
   - These functions implement supported tasks such as querying cells, aggregating sales/cost data, comparing time ranges, creating new columns, plotting, currency conversion, and editing Excel files.
   - The design is intentionally controlled: the assistant can only do what is encoded as a function, which limits unsafe or unintended data manipulation.

7. **LLM orchestration and function calling**
   - `function_calling_agent.py` is the central orchestrator.
   - It takes the user prompt, retrieves the most relevant candidate functions, passes those candidates plus parameter descriptions to the LLM, and then executes the selected function.
   - `prompt.txt` likely contains the system or tool-selection prompt used to steer model behavior.
   - The architecture is therefore “LLM as router,” not “LLM as free-form analyst.”

8. **Retrieval-Augmented Generation for function selection**
   - GEDA uses RAG to narrow a large function catalog down to the top 5 likely matches before asking the LLM to choose.
   - The README describes FAISS as the vector-store/retrieval engine for function names and descriptions.
   - This retrieval step is a key architectural boundary: it keeps prompts small, reduces hallucinated tool calls, and makes the assistant scalable as more functions are added.

9. **Model abstraction**
   - `llm_factory.py` likely hides provider differences behind a common interface.
   - The repo supports multiple backends: OpenAI `gpt-4o-mini`, hosted models on llmhub, and local Ollama models such as `llama3.2`.
   - This makes the model layer interchangeable, while the rest of the system stays focused on tool execution rather than provider-specific APIs.

10. **Data processing and output generation**
   - Once a function is selected, it operates on Pandas data and may produce:
     - text answers,
     - tabular results,
     - charts/plots,
     - or modified Excel files.
   - Excel editing uses `XlsxWriter` when a workbook must be rewritten.
   - Plotting is a first-class output type, and the README emphasizes interactive plots as part of the user experience.

11. **External service dependencies**
   - Currency conversion is supported through an external web API, so the assistant is not entirely self-contained for financial normalization.
   - LLM calls may go to OpenAI, llmhub, or a locally running Ollama server, depending on configuration in `.env`.
   - These external dependencies affect latency, cost, and offline capability, so they are meaningful architectural components.

12. **Local filesystem as the main state store**
   - There is no sign of a separate database or server-side persistence layer.
   - State is primarily local: uploaded Excel files, cached metadata JSON, mapping files, and model/config environment variables.
   - That makes the app lightweight and easy to run, but also means cache/session state is ephemeral across restarts.

13. **Repository content and sample data**
   - The `data/` folder contains representative Excel workbooks for sales and material cost/inventory across multiple countries and years.
   - These samples are likely used for development, demos, and testing the file-identification logic against realistic spreadsheet variants.
   - `data_notice.txt` suggests there are usage or provenance constraints on the dataset.

14. **End-to-end flow to diagram**
   - The main data flow is: upload Excel files → detect structure and create DataFrames → infer/cache metadata → user prompt enters chat → retrieve top candidate functions via RAG → LLM selects a function and parameters → function runs on the relevant file(s) → result is rendered or saved → user optionally downloads the edited workbook.
   - In an architecture diagram, the critical boundaries are: UI, file ingestion/metadata, function retrieval, LLM routing, execution/processing, and output/download handling.

Main entry points: [`function_calling_agent.py`](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/function_calling_agent.py) (Agent), [`functions.py`](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py) (Functions), [`llm_factory.py`](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/llm_factory.py) (Model Factory), [`gui.py`](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/gui.py) (Gradio UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Presentation"]
  node_gui["Gradio UI<br/>web app<br/>[gui.py]"]
  node_file_map["File Map<br/>path mapping<br/>[utils.py]"]
end

subgraph group_ingest["Excel Intake"]
  node_data_loader["Loader<br/>ingest<br/>[data_loader.py]"]
  node_excel_prep["Prep<br/>structure detect"]
  node_meta_cache[("Metadata Cache<br/>json cache<br/>[utils.py]")]
end

subgraph group_orchestrate["Tool Routing"]
  node_agent["Agent<br/>router"]
  node_prompt["Prompt<br/>tool policy<br/>[prompt.txt]"]
  node_retriever{{"Retriever<br/>RAG<br/>[functions.py]"}}
end

subgraph group_execute["Operations"]
  node_functions["Functions<br/>domain ops<br/>[functions.py]"]
  node_plot(("Charts<br/>plotting<br/>[functions.py]"))
  node_xlsx["Workbook Writer<br/>excel export<br/>[functions.py]"]
end

subgraph group_external["Dependencies"]
  node_llm_factory["Model Factory<br/>provider abstraction<br/>[llm_factory.py]"]
  node_openai(("OpenAI<br/>llm api"))
  node_ollama(("Ollama<br/>local llm"))
  node_llmhub(("LLM Hub<br/>hosted llm"))
  node_currency(("FX API<br/>external api"))
end

subgraph group_data["Sample Data"]
  node_samples[("Sample Data<br/>workbooks")]
end

node_gui -->|"upload files"| node_data_loader
node_gui -->|"chat prompt"| node_agent
node_gui -->|"download links"| node_file_map
node_data_loader -->|"normalize sheet"| node_excel_prep
node_excel_prep -->|"infer metadata"| node_meta_cache
node_meta_cache -->|"file context"| node_agent
node_agent -->|"top candidates"| node_retriever
node_retriever -->|"catalog search"| node_functions
node_agent -->|"build prompt"| node_prompt
node_agent -->|"select model"| node_llm_factory
node_llm_factory -->|"provider"| node_openai
node_llm_factory -->|"provider"| node_ollama
node_llm_factory -->|"provider"| node_llmhub
node_agent -->|"execute tool"| node_functions
node_functions -->|"render chart"| node_plot
node_functions -->|"write workbook"| node_xlsx
node_functions -->|"convert values"| node_currency
node_functions -->|"register output"| node_file_map
node_data_loader -->|"read examples"| node_samples

click node_gui "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/gui.py"
click node_file_map "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/utils.py"
click node_data_loader "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/data_loader.py"
click node_excel_prep "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/excel_preparations.py"
click node_meta_cache "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/utils.py"
click node_agent "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/function_calling_agent.py"
click node_prompt "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/prompt.txt"
click node_retriever "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py"
click node_llm_factory "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/llm_factory.py"
click node_functions "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py"
click node_plot "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py"
click node_xlsx "https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py"
click node_samples "https://github.com/thomasruegg/generative-excel-data-assistant/tree/main/data"

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_gui,node_file_map toneBlue
class node_data_loader,node_excel_prep,node_meta_cache toneAmber
class node_agent,node_prompt,node_retriever toneMint
class node_functions,node_plot,node_xlsx toneRose
class node_llm_factory,node_openai,node_ollama,node_llmhub,node_currency toneIndigo
class node_samples toneTeal
```

## Components

### Presentation

Gradio chat interface and file download boundary

- [Gradio UI](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/gui.py): web app · Chat front door for prompts, uploads, model choice, and downloads · `gui.py`
- [File Map](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/utils.py): path mapping · Tracks transient upload paths and edited workbook locations · `utils.py`

### Excel Intake

Spreadsheet loading, structure detection, and metadata caching

- [Loader](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/data_loader.py): ingest · Loads uploaded spreadsheets into DataFrames · `data_loader.py`
- [Prep](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/excel_preparations.py): structure detect · Finds real headers, title text, and cleans spreadsheet layout · `excel_preparations.py`
- [Metadata Cache](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/utils.py): json cache · Stores per-file inferred metadata, checksum, and column mapping · `utils.py`

### Tool Routing

Retriever plus LLM-driven function selection

- [Agent](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/function_calling_agent.py): router · Retrieves candidate functions, asks the LLM, and executes the chosen tool · `function_calling_agent.py`
- [Prompt](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/prompt.txt): tool policy · Instructions and selection guidance for function choice · `prompt.txt`
- [Retriever](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py): RAG · Ranks likely functions before the LLM sees the full catalog · `functions.py`

### Operations

Domain functions, data processing, and output generation

- [Functions](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py): domain ops · Controlled catalog for querying, aggregating, transforming, plotting, and editing spreadsheets · `functions.py`
- [Charts](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py): plotting · Produces visual outputs from tabular results · `functions.py`
- [Workbook Writer](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/functions.py): excel export · Rewrites edited spreadsheets for download · `functions.py`

### Dependencies

LLM providers and external APIs

- [Model Factory](https://github.com/thomasruegg/generative-excel-data-assistant/blob/main/llm_factory.py): provider abstraction · Unifies OpenAI, hosted, and local model backends · `llm_factory.py`
- **OpenAI**: llm api · Hosted model backend used for routing and reasoning
- **Ollama**: local llm · Local model backend for offline or self-hosted execution
- **LLM Hub**: hosted llm · Alternative hosted backend for chat or tool selection
- **FX API**: external api · Converts currencies for financial normalization

### Sample Data

Representative workbook inputs and notices

- [Sample Data](https://github.com/thomasruegg/generative-excel-data-assistant/tree/main/data): workbooks · Representative sales and material spreadsheets for demos and development · `data`

## Connections

- Gradio UI → Loader: upload files · User uploads Excel files through the chat UI
- Gradio UI → Agent: chat prompt · Natural language requests enter the orchestration layer
- Gradio UI → File Map: download links · Restores edited workbook paths for UI download
- Loader → Prep: normalize sheet · Loaded workbooks are inspected for real headers and structure
- Prep → Metadata Cache: infer metadata · Extracted file meaning and mappings are cached locally
- Metadata Cache → Agent: file context · Cached metadata helps the agent target the right workbook
- Agent → Retriever: top candidates · Ranks the most relevant functions for the request
- Retriever → Functions: catalog search · Searches the function library by name and description
- Agent → Prompt: build prompt · Adds selection policy and parameter guidance
- Agent → Model Factory: select model · Resolves the configured model backend
- Model Factory → OpenAI: provider · Routes calls to OpenAI when configured
- Model Factory → Ollama: provider · Routes calls to a local Ollama server when configured
- Model Factory → LLM Hub: provider · Routes calls to a hosted llmhub model when configured
- Agent → Functions: execute tool · Runs the selected predefined function on spreadsheet data
- Functions → Charts: render chart · Some operations emit plots as final output
- Functions → Workbook Writer: write workbook · Edits are persisted into a new Excel file when needed
- Functions → FX API: convert values · Financial normalization may call an external FX service
- Functions → File Map: register output · Edited workbook paths are stored for later download
- Loader → Sample Data: read examples · Sample workbooks provide realistic development inputs
