# mejji/datamuse---ai-datavisuzaliztion architecture

> Architecture diagram of the GitHub repository mejji/datamuse---ai-datavisuzaliztion, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mejji/datamuse---ai-datavisuzaliztion
- Repository: https://github.com/mejji/datamuse---ai-datavisuzaliztion

## Overview

1. **Overall shape**
   - DataMuse is a two-tier local application: a React/Vite frontend talks to a FastAPI backend, and the backend persists semantic context in Qdrant. The app is centered on a single user-uploaded CSV dataset that then feeds chat, charting, story generation, and data mutation workflows.

2. **Frontend boundary**
   - The frontend is a TypeScript React 19 app with Vite, Zustand for shared state, and a component-driven UI. `App.tsx` orchestrates the major modes: upload, explore/chat, dashboard, and story building.
   - Key UI surfaces are the upload zone, Muse companion chat panel, interactive dashboard, chart/table renderers, and story editor. The frontend is primarily a presentation and interaction layer; the analytical work happens in the backend.

3. **Backend boundary**
   - The backend is a FastAPI service organized by feature routers: `upload`, `chat`, `analyze`, `story`, and `data`. This is the main API boundary for the entire product.
   - `main.py` is the application entry point and also the place where cross-cutting concerns like CORS are configured.
   - Pydantic schemas under `models/schemas.py` define the request/response contracts between frontend and backend.

4. **Dataset ingestion path**
   - CSV upload enters through `/api/upload`, where the backend profiles the file and creates the dataset context used everywhere else.
   - `csv_profiler.py` analyzes columns and basic structure, while `data_tools.py` and related services help turn raw data into queryable and displayable forms.
   - The resulting dataset profile is exposed back to the UI so the app can immediately show column summaries, prompts, and suggested next actions.

5. **Semantic indexing and retrieval**
   - Uploaded data is also embedded and indexed for retrieval-augmented generation. `embeddings.py` handles chunking and vector creation using fastembed/ONNX, and `qdrant_service.py` manages Qdrant operations.
   - This creates a semantic memory layer that the LLM can consult for grounded answers, rather than relying only on the current prompt or raw tabular operations.
   - Qdrant runs as a separate service via Docker Compose, making it an explicit infrastructure dependency of the architecture.

6. **Chat and analytical tool use**
   - `/api/chat` is the main conversational workflow. The backend combines the dataset context, vector retrieval, and tool-based operations to answer questions, generate charts, and explain results.
   - `data_tools.py` is the functional boundary for actions like querying data, creating charts/tables, computing stats, and detecting patterns. This is the bridge between natural language and deterministic dataset operations.
   - Frontend chat components render the conversation and can embed charts inline when the backend returns structured visualization output.

7. **Tiered LLM router**
   - `llm_service.py` is the central intelligence layer. It routes each request to a model tier based on complexity, pins conversations to a tier/model, and handles failover when providers rate-limit or exhaust quotas.
   - `config.py` contains the model pool and complexity classification logic. The architecture deliberately separates “trivial metadata” requests from “strong reasoning/tool use” requests, which is important for understanding why some endpoints feel faster and others are more capable.
   - This router is a major system boundary: it abstracts away multiple providers while presenting one unified AI service to the rest of the backend.

8. **Fixed-purpose AI endpoints**
   - Some backend routes are intentionally specialized. `/api/analyze` is for visualization suggestions and `/api/story/generate` is for story generation; both are forced to use the strongest tier because they need reliable structured output and longer-form reasoning.
   - `muse_prompts.py` contains the system prompt and analytical guidance that shape the assistant’s behavior across these workflows.
   - These are distinct from ordinary chat because they are output-constrained product features, not open-ended conversation.

9. **Story-building subsystem**
   - The story flow is its own product area rather than just “chat output.” The backend exposes story angle, refinement, generation, and save routes, while the frontend provides a dedicated story builder and chapter components.
   - This subsystem consumes the dataset context and generated charts/narratives to produce editable, chapter-based stories that can be revised before saving.
   - It shares the same dataset and LLM infrastructure as chat, but the UX and output format are different enough to treat it as a separate architectural slice.

