AGENT SKILL

iOS 端侧 AI 模型开发

local-ai-models

用于通过 Foundation Models 或 MLX Swift 在设备端模型上构建 iOS 功能:本地 LLM 推理、聊天、视觉语言模型(VLMs)、文本嵌入、图像生成、工具调用、多轮对话、自定义模型或结构化生成;不适用于云端托管模型 API、Core ML 或 coremltools 模型转换、Vision 框架分类器;返回框架选择、兼容性门槛、会话与流式模式,以及设备实测验证计划。

适用职业AI 工程师
License 许可
MIT
所属仓库
mintuz/skills
最近更新
2026-11-13
Stars
★ 35
Forks
7

01 前言 Front Matter

name
local-ai-models
description
WHEN building iOS features on on-device models with Foundation Models or MLX Swift: local LLM inference, chat, Vision Language Models (VLMs), text embeddings, image generation, tool calling, multi-turn conversations, custom models, or structured generation; NOT for cloud-hosted model APIs, Core ML or coremltools model conversion, or Vision framework classifiers; returns framework selection, compatibility gates, session and streaming patterns, and device-proof verification plans.

02 资源文件 11

03 SKILL.md 正文

iOS On-Device AI Models

On-device AI in iOS apps with Apple's Foundation Models framework and MLX Swift.

Core Principles

  1. Compatibility Gate - Foundation Models requires iOS 26, iPadOS 26, or macOS 26 or later, on an Apple Intelligence-capable device with Apple Intelligence enabled. Before you select a framework, compare this floor with the deployment target and with every device the feature must support. A runtime check reports one device's state; it cannot make the feature available below the OS or device floor. At runtime, check SystemLanguageModel.default.availability before you create a session, and show a fallback UI for each unavailable state: .modelNotReady (for example, a download in progress), .appleIntelligenceNotEnabled (disabled in Settings), and .deviceNotEligible (hardware cannot run the model).
  2. Single-Flight Streaming - Give one isolation boundary ownership of each conversation's session, transcript, and generation task. While isResponding is true, reject or queue the new send; never call the session, so GenerationError.concurrentRequests never occurs in normal use. Stop and teardown cancel the owned task and await it before a new send starts. Check Task.checkCancellation() while consuming the stream. Commit a transcript turn only when the response completes (principle-separate-before-serializing-shared-state).
  3. Session Persistence - Reuse LanguageModelSession across completed turns; keep partial streaming text separate from committed history.
  4. Memory Awareness - Use quantised models and monitor memory. iOS limits one app to a fraction of the device's total RAM, so reject any model whose weight files approach or exceed the total RAM of the lowest-memory required device: it cannot load. Pick the smallest model that meets quality on that device and measure peak memory there. Give one @Observable owner the load state, the loaded model, and generation. For a multi-gigabyte download: ask for consent, default to Wi-Fi, make it resumable, and keep the feature in an explicit "not downloaded" state until the files are complete.
  5. Async Everything - Load models asynchronously; never block the main thread.
  6. Device Proof - Before calling the design viable, exercise support boundaries and generation lifecycle in focused tests, then verify a Release build on the oldest or lowest-memory required physical device, including offline operation when the product promises on-device behaviour (principle-prove-it-works).
  7. Locale Support - Before you create a session, call supportsLocale(_:) for each user locale. Treat an unsupported locale as an unavailable state; never work around it with a prompt instruction such as "answer in <language>". For a supported locale, put the locale in the session instructions, not in each prompt. Test each locale branch on a physical device set to that locale.
  8. Typed Tools and Outputs - For structured output, define a @Generable type and call respond(to:generating:); never parse free text with regular expressions. For an app action the model needs, implement the Foundation Models Tool protocol with @Generable arguments and pass the tool when you create the session. The session invokes the tool; the view never calls the service on the model's behalf. A tool error surfaces from the session call as LanguageModelSession.ToolCallError: catch it and show the user a normal failure state, never a crash or an empty result.

Quick Reference

Framework Comparison

Topic Guide
Framework comparison and selection framework-selection.md

