|
| 1 | +# AI-Powered Template Generation (RAG) |
| 2 | + |
| 3 | +Generate OS image templates from natural language descriptions using |
| 4 | +Retrieval-Augmented Generation (RAG). The AI feature searches existing |
| 5 | +templates semantically, then uses an LLM to generate a new template grounded |
| 6 | +in real, working examples. |
| 7 | + |
| 8 | +> **Phase 1** - This guide covers the current implementation: core RAG |
| 9 | +> with basic CLI (semantic search, template generation, embedding cache). |
| 10 | +> See the [ADR](../architecture/adr-template-enriched-rag.md) for the full |
| 11 | +> roadmap (query classification, conversational refinement, agentic |
| 12 | +> validation). |
| 13 | +
|
| 14 | +## Table of Contents |
| 15 | + |
| 16 | +- [AI-Powered Template Generation (RAG)](#ai-powered-template-generation-rag) |
| 17 | + - [Table of Contents](#table-of-contents) |
| 18 | + - [Prerequisites](#prerequisites) |
| 19 | + - [Install Ollama (recommended)](#install-ollama-recommended) |
| 20 | + - [Quick Start (Ollama - local, free)](#quick-start-ollama---local-free) |
| 21 | + - [Quick Start (OpenAI - cloud)](#quick-start-openai---cloud) |
| 22 | + - [CLI Reference](#cli-reference) |
| 23 | + - [Generate a Template](#generate-a-template) |
| 24 | + - [Search Only](#search-only) |
| 25 | + - [Save to File](#save-to-file) |
| 26 | + - [Cache Management](#cache-management) |
| 27 | + - [All Flags](#all-flags) |
| 28 | + - [Configuration](#configuration) |
| 29 | + - [Zero Configuration (Ollama)](#zero-configuration-ollama) |
| 30 | + - [Switching to OpenAI](#switching-to-openai) |
| 31 | + - [Full Configuration Reference](#full-configuration-reference) |
| 32 | + - [How It Works](#how-it-works) |
| 33 | + - [Enriching Templates with Metadata](#enriching-templates-with-metadata) |
| 34 | + - [Troubleshooting](#troubleshooting) |
| 35 | + - [Related Documentation](#related-documentation) |
| 36 | + |
| 37 | +--- |
| 38 | + |
| 39 | +## Prerequisites |
| 40 | + |
| 41 | +| Requirement | Details | |
| 42 | +|-------------|---------| |
| 43 | +| **os-image-composer** binary | Built via `earthly +build` or `go build ./cmd/os-image-composer` | |
| 44 | +| **AI provider** (one of) | [Ollama](https://ollama.com) (local) **or** an [OpenAI](https://platform.openai.com) API key | |
| 45 | +| **Template files** | At least a few `image-templates/*.yml` files to serve as the RAG knowledge base | |
| 46 | + |
| 47 | +### Install Ollama (recommended) |
| 48 | + |
| 49 | +Ollama runs models locally - no API keys, no cloud costs. |
| 50 | + |
| 51 | +```bash |
| 52 | +# Install Ollama (Linux) |
| 53 | +curl -fsSL https://ollama.com/install.sh | sh |
| 54 | + |
| 55 | +# Pull the required models |
| 56 | +ollama pull nomic-embed-text # embedding model (768 dimensions) |
| 57 | +ollama pull llama3.1:8b # default chat/generation model |
| 58 | + |
| 59 | +# Verify the server is running |
| 60 | +ollama list |
| 61 | +``` |
| 62 | + |
| 63 | +> **Tip:** Alternative embedding models are supported: |
| 64 | +> `mxbai-embed-large` (1024 dims) and `all-minilm` (384 dims). Change the |
| 65 | +> model in `os-image-composer.yml` if needed. |
| 66 | +
|
| 67 | +--- |
| 68 | + |
| 69 | +## Quick Start (Ollama - local, free) |
| 70 | + |
| 71 | +With Ollama running, no configuration is needed: |
| 72 | + |
| 73 | +```bash |
| 74 | +# 1. Make sure Ollama is serving (default http://localhost:11434) |
| 75 | +ollama serve & |
| 76 | + |
| 77 | +# 2. Generate a template from a natural language description |
| 78 | +./os-image-composer ai "create a minimal edge image for ubuntu with SSH" |
| 79 | + |
| 80 | +# 3. Search for relevant templates without generating |
| 81 | +./os-image-composer ai --search-only "cloud image with monitoring" |
| 82 | +``` |
| 83 | + |
| 84 | +## Quick Start (OpenAI - cloud) |
| 85 | + |
| 86 | +```bash |
| 87 | +# 1. Set your API key |
| 88 | +export OPENAI_API_KEY="sk-..." |
| 89 | + |
| 90 | +# 2. Generate using OpenAI |
| 91 | +./os-image-composer ai --provider openai "create a minimal elxr image for IoT" |
| 92 | +``` |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## CLI Reference |
| 97 | + |
| 98 | +``` |
| 99 | +os-image-composer ai [query] [flags] |
| 100 | +``` |
| 101 | + |
| 102 | +### Generate a Template |
| 103 | + |
| 104 | +```bash |
| 105 | +./os-image-composer ai "create a minimal edge image for elxr with docker support" |
| 106 | +``` |
| 107 | + |
| 108 | +The command will: |
| 109 | + |
| 110 | +1. Index all templates in `image-templates/` (with embedding cache) |
| 111 | +2. Perform semantic search to find the most relevant templates |
| 112 | +3. Show the top reference templates and their similarity scores |
| 113 | +4. Generate a new YAML template grounded in those examples |
| 114 | + |
| 115 | +### Search Only |
| 116 | + |
| 117 | +Find relevant templates without invoking the LLM: |
| 118 | + |
| 119 | +```bash |
| 120 | +./os-image-composer ai --search-only "cloud deployment with monitoring" |
| 121 | +``` |
| 122 | + |
| 123 | +Output shows each matching template with a score breakdown: |
| 124 | + |
| 125 | +``` |
| 126 | +Found 5 matching templates: |
| 127 | +
|
| 128 | +1. elxr-cloud-amd64.yml |
| 129 | + Score: 0.87 (semantic: 0.92, keyword: 0.75, package: 0.60) |
| 130 | + Description: Cloud-ready eLxr image for VM deployment |
| 131 | + Distribution: elxr12, Architecture: x86_64, Type: raw |
| 132 | +``` |
| 133 | + |
| 134 | +### Save to File |
| 135 | + |
| 136 | +```bash |
| 137 | +# Save to image-templates/my-custom-image.yml |
| 138 | +./os-image-composer ai "create a minimal edge image" --output my-custom-image |
| 139 | + |
| 140 | +# Save to a specific path |
| 141 | +./os-image-composer ai "create an edge image" --output /tmp/my-image.yml |
| 142 | +``` |
| 143 | + |
| 144 | +If the output filename matches one of the reference templates returned by |
| 145 | +the current search results, you will be prompted before overwriting. |
| 146 | + |
| 147 | +### Cache Management |
| 148 | + |
| 149 | +Embeddings are cached to avoid recomputation on each run. The cache |
| 150 | +automatically invalidates when a template's content changes (SHA256 hash). |
| 151 | + |
| 152 | +```bash |
| 153 | +# Show cache statistics (entries, size, model, dimensions) |
| 154 | +./os-image-composer ai --cache-stats |
| 155 | + |
| 156 | +# Clear the embedding cache (forces re-indexing on next run) |
| 157 | +./os-image-composer ai --clear-cache |
| 158 | +``` |
| 159 | + |
| 160 | +### All Flags |
| 161 | + |
| 162 | +| Flag | Default | Description | |
| 163 | +|------|---------|-------------| |
| 164 | +| `--provider` | `ollama` | AI provider: `ollama` or `openai` | |
| 165 | +| `--templates-dir` | `./image-templates` | Directory containing template YAML files | |
| 166 | +| `--search-only` | `false` | Only search, don't generate | |
| 167 | +| `--output` | _(none)_ | Save generated template (name or path) | |
| 168 | +| `--cache-stats` | `false` | Show cache statistics | |
| 169 | +| `--clear-cache` | `false` | Clear the embedding cache | |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +## Configuration |
| 174 | + |
| 175 | +### Zero Configuration (Ollama) |
| 176 | + |
| 177 | +If Ollama is running on `localhost:11434` with `nomic-embed-text` and |
| 178 | +`llama3.1:8b` pulled, everything works out of the box - no config file changes |
| 179 | +required. |
| 180 | + |
| 181 | +### Switching to OpenAI |
| 182 | + |
| 183 | +The AI command currently selects the provider via CLI flags, not |
| 184 | +`os-image-composer.yml`. |
| 185 | + |
| 186 | +Use `--provider openai` when running `os-image-composer ai`: |
| 187 | + |
| 188 | +```bash |
| 189 | +./os-image-composer ai --provider openai "minimal Ubuntu server image for cloud VMs" |
| 190 | +``` |
| 191 | + |
| 192 | +You also need an API key: |
| 193 | + |
| 194 | +```bash |
| 195 | +export OPENAI_API_KEY="sk-..." |
| 196 | +``` |
| 197 | + |
| 198 | +The config snippet below shows the global config file schema for reference: |
| 199 | + |
| 200 | +```yaml |
| 201 | +ai: |
| 202 | + provider: openai |
| 203 | +``` |
| 204 | +
|
| 205 | +### Full Configuration Reference |
| 206 | +
|
| 207 | +All settings are optional. Defaults are shown below - only override what you |
| 208 | +need to change in `os-image-composer.yml`: |
| 209 | + |
| 210 | +```yaml |
| 211 | +ai: |
| 212 | + provider: ollama # "ollama" or "openai" |
| 213 | + templates_dir: ./image-templates |
| 214 | +
|
| 215 | + ollama: |
| 216 | + base_url: http://localhost:11434 |
| 217 | + embedding_model: nomic-embed-text # 768 dims |
| 218 | + chat_model: llama3.1:8b |
| 219 | + timeout: "120s" # request timeout |
| 220 | +
|
| 221 | + openai: |
| 222 | + embedding_model: text-embedding-3-small |
| 223 | + chat_model: gpt-4o-mini |
| 224 | + timeout: "60s" # request timeout |
| 225 | +
|
| 226 | + cache: |
| 227 | + enabled: true |
| 228 | + dir: ./.ai-cache |
| 229 | +
|
| 230 | + # Advanced - rarely need to change |
| 231 | + scoring: |
| 232 | + semantic_weight: 0.70 # embedding similarity weight |
| 233 | + keyword_weight: 0.20 # keyword overlap weight |
| 234 | + package_weight: 0.10 # package name matching weight |
| 235 | +``` |
| 236 | + |
| 237 | +| Environment Variable | Description | |
| 238 | +|----------------------|-------------| |
| 239 | +| `OPENAI_API_KEY` | Required when `provider: openai` | |
| 240 | + |
| 241 | +--- |
| 242 | + |
| 243 | +## How It Works |
| 244 | + |
| 245 | +``` |
| 246 | +User query ──► Index templates ──► Semantic search ──► Build LLM context ──► Generate YAML |
| 247 | + │ │ |
| 248 | + ▼ ▼ |
| 249 | + Embedding cache Hybrid scoring |
| 250 | + (SHA256 hash) (semantic + keyword + package) |
| 251 | +``` |
| 252 | +
|
| 253 | +1. **Indexing** - On first run (or when templates change), each template in |
| 254 | + `image-templates/` is parsed and converted to a searchable text |
| 255 | + representation. An embedding vector is generated via the configured |
| 256 | + provider and cached locally. |
| 257 | +
|
| 258 | +2. **Search** - The user query is embedded and compared against all template |
| 259 | + vectors using cosine similarity. A hybrid score combines: |
| 260 | + - **Semantic similarity** (70%) - how closely the meaning matches |
| 261 | + - **Keyword overlap** (20%) - exact term matches |
| 262 | + - **Package matching** (10%) - package name overlap |
| 263 | +
|
| 264 | +3. **Generation** - The top-scoring templates are included as context for the |
| 265 | + LLM, which generates a new YAML template grounded in real, working |
| 266 | + examples. |
| 267 | +
|
| 268 | +--- |
| 269 | +
|
| 270 | +## Enriching Templates with Metadata |
| 271 | +
|
| 272 | +Templates work without metadata, but adding an optional `metadata` section |
| 273 | +improves search accuracy: |
| 274 | +
|
| 275 | +```yaml |
| 276 | +metadata: |
| 277 | + description: "Cloud-ready eLxr image for VM deployment on AWS, Azure, GCP" |
| 278 | + use_cases: |
| 279 | + - cloud-deployment |
| 280 | + keywords: |
| 281 | + - cloud |
| 282 | + - cloud-init |
| 283 | + - aws |
| 284 | + - azure |
| 285 | +
|
| 286 | +image: |
| 287 | + name: elxr-cloud-amd64 |
| 288 | + # ... rest of template |
| 289 | +``` |
| 290 | + |
| 291 | +All metadata fields are optional. Templates without metadata are still |
| 292 | +indexed using their filename, distribution, architecture, image type, and |
| 293 | +package lists. |
| 294 | + |
| 295 | +--- |
| 296 | + |
| 297 | +## Troubleshooting |
| 298 | + |
| 299 | +| Symptom | Cause | Fix | |
| 300 | +|---------|-------|-----| |
| 301 | +| `failed to create AI engine` | Ollama not running | Run `ollama serve` | |
| 302 | +| `connection refused :11434` | Ollama server down | Start Ollama: `ollama serve` | |
| 303 | +| Embeddings fail | Model not pulled | `ollama pull nomic-embed-text` | |
| 304 | +| Chat generation fails | Chat model not pulled | `ollama pull llama3.1:8b` | |
| 305 | +| Poor search results | Stale cache | `./os-image-composer ai --clear-cache` | |
| 306 | +| OpenAI auth error | Missing API key | `export OPENAI_API_KEY="sk-..."` | |
| 307 | +| Slow first run | Building embedding cache | Normal - subsequent runs use cache | |
| 308 | +| `No matching templates found` | Empty templates dir | Check `--templates-dir` points to templates | |
| 309 | + |
| 310 | +--- |
| 311 | + |
| 312 | +## Related Documentation |
| 313 | + |
| 314 | +- [ADR: Template-Enriched RAG](../architecture/adr-template-enriched-rag.md) - |
| 315 | + Full architecture decision record, design details, and roadmap |
| 316 | +- [Usage Guide](usage-guide.md) - General os-image-composer usage |
| 317 | +- [Image Templates](../architecture/os-image-composer-templates.md) - |
| 318 | + Template format specification |
| 319 | +- [CLI Specification](../architecture/os-image-composer-cli-specification.md) - |
| 320 | + Complete command reference |
0 commit comments