|
| 1 | +--- |
| 2 | +name: foundry-local |
| 3 | +description: > |
| 4 | + Use this skill when deploying mobius-exported ONNX models to |
| 5 | + Microsoft Foundry Local for local inference. Covers custom model |
| 6 | + registration via the cache directory, inference_model.json format, |
| 7 | + CLI and API usage, SDK limitations, and GenAI version compatibility. |
| 8 | +--- |
| 9 | + |
| 10 | +# Skill: Deploying to Foundry Local |
| 11 | + |
| 12 | +## When to use |
| 13 | + |
| 14 | +Use this skill when: |
| 15 | +- Deploying a mobius-exported ONNX model to Foundry Local for local |
| 16 | + inference |
| 17 | +- Registering a custom model in the Foundry Local cache |
| 18 | +- Writing the `inference_model.json` descriptor for a custom model |
| 19 | +- Troubleshooting Foundry Local model loading or compatibility issues |
| 20 | +- Accessing the OpenAI-compatible API endpoint for local models |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +1. **Foundry Local SDK:** |
| 25 | + |
| 26 | + ```bash |
| 27 | + pip install foundry-local-sdk |
| 28 | + ``` |
| 29 | + |
| 30 | +2. **ONNX model exported by mobius** with `--runtime ort-genai`: |
| 31 | + |
| 32 | + ```bash |
| 33 | + mobius build --model <hf-model-id> --dtype f16 \ |
| 34 | + --ep default --runtime ort-genai \ |
| 35 | + --external-data safetensors --max-shard-size 5GB \ |
| 36 | + output/ |
| 37 | + ``` |
| 38 | + |
| 39 | + The output directory must contain `genai_config.json`, tokenizer |
| 40 | + files, and the model sub-directories (decoder/, embedding/, etc.). |
| 41 | + |
| 42 | +## Custom model registration |
| 43 | + |
| 44 | +Foundry Local discovers models from its cache directory. To deploy a |
| 45 | +custom model, place it in the cache with an `inference_model.json` |
| 46 | +descriptor. |
| 47 | + |
| 48 | +### Step 1: Find the cache directory |
| 49 | + |
| 50 | +```bash |
| 51 | +foundry cache cd |
| 52 | +``` |
| 53 | + |
| 54 | +This prints the cache path — typically `~/.foundry/cache/` on Linux |
| 55 | +or `%LOCALAPPDATA%\foundry\cache\` on Windows. |
| 56 | + |
| 57 | +### Step 2: Create the model directory |
| 58 | + |
| 59 | +```bash |
| 60 | +CACHE_DIR=$(foundry cache cd) |
| 61 | +MODEL_NAME="my-custom-model" |
| 62 | +mkdir -p "${CACHE_DIR}/models/Custom/${MODEL_NAME}" |
| 63 | +``` |
| 64 | + |
| 65 | +### Step 3: Copy model files |
| 66 | + |
| 67 | +Copy the entire mobius export output into the model directory: |
| 68 | + |
| 69 | +```bash |
| 70 | +# Copy all model files |
| 71 | +cp -r output/decoder/ "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" |
| 72 | +cp -r output/embedding/ "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" 2>/dev/null |
| 73 | +cp -r output/vision_encoder/ "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" 2>/dev/null |
| 74 | +cp -r output/audio_encoder/ "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" 2>/dev/null |
| 75 | +cp output/genai_config.json "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" |
| 76 | +cp output/tokenizer* "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" |
| 77 | +cp output/image_processor.json "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" 2>/dev/null |
| 78 | +cp output/audio_processor.json "${CACHE_DIR}/models/Custom/${MODEL_NAME}/" 2>/dev/null |
| 79 | +``` |
| 80 | + |
| 81 | +### Step 4: Create `inference_model.json` |
| 82 | + |
| 83 | +Create an `inference_model.json` file in the model directory: |
| 84 | + |
| 85 | +```bash |
| 86 | +cat > "${CACHE_DIR}/models/Custom/${MODEL_NAME}/inference_model.json" << 'EOF' |
| 87 | +{ |
| 88 | + "Name": "my-custom-model", |
| 89 | + "PromptTemplate": { |
| 90 | + "system": "<|im_start|>system\n{Content}<|im_end|>\n", |
| 91 | + "user": "<|im_start|>user\n{Content}<|im_end|>\n", |
| 92 | + "assistant": "<|im_start|>assistant\n{Content}<|im_end|>\n", |
| 93 | + "prompt": "{Messages}<|im_start|>assistant\n" |
| 94 | + } |
| 95 | +} |
| 96 | +EOF |
| 97 | +``` |
| 98 | + |
| 99 | +### Step 5: Verify registration |
| 100 | + |
| 101 | +```bash |
| 102 | +foundry cache ls |
| 103 | +``` |
| 104 | + |
| 105 | +Your model should appear in the list. |
| 106 | + |
| 107 | +## `inference_model.json` format |
| 108 | + |
| 109 | +The `inference_model.json` descriptor tells Foundry Local how to |
| 110 | +identify and prompt the model: |
| 111 | + |
| 112 | +```json |
| 113 | +{ |
| 114 | + "Name": "<model-name>", |
| 115 | + "PromptTemplate": { |
| 116 | + "system": "<system-message-template>", |
| 117 | + "user": "<user-message-template>", |
| 118 | + "assistant": "<assistant-message-template>", |
| 119 | + "prompt": "<full-prompt-template>" |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Fields |
| 125 | + |
| 126 | +| Field | Description | |
| 127 | +|-------|-------------| |
| 128 | +| `Name` | Display name for the model. Used in CLI and API. | |
| 129 | +| `PromptTemplate.system` | Template for system messages. `{Content}` is replaced with the message text. | |
| 130 | +| `PromptTemplate.user` | Template for user messages. `{Content}` is replaced with the message text. | |
| 131 | +| `PromptTemplate.assistant` | Template for assistant messages. `{Content}` is replaced with the message text. | |
| 132 | +| `PromptTemplate.prompt` | Template for the full prompt. `{Messages}` is replaced with the concatenated formatted messages. | |
| 133 | + |
| 134 | +### Common prompt templates |
| 135 | + |
| 136 | +**ChatML (Qwen, many recent models):** |
| 137 | + |
| 138 | +```json |
| 139 | +{ |
| 140 | + "system": "<|im_start|>system\n{Content}<|im_end|>\n", |
| 141 | + "user": "<|im_start|>user\n{Content}<|im_end|>\n", |
| 142 | + "assistant": "<|im_start|>assistant\n{Content}<|im_end|>\n", |
| 143 | + "prompt": "{Messages}<|im_start|>assistant\n" |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +**Llama-style:** |
| 148 | + |
| 149 | +```json |
| 150 | +{ |
| 151 | + "system": "<|start_header_id|>system<|end_header_id|>\n\n{Content}<|eot_id|>", |
| 152 | + "user": "<|start_header_id|>user<|end_header_id|>\n\n{Content}<|eot_id|>", |
| 153 | + "assistant": "<|start_header_id|>assistant<|end_header_id|>\n\n{Content}<|eot_id|>", |
| 154 | + "prompt": "<|begin_of_text|>{Messages}<|start_header_id|>assistant<|end_header_id|>\n\n" |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +**Gemma-style:** |
| 159 | + |
| 160 | +```json |
| 161 | +{ |
| 162 | + "system": "", |
| 163 | + "user": "<start_of_turn>user\n{Content}<end_of_turn>\n", |
| 164 | + "assistant": "<start_of_turn>model\n{Content}<end_of_turn>\n", |
| 165 | + "prompt": "{Messages}<start_of_turn>model\n" |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +Match the template to your model's chat format. Check the HuggingFace |
| 170 | +`tokenizer_config.json` for the model's `chat_template` to determine |
| 171 | +the correct format. |
| 172 | + |
| 173 | +## Running the model |
| 174 | + |
| 175 | +### CLI |
| 176 | + |
| 177 | +```bash |
| 178 | +foundry model run my-custom-model |
| 179 | +``` |
| 180 | + |
| 181 | +This starts a local inference server and opens an interactive chat. |
| 182 | + |
| 183 | +### API access |
| 184 | + |
| 185 | +Foundry Local exposes an **OpenAI-compatible API** endpoint. Once a |
| 186 | +model is running, you can access it via HTTP: |
| 187 | + |
| 188 | +```python |
| 189 | +import openai |
| 190 | + |
| 191 | +client = openai.OpenAI( |
| 192 | + base_url="http://localhost:5273/v1", |
| 193 | + api_key="foundry-local", # any non-empty string |
| 194 | +) |
| 195 | + |
| 196 | +response = client.chat.completions.create( |
| 197 | + model="my-custom-model", |
| 198 | + messages=[ |
| 199 | + {"role": "system", "content": "You are a helpful assistant."}, |
| 200 | + {"role": "user", "content": "Hello!"}, |
| 201 | + ], |
| 202 | + max_tokens=100, |
| 203 | +) |
| 204 | +print(response.choices[0].message.content) |
| 205 | +``` |
| 206 | + |
| 207 | +The port (default 5273) may vary — check the Foundry Local output |
| 208 | +when starting the server. |
| 209 | + |
| 210 | +### Python SDK |
| 211 | + |
| 212 | +```python |
| 213 | +from foundry_local import FoundryLocalManager |
| 214 | + |
| 215 | +manager = FoundryLocalManager() |
| 216 | +# List available models |
| 217 | +print(manager.list_models()) |
| 218 | +``` |
| 219 | + |
| 220 | +> **Limitation:** The `FoundryLocalManager` SDK may not discover custom |
| 221 | +> models placed in the cache directory. If your model doesn't appear, |
| 222 | +> use the CLI (`foundry model run`) or access the HTTP API directly. |
| 223 | +
|
| 224 | +## Known limitations |
| 225 | + |
| 226 | +### 1. Bundled GenAI version |
| 227 | + |
| 228 | +Foundry Local bundles its own version of `onnxruntime-genai`. Custom |
| 229 | +models must be compatible with the bundled GenAI version. If your model |
| 230 | +uses features from a newer GenAI version (e.g. new model_type support), |
| 231 | +it may not load. |
| 232 | + |
| 233 | +**Workaround:** Force-reinstall your target GenAI wheel after installing |
| 234 | +the Foundry Local SDK: |
| 235 | + |
| 236 | +```bash |
| 237 | +pip install foundry-local-sdk |
| 238 | +# Override with your specific GenAI build |
| 239 | +pip install onnxruntime-genai==<version> --force-reinstall --no-deps |
| 240 | +``` |
| 241 | + |
| 242 | +This replaces the bundled GenAI with your version. Use `--no-deps` to |
| 243 | +avoid re-installing Foundry's other dependencies. |
| 244 | + |
| 245 | +### 2. No CUDA EP in pip SDK version |
| 246 | + |
| 247 | +The pip-installed `foundry-local-sdk` does not include CUDA execution |
| 248 | +provider support. Models run on CPU by default. |
| 249 | + |
| 250 | +For GPU inference, use the standalone Foundry Local application (not |
| 251 | +the pip package) or set up ORT GenAI with CUDA EP directly. |
| 252 | + |
| 253 | +### 3. Multimodal models require GenAI 0.14+ |
| 254 | + |
| 255 | +Vision-language and audio-language models need `onnxruntime-genai` |
| 256 | +version 0.14 or later for multi-model pipeline support. Check your |
| 257 | +Foundry Local's bundled version: |
| 258 | + |
| 259 | +```python |
| 260 | +import onnxruntime_genai as og |
| 261 | +print(og.__version__) |
| 262 | +``` |
| 263 | + |
| 264 | +### 4. Custom model discovery |
| 265 | + |
| 266 | +The Python SDK's `FoundryLocalManager.list_models()` uses a catalog |
| 267 | +service that may not index custom models in the cache. Use the CLI |
| 268 | +or direct HTTP API as a reliable alternative. |
| 269 | + |
| 270 | +### 5. Model type compatibility |
| 271 | + |
| 272 | +Foundry Local uses ORT GenAI internally, which has a model_type |
| 273 | +whitelist (see the `ort-genai-config` skill). If your model uses an |
| 274 | +unregistered model_type, set `model_type: "decoder"` in |
| 275 | +`genai_config.json` as a workaround. |
| 276 | + |
| 277 | +## End-to-end example |
| 278 | + |
| 279 | +Complete workflow from HuggingFace model to local inference: |
| 280 | + |
| 281 | +```bash |
| 282 | +# 1. Export with mobius |
| 283 | +mobius build --model Qwen/Qwen2.5-7B-Instruct \ |
| 284 | + --dtype f16 --ep default --runtime ort-genai \ |
| 285 | + --external-data safetensors --max-shard-size 5GB \ |
| 286 | + qwen2.5-7b/ |
| 287 | + |
| 288 | +# 2. Register in Foundry Local cache |
| 289 | +CACHE_DIR=$(foundry cache cd) |
| 290 | +mkdir -p "${CACHE_DIR}/models/Custom/qwen2.5-7b" |
| 291 | +cp -r qwen2.5-7b/* "${CACHE_DIR}/models/Custom/qwen2.5-7b/" |
| 292 | + |
| 293 | +# 3. Create inference_model.json |
| 294 | +cat > "${CACHE_DIR}/models/Custom/qwen2.5-7b/inference_model.json" << 'EOF' |
| 295 | +{ |
| 296 | + "Name": "qwen2.5-7b", |
| 297 | + "PromptTemplate": { |
| 298 | + "system": "<|im_start|>system\n{Content}<|im_end|>\n", |
| 299 | + "user": "<|im_start|>user\n{Content}<|im_end|>\n", |
| 300 | + "assistant": "<|im_start|>assistant\n{Content}<|im_end|>\n", |
| 301 | + "prompt": "{Messages}<|im_start|>assistant\n" |
| 302 | + } |
| 303 | +} |
| 304 | +EOF |
| 305 | + |
| 306 | +# 4. Verify and run |
| 307 | +foundry cache ls |
| 308 | +foundry model run qwen2.5-7b |
| 309 | +``` |
| 310 | + |
| 311 | +## Reference |
| 312 | + |
| 313 | +- [Deploying Custom Models with Olive and Foundry Local](https://techcommunity.microsoft.com/blog/educatordeveloperblog/deploying-custom-models-with-microsoft-olive-and-foundry-local/4489002) |
| 314 | + |
| 315 | +## Cross-references |
| 316 | + |
| 317 | +- **Exporting models:** `.agents/skills/onnx-export-quantization/SKILL.md` |
| 318 | +- **ORT GenAI config:** `.agents/skills/ort-genai-config/SKILL.md` |
| 319 | +- **Quality checklist (L5):** `.agents/skills/quality-checklist/SKILL.md` |
0 commit comments