Best practices while using foundation model framework

Asked on 2025-07-05

1 search

To effectively use the Foundation Models framework, here are some best practices highlighted during the WWDC sessions:

  1. Prompt Engineering:

    • Control the content length by specifying phrases like "in three sentences" for shorter outputs or "in detail" for longer ones.
    • Define the style and voice by specifying a role in your prompt. For example, you can instruct the model to generate content in a specific tone or style.
  2. Safety and Trust:

    • Ensure your app does not generate inappropriate content by leveraging the guardrails provided by the Foundation Models framework. These guardrails help block harmful inputs and outputs.
    • Handle user input with care and consider the potential impact of the model's responses on users.
  3. Guided Generation:

    • Use the Generable and Guide macros to generate structured outputs that can be directly used in your app. This helps in creating data structures without worrying about parsing the model's output.
  4. Stateful Sessions:

    • Utilize stateful sessions to maintain context across multiple interactions with the model. This can be particularly useful for applications that require multi-turn conversations.
  5. Tool Calling:

    • Implement tool calling to allow the model to autonomously execute code defined in your app, enhancing the model's capabilities.

For more detailed information on these practices, you can refer to the session Explore prompt design & safety for on-device foundation models (06:14) and Code-along: Bring on-device AI to your app using the Foundation Models framework (02:30).