apple-foundation-models
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseApple Foundation Models Skill
Apple Foundation Models 技能
When to Use This Skill
何时使用此技能
Use this skill when you need help with:
- On-device AI: Building iOS/macOS apps with on-device language models
- SystemLanguageModel: Working with Apple's on-device LLM API
- Guided Generation: Generating structured Swift types from prompts
- Tool Calling: Extending model capabilities with custom tools
- Prompting: Crafting effective prompts for on-device models
- Safety: Implementing guardrails and handling sensitive content
- Localization: Supporting multilingual AI features
This skill covers iOS 26.0+, iPadOS 26.0+, macOS 26.0+, and visionOS 26.0+.
当你需要以下方面的帮助时使用此技能:
- 设备端AI:构建搭载设备端语言模型的iOS/macOS应用
- SystemLanguageModel:使用Apple的设备端LLM API
- 引导生成:通过提示词生成结构化Swift类型
- 工具调用:通过自定义工具扩展模型能力
- 提示词工程:为设备端模型设计有效的提示词
- 安全机制:实现防护措施并处理敏感内容
- 本地化:支持多语言AI功能
此技能适用于iOS 26.0+、iPadOS 26.0+、macOS 26.0+和visionOS 26.0+。
Description
说明
Use this skill when working with Apple's Foundation Models framework for on-device AI and LLM capabilities in iOS/macOS apps. Covers SystemLanguageModel, LanguageModelSession, guided generation, tool calling, and prompting patterns.
当你在iOS/macOS应用中使用Apple的Foundation Models框架开发设备端AI和LLM功能时,可使用此技能。内容涵盖SystemLanguageModel、LanguageModelSession、引导生成、工具调用和提示词设计模式。
Quick Reference
快速参考
Core Components
核心组件
Advanced
高级功能
TranscriptTranscript.EntryTranscript.ResponseTranscript.ResponseFormatTranscript.SegmentTranscript.StructuredSegment
TranscriptTranscript.EntryTranscript.ResponseTranscript.ResponseFormatTranscript.SegmentTranscript.StructuredSegment
Api Reference
API参考
FoundationModelsGenerationIDSystemLanguageModelSystemLanguageModel.AdapterSystemLanguageModel.GuardrailsSystemLanguageModel.UseCase
FoundationModelsGenerationIDSystemLanguageModelSystemLanguageModel.AdapterSystemLanguageModel.GuardrailsSystemLanguageModel.UseCase
Getting Started
入门指南
SystemLanguageModel.Availability
SystemLanguageModel.Availability
Guided Generation
引导生成
DynamicGenerationSchemaGenerableGenerationGuideGenerationSchemaGuide
DynamicGenerationSchemaGenerableGenerationGuideGenerationSchemaGuide
Localization
本地化
LanguageModelFeedback
LanguageModelFeedback
Prompting
提示词工程
InstructionsInstructionsBuilderInstructionsRepresentableLanguageModelSessionLanguageModelSession.GenerationErrorLanguageModelSession.ToolCallErrorPromptPromptBuilderPromptRepresentableTranscript.InstructionsTranscript.Prompt
InstructionsInstructionsBuilderInstructionsRepresentableLanguageModelSessionLanguageModelSession.GenerationErrorLanguageModelSession.ToolCallErrorPromptPromptBuilderPromptRepresentableTranscript.InstructionsTranscript.Prompt
Tool Calling
工具调用
Tool
Tool
Key Concepts
核心概念
Platform Support
平台支持
- iOS 26.0+
- iPadOS 26.0+
- macOS 26.0+
- Mac Catalyst 26.0+
- visionOS 26.0+
- iOS 26.0+
- iPadOS 26.0+
- macOS 26.0+
- Mac Catalyst 26.0+
- visionOS 26.0+
On-Device AI
设备端AI
All models run entirely on-device, ensuring privacy and offline capability.
所有模型完全在设备端运行,确保隐私性和离线使用能力。
Usage Guidelines
使用指南
- Check model availability before use
- Define clear instructions for the model's behavior
- Use guided generation for structured outputs
- Implement tool calling for dynamic capabilities
- Handle errors appropriately
- 使用前检查模型可用性
- 为模型行为定义清晰的指令
- 使用引导生成获取结构化输出
- 实现工具调用以获得动态能力
- 妥善处理错误
Navigation
导航
See the directory for detailed API documentation organized by category:
references/- - Advanced
references/advanced.md - - Api Reference
references/api_reference.md - - Getting Started
references/getting_started.md - - Guided Generation
references/guided_generation.md - - Localization
references/localization.md - - Prompting
references/prompting.md - - Tool Calling
references/tool_calling.md
查看目录下按类别组织的详细API文档:
references/- - 高级功能
references/advanced.md - - API参考
references/api_reference.md - - 入门指南
references/getting_started.md - - 引导生成
references/guided_generation.md - - 本地化
references/localization.md - - 提示词工程
references/prompting.md - - 工具调用
references/tool_calling.md
Best Practices
最佳实践
- Prompting: Be specific and clear in your prompts
- Instructions: Define the model's behavior upfront
- Safety: Enable guardrails for sensitive content
- Localization: Check supported languages for your use case
- Performance: Use prewarm() for better response times
- Streaming: Use streamResponse() for real-time user feedback
- 提示词工程:提示词要具体清晰
- 指令定义:预先明确模型的行为
- 安全机制:为敏感内容启用防护措施
- 本地化:检查你的用例支持的语言
- 性能优化:使用prewarm()提升响应速度
- 流式输出:使用streamResponse()获取实时用户反馈
Common Patterns
常见模式
Basic Session
基础会话
swift
let model = SystemLanguageModel(useCase: .general)
let session = LanguageModelSession(model: model)
let response = try await session.respond(to: Prompt("Your question"))swift
let model = SystemLanguageModel(useCase: .general)
let session = LanguageModelSession(model: model)
let response = try await session.respond(to: Prompt("Your question"))Guided Generation
引导生成
swift
struct Recipe: Generable {
let title: String
let ingredients: [String]
}
let recipe = try await session.respond(
generating: Recipe.self,
prompt: Prompt("Create a pasta recipe")
)swift
struct Recipe: Generable {
let title: String
let ingredients: [String]
}
let recipe = try await session.respond(
generating: Recipe.self,
prompt: Prompt("Create a pasta recipe")
)Tool Calling
工具调用
swift
struct WeatherTool: Tool {
func call(arguments: String) async throws -> String {
// Fetch weather data
}
}
let session = LanguageModelSession(
model: model,
tools: [WeatherTool()]
)swift
struct WeatherTool: Tool {
func call(arguments: String) async throws -> String {
// Fetch weather data
}
}
let session = LanguageModelSession(
model: model,
tools: [WeatherTool()]
)Reference Documentation
参考文档
For complete API details, see the categorized documentation in the directory.
references/如需完整的API详情,请查看目录下的分类文档。
references/