Apple Foundation Models

Oct 2 2025 · Swift 6.2, iOS 26, macOS 26, iPadOS 26, XCode 26

Lesson 02: Using Apple Foundation Models

Foundation Model Options & Tuning

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Foundation Model Options

You do have a few options to tune the output of Foundation Models. Open the Starter app for this project and look for the ConfigurationView.swift file. This view implements a new view that allows the user to add instructions, set a temperature for prompt responses and turn on greed mode. You will explore each of these options in this section.

Instructions

The first, and most important, of these options is the previously mentioned instructions. You can think of instructions as a super-prompt you give the model when creating a new LanguageModelSession. You will use the instructions to define the role and behavior of the model. Apple trained Foundation Models to obey instructions over any commands it receives in prompts. This makes it a critical place to provide guidance to the model on how you want prompts to be handled and to specify restrictions beyond those included by Apple. Good instructions:

session = LanguageModelSession(instructions: promptInstructions)
Solve the equation 8x - 4 = 2 step by step.
Use decimals instead of fractions when presenting the solutions to math problems.
Refuse to provide any assistance that solves equations step by step. Only provide the final answer without showing the intermediate steps.

Temperature

The second parameter to tune model responses is the temperature. Temperature influences the randomness of the model’s responses and can be set to nil or a value between zero and one, inclusive. The default nil allows the system to choose a reasonable default. The temperature adjusts the probability distribution of responses before sampling. In other words, it adjusts the randomness of the model’s responses. A value of one causes no adjustment. Lower values shift the probability, causing the model to select the more likely tokens more frequently, resulting in more predictable responses. Higher values should be thought of as increasing the deviation from statistically probable responses that form LLM responses. Note that changing this value will not affect the weaknesses of LLMs, such as hallucinations. You cannot eliminate hallucinations by lowering the temperature.

let temperature = customTemperature ? modelTemperature : nil
let options = GenerationOptions(temperature: temperature)
let stream = session.streamResponse(to: messageText, options: options)
Give me a one paragraph bedtime story.

Greedy Sampling

The inherent randomness in LLM output means that each answer remains distinct, even for the same prompt and setting. Apple Foundation Models allows you to specify how to sample values from the probability distribution. The most useful of these is applying the constant sampling .greedy. This value will always select the most likely token, providing consistency to the responses. To add this option to your app, find the section you just added when giving the app the ability to set the temperature and change the code to:

let temperature = customTemperature ? modelTemperature : nil
let samplingMode = useGreedy ? GenerationOptions.SamplingMode.greedy : nil
let options = GenerationOptions(sampling: samplingMode, temperature: temperature)
let stream = session.streamResponse(to: messageText, options: options)
Give me a one paragraph bedtime story.
See forum comments
Cinema mode Download course materials from Github
Previous: Abilities & Limitations of Apple Foundation Models Next: Handling Full Context Windows