how to use foundationmodels?

Asked on 06/12/2025

1 search

To use the 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 with a convenient Swift API.

  2. Create a Session: The framework is built around the concept of a stateful session. You create a session to interact with the model. This session can be customized with instructions that guide the model's behavior.

  3. Send a Prompt: Once you have a session, you can send prompts to the model. The model processes these prompts and generates responses. You can use the session.respond method to send your prompt and receive a response.

  4. Use Generable for Structured Output: If you need structured output, you can use the Generable macro to define types that the model can generate. This allows you to get structured data directly from the model, which can be more reliable than parsing unstructured text.

  5. Tool Calling: The framework supports tool calling, which allows the model to autonomously call functions you define. This can be used to fetch additional information or perform actions based on the model's processing.

  6. Prompt Engineering: Crafting effective prompts is crucial. You can use Xcode's Playground to experiment with different prompts and refine them to get the desired output.

For a more detailed exploration, you can refer to the session Deep dive into the Foundation Models framework (00:00:00) and Code-along: Bring on-device AI to your app using the Foundation Models framework (00:01:09). These sessions provide comprehensive guidance on using the framework effectively.