讓任意 LLM 變成 Foundation Models 的可替換模型
這場 session 面向模型供應商與進階 app 團隊:Apple 把 on-device model、Private Cloud Compute、Core AI、MLX 和第三方模型放進同一個 LanguageModel protocol,重點不只是「換模型」,而是如何正確實作 executor、streaming、state、認證與自訂能力。
Foundation Models 正從單一 Apple model API 變成 Swift 生態的 LLM adapter layer;模型供應商真正要交付的是可靠的 LanguageModelExecutor,而不是只包一個 HTTP client。
這支影片在拆四個 provider integration 問題
模型要以 Swift Package 形式被採用
Apple 建議用 Swift Package Manager 發佈,支援 iOS、macOS、visionOS、watchOS,並考慮 Foundation Models open source 後的 Linux server-side Swift 用途。
Model 輕、Executor 重
LanguageModel 宣告 capabilities 與 executor configuration;LanguageModelExecutor 才負責載入權重、管理連線、轉 transcript、串流 token 與處理錯誤。
Configuration 是 executor cache key
同一個 session 會用 configuration 查 executor store。若 integration 有 KV cache、persistent session 或 server connection,這個 lifecycle 決定能否避免重做昂貴工作。
Metadata、custom segments、server-side tools 是差異化點
Provider 可以在不離開 LanguageModelSession 的前提下暴露引用、效能資訊、新 modality 或工具過程,但要明確處理隱私與 developer 可理解性。
把一個模型接進 Foundation Models 的導入路徑
先設計 Swift package 邊界
把 public model conformance、runtime、tests 拆清楚;dependencies 會變成 app shipping bytes,不能把 provider SDK 的複雜度全部丟給 app。
實作 LanguageModel 與 executor configuration
capabilities 要誠實描述 tool calling、guided generation、reasoning 等能力;configuration 必須 Hashable,因為它控制 executor reuse。
在 executor 轉換 transcript 與 options
把 instructions、prompt、tool calls、tool output、response、reasoning 映射到模型原生 message role;同時讀取 context options 與 generation options。
用 streaming channel 回傳可觀測事件
先送 metadata 和 usage,再送 text delta、tool call delta、reasoning 或 custom segment。one-shot API 只是收集 stream,實作仍應以 streaming 為核心。
補齊認證、錯誤與隱私契約
不要只收字串 API key;提供 token provider 或 sign-in flow,用 Keychain 保存 token,並用內建 LanguageModelError 或清楚 custom error 告知限制。
重點 1:LanguageModel protocol 是替換模型的表層,executor 才是產品品質
影片一開始展示同一段 app code 可以換成 System Language Model、Private Cloud Compute、Core AI local model 或 MLX Hugging Face model。這個便利的背後,是所有模型都要遵守同一個 LanguageModel protocol。
對 app 開發者而言,這代表 model choice 不應散落在業務邏輯裡;對 provider 而言,真正要寫好的是 executor:載入成本、streaming latency、usage accounting、error semantics 與 state invalidation 都在這裡。
重點 2:狀態與 cache 的難點在 transcript divergence
executor 每次 respond 都會收到完整 transcript。若新的 transcript 只是 append prompt,就能保留既有 KV cache 或 server session,只處理新增內容;若開發者 trim 或修改舊 entry,executor 必須找出 divergence point,回退或重建狀態。
重點 3:認證 API shape 會塑造開發者是否犯錯
Apple 明確提醒:如果 initializer 只吃 API key string,開發者容易把密鑰硬塞進 app。比較好的 provider package 應提供 token provider 或 sign-in flow;若 package 替開發者取 token,應存入 Keychain。雲端模型還應評估 App Attest,避免 tampered builds 或濫用流量。
風險與遷移注意事項
對開發者的實作啟發
來源
主要來源為 Apple Developer 官方影片頁、transcript、chapters 與 code snippets;YouTube 影片頁用於 iframe 與發布 metadata。官方頁:Bring an LLM provider to the Foundation Models framework - WWDC26。YouTube:WWDC26: Bring an LLM provider to the Foundation Models framework | Apple。
- 01Foundation Models 的新方向是同一個 Swift API 接 on-device、PCC、Core AI、MLX 與第三方模型。
- 02Provider package 的品質取決於 executor lifecycle、transcript mapping、streaming event 與錯誤語意。
- 03認證、隱私與 custom segment 設計要從第一版就納入,否則很快會變成 app 端安全與 UX 負債。