10. **Data mutation and lineage**
   - `/api/apply`, `/api/undo`, `/api/download/{id}`, and `/api/history/{id}` form a mutation workflow around the dataset. The app can apply transformations, revert them, and export the mutated result.
   - This creates a lightweight data versioning layer focused on user-driven analysis changes rather than full warehouse-style persistence.
   - The frontend includes preview-oriented components so users can inspect the effect of a mutation before committing to it.

11. **State management and data flow in the UI**
   - Zustand in `useDataStore.ts` acts as the shared client-side state for the active dataset, chat state, dashboard contents, and story artifacts.
   - API access is centralized in `lib/api.ts`, so the UI’s data flow is largely: upload or select dataset → store profile/context → trigger chat/analyze/story requests → render charts, panels, and chapters.
   - This keeps the UI reactive while the backend remains the source of truth for data processing.

12. **Operational setup and deployment model**
   - The repository is optimized for local use: startup scripts launch Qdrant, the backend, and the frontend together, and the README explicitly recommends local deployment because of vector DB and model/API constraints.
   - `docker-compose.yml` exists specifically for the Qdrant dependency; the rest of the stack runs as standard app servers.
   - Environment variables provide the external service credentials and Qdrant connection details, which means the main runtime dependencies are configuration-driven rather than hardcoded.

13. **External service boundaries**
   - The only major external dependencies are LLM providers (Groq, Cerebras, Gemini) and the Qdrant vector database. The backend abstracts provider-specific behavior so the frontend never talks to these services directly.
   - Because the app depends on multiple AI providers and a local vector store, the backend is the integration hub and the frontend stays provider-agnostic.

14. **What to show in an architecture diagram**
   - A good diagram should highlight: browser UI → FastAPI API → dataset processing/LLM services → Qdrant, plus the separate Qdrant container.
   - Within the backend, show three main functional paths: upload/profiling, chat/analysis with model routing and tools, and story/mutation workflows.
   - The most important boundary is that the frontend only renders and collects interactions; the backend owns data understanding, retrieval, model routing, and all generated outputs.

Main entry points: [`backend/app/main.py`](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/main.py) (API Entry), [`backend/app/services/data_tools.py`](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/data_tools.py) (Data Tools), [`backend/app/services/llm_service.py`](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/llm_service.py) (LLM Router), [`start.sh`](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/start.sh) (Local Stack)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_browser_app["Browser UI<br/>React app<br/>[App.tsx]"]
  node_ui_components["UI Components<br/>React components"]
  node_client_store["Client Store<br/>Zustand state<br/>[useDataStore.ts]"]
  node_api_client["API Client<br/>HTTP client<br/>[api.ts]"]
end

subgraph group_backend["Backend"]
  node_api_entry["API Entry<br/>FastAPI app<br/>[main.py]"]
  node_api_contracts["Schemas<br/>Pydantic models<br/>[schemas.py]"]
  node_upload_router["Upload API<br/>router<br/>[upload.py]"]
  node_chat_router["Chat API<br/>router<br/>[chat.py]"]
  node_analyze_router["Analyze API<br/>router<br/>[analyze.py]"]
  node_story_router["Story API<br/>router<br/>[story.py]"]
  node_data_router["Data API<br/>router<br/>[data.py]"]
  node_csv_profiler["CSV Profiler<br/>profiling service<br/>[csv_profiler.py]"]
  node_data_tools["Data Tools<br/>analysis tools<br/>[data_tools.py]"]
  node_llm_router["LLM Router<br/>model orchestration<br/>[llm_service.py]"]
  node_prompt_layer["Prompts<br/>prompting<br/>[muse_prompts.py]"]
  node_embed_service["Embeddings<br/>vectorization<br/>[embeddings.py]"]
  node_qdrant_service["Qdrant Access<br/>vector store client<br/>[qdrant_service.py]"]
end

