# k3maal/localai architecture

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

- Interactive diagram: https://gitdiagram.com/k3maal/localai
- Repository: https://github.com/k3maal/localai

## Overview

MyAi is a private, terminal-based chat assistant backed by an ASP.NET Core API, SQLite, and local Ollama. The main workflow is: the user sends a message through the CLI; the API saves it, loads the latest 20 messages, asks Ollama for a response, saves that response, and returns it to the CLI. The CLI also supports creating, listing, reopening, continuing, and deleting chats, and selecting a model. Edges reflect the sampled code and README; unsampled internals are not inferred.

Main entry points: [`Api/Program.cs`](https://github.com/k3maal/localai/blob/main/Api/Program.cs) (Chat API routes), [`Cli/Program.cs`](https://github.com/k3maal/localai/blob/main/Cli/Program.cs) (Chat menu), [`Api/ChatDbContext.cs`](https://github.com/k3maal/localai/blob/main/Api/ChatDbContext.cs) (EF database context), [`Shared/ChatModel.cs`](https://github.com/k3maal/localai/blob/main/Shared/ChatModel.cs) (Shared chat contracts)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["Terminal client"]
  node_cli_menu["Chat menu<br/>[Program.cs]"]
  node_cli_conversation["Conversation view<br/>[Program.cs]"]
  node_cli_model["Model selection<br/>[Program.cs]"]
end

subgraph group_api["Chat API"]
  node_api_routes["Chat API routes<br/>[Program.cs]"]
  node_contracts["Shared chat contracts<br/>[ChatModel.cs]"]
end

subgraph group_storage["Chat storage"]
  node_db_context["EF database context<br/>[ChatDbContext.cs]"]
  node_chat_entity["Chat record<br/>[Chat.cs]"]
  node_message_entity["Message record<br/>[ChatMessage.cs]"]
  node_sqlite[("SQLite database")]
end

subgraph group_ai["Local inference"]
  node_ollama["Ollama service"]
  node_model["Local AI model"]
end

node_user(("Terminal user"))

node_user -->|"chooses actions"| node_cli_menu
node_user -->|"enters messages"| node_cli_conversation
node_cli_menu -->|"manages chats"| node_api_routes
node_cli_conversation -->|"sends messages"| node_api_routes
node_cli_model -->|"lists models"| node_api_routes
node_cli_menu -->|"uses chat summaries"| node_contracts
node_cli_conversation -->|"uses message DTOs"| node_contracts
node_api_routes -->|"returns chat DTOs"| node_contracts
node_api_routes -->|"queries and saves"| node_db_context
node_db_context -->|"stores chats"| node_chat_entity
node_db_context -->|"stores messages"| node_message_entity
node_api_routes -->|"persists chat data"| node_sqlite
node_api_routes -->|"requests responses"| node_ollama
node_ollama -->|"runs inference"| node_model
node_ollama -->|"returns answer"| node_api_routes

click node_cli_menu "https://github.com/k3maal/localai/blob/main/Cli/Program.cs"
click node_cli_conversation "https://github.com/k3maal/localai/blob/main/Cli/Program.cs"
click node_cli_model "https://github.com/k3maal/localai/blob/main/Cli/Program.cs"
click node_api_routes "https://github.com/k3maal/localai/blob/main/Api/Program.cs"
click node_contracts "https://github.com/k3maal/localai/blob/main/Shared/ChatModel.cs"
click node_db_context "https://github.com/k3maal/localai/blob/main/Api/ChatDbContext.cs"
click node_chat_entity "https://github.com/k3maal/localai/blob/main/Api/Chat.cs"
click node_message_entity "https://github.com/k3maal/localai/blob/main/Api/ChatMessage.cs"

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_cli_menu,node_cli_conversation,node_cli_model,node_user toneBlue
class node_api_routes,node_contracts toneAmber
class node_db_context,node_chat_entity,node_message_entity,node_sqlite toneMint
class node_ollama,node_model toneRose
```

## Components

### Terminal client

- [Chat menu](https://github.com/k3maal/localai/blob/main/Cli/Program.cs): `Cli/Program.cs`
- [Conversation view](https://github.com/k3maal/localai/blob/main/Cli/Program.cs): `Cli/Program.cs`
- [Model selection](https://github.com/k3maal/localai/blob/main/Cli/Program.cs): `Cli/Program.cs`

### Chat API

- [Chat API routes](https://github.com/k3maal/localai/blob/main/Api/Program.cs): `Api/Program.cs`
- [Shared chat contracts](https://github.com/k3maal/localai/blob/main/Shared/ChatModel.cs): `Shared/ChatModel.cs`

### Chat storage

- [EF database context](https://github.com/k3maal/localai/blob/main/Api/ChatDbContext.cs): `Api/ChatDbContext.cs`
- [Chat record](https://github.com/k3maal/localai/blob/main/Api/Chat.cs): `Api/Chat.cs`
- [Message record](https://github.com/k3maal/localai/blob/main/Api/ChatMessage.cs): `Api/ChatMessage.cs`
- **SQLite database**

### Local inference

- **Ollama service**
- **Local AI model**

### Other components

- **Terminal user**

## Connections

- Terminal user → Chat menu: chooses actions (evidence: `Cli/Program.cs`)
- Terminal user → Conversation view: enters messages (evidence: `Cli/Program.cs`)
- Chat menu → Chat API routes: manages chats (evidence: `Cli/Program.cs`)
- Conversation view → Chat API routes: sends messages (evidence: `Cli/Program.cs`)
- Model selection → Chat API routes: lists models (evidence: `Cli/Program.cs`)
- Chat menu → Shared chat contracts: uses chat summaries (evidence: `Cli/Program.cs`)
- Conversation view → Shared chat contracts: uses message DTOs (evidence: `Cli/Program.cs`)
- Chat API routes → Shared chat contracts: returns chat DTOs (evidence: `Api/Program.cs`)
- Chat API routes → EF database context: queries and saves (evidence: `Api/Program.cs`)
- EF database context → Chat record: stores chats (evidence: `Api/ChatDbContext.cs`)
- EF database context → Message record: stores messages (evidence: `Api/ChatDbContext.cs`)
- Chat API routes → SQLite database: persists chat data (evidence: `Api/Program.cs`)
- Chat API routes → Ollama service: requests responses (evidence: `Api/Program.cs`)
- Ollama service → Local AI model: runs inference (evidence: `README.md`)
- Ollama service → Chat API routes: returns answer (evidence: `README.md`)
