iOS & Swift
New

Apple Foundation Models

Learn how to build intelligent SwiftUI applications using Apple's Foundation Models framework, guided generation, prompt engineering, and on-device AI technologies. By Bill Morefield.

Read for Free with the Personal Plan* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99* *Includes access to all of our online reading features.
Leave a rating/review
Download materials
Comments
Save for later
Share

Who is this for?

This book is for Swift developers who are familiar with writing SwiftUI apps and who want to build intelligent, AI-powered applications using Apple’s new Foundation Models framework and on-device machine learning technologies.

Covered concepts

  • Understanding LLMs
  • Streaming Model Responses
  • Managing Model Sessions
  • Prompt Engineering
  • AI Safety and Limitations
  • Guided Generation
  • Dynamic Schemas
  • Integrating External Tools
  • Building Real AI Apps

Apple Foundation Models is a practical guide to building intelligent, on-device AI applications using Apple’s new Foundation Models framework. Designed for Swift developers, this book introduces the fundamentals of large language models before guiding you through real-world implementations using Apple’s on-device AI technologies.

Throughout the book, you’ll learn how to...

more

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

Section I: Apple Foundation Models

This section provides a comprehensive introduction to Apple’s Foundation Models framework and its integration into SwiftUI applications. It begins with a practical overview of Large Language Models (LLMs), introducing the terminology, capabilities, and limitations that developers must understand before working with on-device intelligence. From there, it guides you through building conversational SwiftUI applications powered by Foundation Models, demonstrating how to interact with models in real time and create responsive, user-friendly experiences.

The section then explores the mechanics of model sessions, including transcript management, persistence, and strategies for handling limited context windows. It examines how developers can maintain meaningful conversations while working within the constraints of on-device models. Alongside these practical concerns, it presents an in-depth discussion of prompt engineering and safety, covering techniques for crafting reliable prompts while addressing challenges such as hallucinations, inconsistent output, and responsible AI usage.

Building on these foundations, the section introduces Guided Generation, one of the framework’s most powerful features for producing structured and strongly typed data. It demonstrates how to define schemas using Swift macros, enforce constraints on generated content, and dynamically construct data structures at runtime when compile-time definitions are unavailable. It further explores the integration of external tools and data sources, showing how models can be extended beyond their static training data to provide more contextual and useful responses.

Finally, the section brings these concepts together through the development of a practical on-device AI application. Using modern machine learning and Foundation Models, you will build an app capable of capturing audio, converting speech to text, summarizing content, and extracting meaningful information from user input. Each chapter provides both conceptual understanding and hands-on implementation, ensuring you develop a strong and practical foundation for building intelligent SwiftUI applications with Apple’s new Foundation Models framework.

This chapter introduces Apple Foundation Models through building a simple chat-style app that allows the user to interact with the model. You will also learn more about the nature of Foundation Models and large language models in general.
Toggle description
This chapter explores how to improve user experience by supporting streamed responses to prompts in Foundation Models. It also explores some of the limitations and compromises made in Foundation Models to fit it onto consumer devices. Finally, it discusses LanguageModelSession and tokens.
Toggle description
In this chapter, you will learn how Apple Foundation Models allows you to tune responses by specifying several properties. Instructions provide guides to model behavior, while temperature and sampling mode adjust the randomness of token selection. You'll also learn how to produce better prompts and the specific guidelines for instruction prompts.
This chapter will cover two important concepts when working with Foundation Models. You will look at strategies for managing the context window, and limitations and safety concerns when working with the model.
Toggle description
Guided generation is perhaps the most powerful feature of Apple Foundation models. It allows you to create data structures using the LLM that match predefined or dynamically defined data structures, without parsing and formatting string responses. This chapter will show you how to generate asynchronous and streamed structures using guided generation and how to define a dynamic structure at run time.
By default, Foundation Models can only access data from its training data. This chapter introduces Tools, which expand the information available to Foundation Models to external APIs and other on-device services.
In this chapter, you'll look at a simple, real-world application while learning how you can take a simple tool and use Foundation Models to enhance the user experience and make it a powerful app.
In this chapter, you will apply Foundation Models to the transcripts of voice notes to produce useful analysis and identify information contained in the notes. You will then look at ways to present the information to the user and make it searchable.