subgraph group_infra["Infrastructure"]
  node_qdrant_db[("Qdrant<br/>vector database<br/>[docker-compose.yml]")]
  node_runtime_stack["Local Stack<br/>startup scripts<br/>[start.sh]"]
end

node_browser_app -->|"calls"| node_api_client
node_browser_app -->|"renders"| node_ui_components
node_ui_components -->|"reads/writes"| node_client_store
node_api_client -->|"HTTP API"| node_api_entry
node_api_entry -->|"uses"| node_api_contracts
node_api_entry -->|"mounts"| node_upload_router
node_api_entry -->|"mounts"| node_chat_router
node_api_entry -->|"mounts"| node_analyze_router
node_api_entry -->|"mounts"| node_story_router
node_api_entry -->|"mounts"| node_data_router
node_upload_router -->|"profiles"| node_csv_profiler
node_upload_router -->|"prepares"| node_data_tools
node_chat_router -->|"uses"| node_data_tools
node_chat_router -->|"orchestrates"| node_llm_router
node_analyze_router -->|"forces strong tier"| node_llm_router
node_story_router -->|"forces strong tier"| node_llm_router
node_story_router -->|"guides"| node_prompt_layer
node_llm_router -->|"consumes"| node_prompt_layer
node_data_tools -->|"feeds"| node_embed_service
node_embed_service -->|"stores vectors"| node_qdrant_service
node_qdrant_service -->|"persists to"| node_qdrant_db
node_data_router -->|"mutates"| node_data_tools
node_runtime_stack -->|"starts"| node_api_entry
node_runtime_stack -->|"starts"| node_browser_app
node_runtime_stack -->|"starts"| node_qdrant_db

