how to use a foundation model

Asked on 06/18/2025

1 search

To use a foundation model with Apple's Foundation Models framework, you can follow these steps:

  1. Import the Framework: Start by importing the Foundation Models framework into your project. This gives you access to the on-device large language model.

  2. Create a Session: Use the framework to create a session. This session will allow you to interact with the model.

  3. Send a Prompt: You can send a prompt to the model using the session. The model is capable of tasks such as text extraction, summarization, and more. You can choose to display the answer as one response or incrementally using streaming output.

  4. Use Generable for Structured Output: If you need structured data, you can use the Generable macro to define types that the model can generate. This allows the model to produce structured responses that can be directly used in your app.

  5. Tool Calling: The framework supports tool calling, which allows the model to autonomously call functions you define in Swift. This can be used to fetch up-to-date content or perform actions within your app.

  6. Safety and Prompt Design: The framework includes guardrails for safety, and you can add your own safety rules. It's important to carefully design your prompts and instructions to ensure the model generates appropriate content.

For a deeper understanding, you might want to explore the session Deep dive into the Foundation Models framework (00:00:00) which covers sessions, generable, dynamic schemas, and tool calling in detail.

how to use a foundation model | Ask WWDC