Foundation Models (Apple's Framework)

Topic Guide
Setup and configuration foundation-models/setup.md
Chat patterns and conversations foundation-models/chat-patterns.md

MLX Swift (Advanced Features)

Topic Guide
Setup and configuration mlx-swift/setup.md
Chat patterns with custom models mlx-swift/chat-patterns.md
Vision Language Models (VLMs) mlx-swift/vision-patterns.md
Tool calling, embeddings, structured gen mlx-swift/advanced-patterns.md
Model quantization with MLX-LM mlx-swift/quantization.md

Shared (Both Frameworks)

Topic Guide
Best practices and optimization shared/best-practices.md
Error handling and recovery shared/error-handling.md
Testing strategies shared/testing.md

Quick Decision Trees

Which framework should I use?

Is Foundation Models available on every OS version and device the feature
must support (iOS 26 or later, Apple Intelligence-capable hardware)?
├── No → Can a suitable MLX model meet the same device floor?
│   ├── Yes → MLX Swift (prove memory, latency, and output on that floor)
│   └── No → The requirements are infeasible; change the support contract
└── Yes → Do you need VLMs, image generation, or custom models?
    ├── Yes → MLX Swift (references/mlx-swift/)
    └── No → Foundation Models (references/foundation-models/)

Where should I start?

New to on-device AI?
└── Start with Foundation Models:
    1. Read framework-selection.md
    2. Follow foundation-models/setup.md
    3. Implement foundation-models/chat-patterns.md

Need advanced features?
└── Use MLX Swift:
    1. Read framework-selection.md
    2. Follow mlx-swift/setup.md
    3. Choose pattern:
       - Chat: mlx-swift/chat-patterns.md
       - Vision: mlx-swift/vision-patterns.md
       - Advanced: mlx-swift/advanced-patterns.md

Where should my model loading code live?

Is this model shared across features?
├── Yes → Create @Observable service in app/services/
└── No → Is it feature-specific?
    ├── Yes → Create @Observable class in feature/
    └── No → Load inline with @State (simple cases only)

How should I handle conversations?

Foundation Models:
└── Reuse LanguageModelSession for context
    (references/foundation-models/chat-patterns.md #multi-turn)

MLX Swift:
└── Implement custom context management
    (references/mlx-swift/chat-patterns.md)

What generation parameters should I use?

What's the use case?

Factual answers (summaries, facts)
└── temperature: 0.1-0.3

Balanced (chat, Q&A)
└── temperature: 0.6-0.8

Creative (storytelling, ideas)
└── temperature: 0.9-1.2

See references/shared/best-practices.md for details

Resources

04 同仓库更多 Skills

mintuz
mintuz/skills

lens-review

USE WHEN reviewing uncommitted work, a branch diff or a pull request through several installed skills, each run as its own review lens; NOT WHEN you need one cold second opinion (double-check), a verdict against a specification (acceptance-review) or the built-in fixed-lens /code-review. Runs each lens in an isolated read-only agent, has a separate agent try to refute every finding, and returns one severity-ranked report.

★ 35
mintuz
mintuz/skills

XState 状态机建模

xstate

当 React 状态因外部响应而变化——异步提交、进行中或加载标志、重试、取消、轮询、乐观更新、过期响应——或因与可中断的过程(如拖拽或手势)交互而变化时使用;或在构建、审查、测试或可视化 XState 5 actors 和 @xstate/react 集成时使用;不适用于单个事件即可完全设定的值,如文本草稿、焦点标志或简单的开关切换;建模类型化生命周期并渲染合法的 Mermaid stateDiagram-v2 图表。

软件开发工程师前端开发与界面实现
★ 35
mintuz
mintuz/skills

网页界面设计优化

web-design

在优化 UI 布局、排版、色彩或细节打磨时使用;不涉及代码实现;提供简洁的原则,以实现有意图、易读的设计。

界面设计师用户体验设计
★ 35
mintuz
mintuz/skills

Tailwind CSS 样式开发

tailwind

在编写、修复或审查使用 Tailwind CSS 的项目中的样式时使用,即便只是改一个类名,或请求中未提及 Tailwind:颜色、焦点环、间距、由 prop、map 或 CVA 变体选择的类名、Tailwind 样式表或 tailwind.config 中的主题令牌与深色模式、配置或升级(如不确定项目是否使用 Tailwind,先检查 package.json 或主样式表);不适用于在没有 Tailwind 的项目中编写纯 CSS 或 Sass;产出的类名能被 Tailwind 识别,语法适配已安装的主版本,具有可见的焦点指示器,且不引入未要求的依赖或杜撰的品牌值。

软件开发工程师前端开发与界面实现
★ 35
mintuz
mintuz/skills

代码重构规划

refactoring

当你需要制定计划来重构、重组或清理正常工作的代码,且测试结果已知(全绿或已记录的失败集合)时使用;不适用于新功能交付或任何行为变更;以可逆的基线安全性和按优先级排序的改进为核心。

软件开发工程师设计评审与架构优化
★ 35
mintuz
mintuz/skills

react

USE WHEN changing the logic, data, props, state, Effects or rendered text of a React component, hook or page, even a one-line change; NOT WHEN the request is styling only (class names, Tailwind, CSS, dark mode, spacing, shadows) or tests only, or when it models submit, in-flight, loading, retry or failure states, even for a named React form (xstate covers that, including its React wiring); applies React 19 composition, state, Effect, structure, API and performance rules with only the project's installed libraries.

★ 35