click node_browser_app "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/frontend/src/App.tsx"
click node_ui_components "https://github.com/mejji/datamuse---ai-datavisuzaliztion/tree/master/frontend/src/components"
click node_client_store "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/frontend/src/stores/useDataStore.ts"
click node_api_client "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/frontend/src/lib/api.ts"
click node_api_entry "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/main.py"
click node_api_contracts "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/models/schemas.py"
click node_upload_router "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/upload.py"
click node_chat_router "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/chat.py"
click node_analyze_router "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/analyze.py"
click node_story_router "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/story.py"
click node_data_router "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/data.py"
click node_csv_profiler "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/csv_profiler.py"
click node_data_tools "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/data_tools.py"
click node_llm_router "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/llm_service.py"
click node_prompt_layer "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/muse_prompts.py"
click node_embed_service "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/embeddings.py"
click node_qdrant_service "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/qdrant_service.py"
click node_qdrant_db "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/docker-compose.yml"
click node_runtime_stack "https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/start.sh"

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_browser_app,node_ui_components,node_client_store,node_api_client toneBlue
class node_api_entry,node_api_contracts,node_upload_router,node_chat_router,node_analyze_router,node_story_router,node_data_router,node_csv_profiler,node_data_tools,node_llm_router,node_prompt_layer,node_embed_service,node_qdrant_service toneAmber
class node_qdrant_db,node_runtime_stack toneMint
```

## Components

### Frontend

React/Vite browser app and UI state

- [Browser UI](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/frontend/src/App.tsx): React app · User-facing upload, chat, dashboard, and story experiences · `frontend/src/App.tsx`
- [UI Components](https://github.com/mejji/datamuse---ai-datavisuzaliztion/tree/master/frontend/src/components): React components · Reusable panels for charts, chat, tables, stories, and previews · `frontend/src/components`
- [Client Store](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/frontend/src/stores/useDataStore.ts): Zustand state · Shared dataset, chat, dashboard, and story state · `frontend/src/stores/useDataStore.ts`
- [API Client](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/frontend/src/lib/api.ts): HTTP client · Centralized frontend calls into the backend API · `frontend/src/lib/api.ts`

### Backend

FastAPI API, services, and domain logic

- [API Entry](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/main.py): FastAPI app · Application entrypoint and cross-cutting API configuration · `backend/app/main.py`
- [Schemas](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/models/schemas.py): Pydantic models · Request and response contracts shared across routes · `backend/app/models/schemas.py`
- [Upload API](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/upload.py): router · CSV ingestion and dataset profile creation · `backend/app/routers/upload.py`
- [Chat API](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/chat.py): router · Conversational Q&A, tool use, and grounded responses · `backend/app/routers/chat.py`
- [Analyze API](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/analyze.py): router · Visualization suggestions and analysis output · `backend/app/routers/analyze.py`
- [Story API](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/story.py): router · Story generation, refinement, and save flows · `backend/app/routers/story.py`
- [Data API](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/routers/data.py): router · Mutation, history, download, and undo endpoints · `backend/app/routers/data.py`
- [CSV Profiler](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/csv_profiler.py): profiling service · Inspects uploaded columns and basic dataset structure · `backend/app/services/csv_profiler.py`
- [Data Tools](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/data_tools.py): analysis tools · Deterministic querying, charts, stats, and transformations · `backend/app/services/data_tools.py`
- [LLM Router](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/llm_service.py): model orchestration · Routes requests across model tiers with failover and pinning · `backend/app/services/llm_service.py`
- [Prompts](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/muse_prompts.py): prompting · System guidance and task-specific assistant instructions · `backend/app/services/muse_prompts.py`
- [Embeddings](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/embeddings.py): vectorization · Chunks data and creates semantic embeddings · `backend/app/services/embeddings.py`
- [Qdrant Access](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/backend/app/services/qdrant_service.py): vector store client · Manages semantic indexing and retrieval in Qdrant · `backend/app/services/qdrant_service.py`

### Infrastructure

Local runtime dependencies and deployment glue

- [Qdrant](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/docker-compose.yml): vector database · External semantic memory store used by the backend · `docker-compose.yml`
- [Local Stack](https://github.com/mejji/datamuse---ai-datavisuzaliztion/blob/master/start.sh): startup scripts · Launches frontend, backend, and vector store for local use · `start.sh`

## Connections

- Browser UI → API Client: calls · UI actions are translated into backend requests
- Browser UI → UI Components: renders · Top-level app composes the interactive surfaces
- UI Components → Client Store: reads/writes · Components bind to shared client state
- API Client → API Entry: HTTP API · Frontend talks only to the FastAPI backend
- API Entry → Schemas: uses · Routes exchange typed request and response payloads
- API Entry → Upload API: mounts · Upload workflow is exposed as a route group
- API Entry → Chat API: mounts · Chat workflow is exposed as a route group
- API Entry → Analyze API: mounts · Analysis workflow is exposed as a route group
- API Entry → Story API: mounts · Story workflow is exposed as a route group
- API Entry → Data API: mounts · Mutation and lineage workflow is exposed as a route group
- Upload API → CSV Profiler: profiles · Uploaded CSVs are inspected and summarized
- Upload API → Data Tools: prepares · Profiled dataset context is made queryable
- Chat API → Data Tools: uses · Chat can run deterministic dataset operations
- Chat API → LLM Router: orchestrates · Conversational responses are routed through the model tier system
- Analyze API → LLM Router: forces strong tier · Structured analysis and visualization suggestions use the strongest model tier
- Story API → LLM Router: forces strong tier · Story generation relies on the strongest model tier
- Story API → Prompts: guides · Story behavior is shaped by dedicated prompts
- LLM Router → Prompts: consumes · Model routing is influenced by prompt and complexity logic
- Data Tools → Embeddings: feeds · Data can be chunked for semantic indexing
- Embeddings → Qdrant Access: stores vectors · Embeddings are written and retrieved through the vector store client
- Qdrant Access → Qdrant: persists to · Semantic memory lives in the Qdrant service
- Data API → Data Tools: mutates · Applied transformations and history operations use dataset tooling
- Local Stack → API Entry: starts · Local startup launches the backend server
- Local Stack → Browser UI: starts · Local startup launches the frontend app
- Local Stack → Qdrant: starts · Local startup brings up the vector database
