Give me an example of how to use foundation models from an app intent

Asked on 06/13/2025

1 search

To use foundation models from an app intent, you can leverage the Foundation Models framework introduced by Apple. Here's a step-by-step example based on the session "Code-along: Bring on-device AI to your app using the Foundation Models framework":

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

  2. Create a Session: Use the framework to create a session. This session will be used to interact with the on-device large language model.

  3. Define Instructions: Use the Builder API to create instructions for the model. Instructions are a higher-level form of prompting where you define the model's job. For example, if you want the model to generate an itinerary, you can provide information about a selected landmark and include an example to guide the model's response.

  4. Use Generable Types: If you have structured data, you can define it using generable types. The framework will automatically convert these types to text that the model can understand.

  5. Make a Request: With your instructions and generable types set, you can make a request to the model. For instance, you can explicitly ask the model to generate an itinerary.

  6. Tool Calling: Optionally, you can use tool calling to let the model autonomously choose the best points of interest or fetch additional information from external sources.

For a detailed walkthrough, you can refer to the session Code-along: Bring on-device AI to your app using the Foundation Models framework (02:30).

This approach allows you to integrate powerful AI capabilities into your app while keeping user data private, as everything runs on-device.