How do I use foundation models?

Asked on 2025-06-30

1 search

To use Foundation Models, you can leverage the Foundation Models framework introduced by Apple. This framework provides access to an on-device large language model with a convenient Swift API, available on macOS, iPadOS, iOS, and VisionOS. Here are some key steps and features to consider:

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

  2. Create a Session: You need to create a session to interact with the model. This session can be customized with instructions to guide the model's behavior.

  3. Prompt the Model: You can send prompts to the model to generate text or structured data. The model is optimized for tasks like text extraction, summarization, and more.

  4. Use Generable and Guided Generation: For structured output, you can use the Generable macro to define types that the model can generate. Guided generation helps in producing structured data like JSON or CSV.

  5. Tool Calling: The framework supports tool calling, allowing the model to autonomously call functions you define, such as fetching data or performing actions within your app.

  6. Privacy and Efficiency: Since the model runs on-device, it ensures user data privacy and can operate offline. The framework is optimized for performance using techniques like quantization and speculative decoding.

For a deeper understanding, you can explore sessions like Deep dive into the Foundation Models framework and Meet the Foundation Models framework. These sessions provide comprehensive insights into using the framework effectively.

How do I use foundation models? | Ask WWDC