with the new on device llm how can i write a few lines of code for text generation
Asked on 06/11/2025
1 search
To write a few lines of code for text generation using the new on-device large language model (LLM) from Apple, you can use the Foundation Models framework. Here's a simple example of how you can get started:
-
Import the Framework: First, you need to import the Foundation Models framework into your project.
-
Create a Session: Set up a session with the language model. This session will handle the interaction with the model.
-
Send a Prompt: Use the session to send a text prompt to the model and receive a generated response.
Here's a basic example in Swift:
import FoundationModels
// Create a session with the language model
let session = LanguageModelSession()
// Define your prompt
let prompt = "Generate a title for a trip to Japan"
// Send the prompt to the model and get the response
session.respond(to: prompt) { response in
print("Generated text: \(response)")
}
This code snippet demonstrates how to set up a session and send a prompt to the on-device LLM to generate text. The model's response will be printed to the console.
For more detailed guidance, you can refer to the session Meet the Foundation Models framework (02:04) from WWDC 2025, which provides an introduction to using the Foundation Models framework for text generation.

Meet the Foundation Models framework
Learn how to tap into the on-device large language model behind Apple Intelligence! This high-level overview covers everything from guided generation for generating Swift data structures and streaming for responsive experiences, to tool calling for integrating data sources and sessions for context management. This session has no prerequisites.

Explore large language models on Apple silicon with MLX
Discover MLX LM – designed specifically to make working with large language models simple and efficient on Apple silicon. We’ll cover how to fine-tune and run inference on state-of-the-art large language models on your Mac, and how to seamlessly integrate them into Swift-based applications and projects.

Explore prompt design & safety for on-device foundation models
Design generative AI experiences that leverage the strengths of the Foundation Models framework. We’ll start by showing how to design prompts for the on-device large language model at the core of Apple Intelligence. Then, we’ll introduce key ideas around AI safety, and offer concrete strategies to make your generative AI features safe, reliable, and delightful.