如何使用 Foundation Models

Asked on 06/13/2025

1 search

To use Foundation Models, you can leverage the Foundation Models Framework, which provides access to an on-device large language model through a convenient Swift API. This framework is available on macOS, iPadOS, iOS, and VisionOS, and it allows you to enhance your apps with intelligent features while keeping data private and running offline.

Here are some key steps and features for using Foundation Models:

  1. Import the Framework: Start by importing the Foundation Models framework into your project.

  2. Create a Session: Establish a session to interact with the model. This session will manage the state and history of interactions.

  3. Prompt the Model: Send prompts to the model to generate responses. You can use simple text prompts or more structured prompts using the Generable and Guide macros to get structured output.

  4. Tool Calling: Define tools that the model can call autonomously to perform tasks or fetch additional information. This is useful for integrating external data or actions into the model's processing.

  5. Guided Generation: Use guided generation to produce structured data that can be directly used in your app. This involves defining types and constraints that the model should adhere to when generating output.

  6. Safety and Trust: Implement safety measures and guardrails to ensure the model generates appropriate content and handles user input responsibly.

For a detailed exploration of these features, you can refer to the following sessions from WWDC 2025:

These sessions provide comprehensive insights into using the Foundation Models framework effectively in your applications.

如何使用 Foundation Models | Ask WWDC