Getting Started with Apple's FoundationModels Framework
With iOS 26, Apple introduces on-device large language models that support various use cases, including tool calling. This post explores the FoundationModels framework. According to Apple's documentation, this model excels at text generation, understanding, summarization, and more.
Checking Apple Intelligence Availability
Before setting up and calling LanguageModelSession, you must ensure that Apple Intelligence is available and ready to use on the device. This is important because not all devices support Apple Intelligence capabilities, and users may not have enabled it.
You can check availability using SystemLanguageModel.default.availability and SystemLanguageModel.default.isAvailable:
switch SystemLanguageModel.default.availability {
case .available:
print("FoundationModels available to use")
case let .unavailable(reason):
switch reason {
case .deviceNotEligible:
print("FoundationModels not available on this device")
case .appleIntelligenceNotEnabled:
print("FoundationModels is not enabled on this device")
case .modelNotReady:
print("FoundationModels is not ready")
@unknown default:
print("Unknown reason for unavailability")
}
}
The default option in SystemLanguageModel represents the base model that handles text generation tasks. You can also configure SystemLanguageModel for specific use cases, such as contentTagging.
Generating Text with FoundationModels
To generate text based on user prompts, start by initializing a LanguageModelSession, then call the session.respond(to:) function.
A session represents a single context window that can be used to generate content and maintain state between requests.
do {
let session = LanguageModelSession()
let result = try await session.respond(to: "Write me a poem!")
print(result.content)
/**
Certainly! Here's a poem for you:
---
In the hush of dawn's gentle light,
Where shadows dance and dreams take flight,
A whisper stirs the waking air,
A promise woven with tender care.
The morning blooms in hues anew,
A tapestry of skies of blue,
Each petal kissed by morning dew,
In nature's arms, the world renews.
The river sings a timeless tune,
Beneath the sun's warm, golden noon,
Its waters weave a silent thread,
Of journeys past and futures spread.
The trees, like sentinels, stand tall,
Their leaves a symphony, soft and small,
They guard the secrets of the land,
In whispers shared by wind and sand.
Beneath the arch of endless sky,
We find the dreams that never die,
In every heart, a spark ignites,
A beacon guiding through the nights.
So let us cherish, hold, and dream,
In every dawn, in every beam,
For in the dance of day to night,
We find the beauty, pure and bright.
I hope you enjoyed it!
*/
} catch LanguageModelSession.GenerationError.refusal(let refusal, let context) {
print(refusal)
}
For best results when generating longer content, use multiple sessions. Split the content generation task into smaller chunks to maintain quality and coherence.
Using Instructions to Guide Model Behavior
While you can use any prompt to generate content with FoundationModels, it's more effective to use instructions to steer the role and behavior of the model. For example, if you want the model to generate poetry, you can pass instructions during LanguageModelSession initialization. This allows user prompts to be simple keywords or themes rather than detailed requests.
do {
let instructions = """
You are a poetic text generation model.
Given a single keyword or theme, generate an original poem that captures its essence through vivid imagery, emotional depth, and coherent structure.
"""
let session = LanguageModelSession(instructions: instructions)
let result = try await session.respond(to: "sea")
print(result.content)
/**
In the cradle of the whispering tide,
Where azure dreams and silver streams collide,
The sea unfurls its ancient, tender grace,
A timeless mirror to the sky's embrace.
Its depths hold secrets of forgotten lore,
In silent depths, where mysteries explore,
With every wave, a story softly spun,
Of distant lands and sun-drenched, golden run.
The salt-kissed breeze weaves through swaying reeds,
A lullaby of whispers that the ocean feeds,
While seagulls dance on wings of purest white,
In harmony with day and fading light.
O vast expanse of endless, rolling blue,
In your embrace, the world feels ever new,
A symphony of life, both fierce and mild,
In every crest and trough, a tale compiled.
So let us wander where the horizon lies,
Beneath your watchful, timeless, watchful eyes,
And find within your depths a quiet peace,
In the gentle lull of the eternal seas.
*/
} catch LanguageModelSession.GenerationError.refusal(let refusal, let context) {
print(refusal)
}
What's Next
The FoundationModels framework opens up exciting possibilities for integrating on-device AI into your iOS applications. With proper availability checking, session management, and thoughtful instructions, you can create powerful text generation features that respect user privacy by running entirely on-device. In the next part of this series, we'll explore more advanced features including tool calling, multi-turn conversations, and handling different content generation scenarios.