Cover Material, Copyright, and License
Preface
- Requests from the Author
- Parts of this Book are Specific for macOS and iOS, with Some Support for Linux
- Code for this Book
- Author’s Background
- Cover Art
- Swift 3rd Party Libraries
- Acknowledgements
Part 1: Getting Started with Swift for AI Development
Setting Up Swift for Command Line Development
- Installing Swift Packages
- Creating Swift Packages
- Accessing Libraries that You Write in Other Projects
Background Information for Writing Swift Command Line Utilities
- Using Shell Processes
- FileIO Examples
- Mono-Repo Umbrella Package
- Swift REPL
Web Scraping
- Running in the Swift REPL
Part 2: Large Language Models
Using the OpenAI LLM APIs
- Core Architecture
- Key Features
- Technical Implementation Details
- Running Tests
Using Ollama to Run Local LLMs
- Running the Ollama Service
- The OllamaService Actor Library
- Tool Definitions
- Example Tests
- Ollama Wrap Up
Running Local LLMs with Apple’s MLX Framework
- Background: MLX and Apple Silicon
- Choosing a Model
- Project Structure
- Package.swift
- main.swift — Full Walkthrough
- Running the Example
- Swapping Models
- Key Takeaways
- Summary
Using the AnyLanguageModel Package with OpenAI, Gemini, and Ollama
- OpenAI Example
- Google Gemini Example with Tool Use
- Ollama Local Example
- AnyLanguageModel Package Wrap Up
Part 3: Deep Learning, Natural Language Processing, and Retrieval-Augmented Generation
Deep Learning Introduction
- Simple Multi-layer Perceptron Neural Networks
- Deep Learning
Natural Language Processing Using Apple’s Natural Language Framework
- Using Apple’s NaturalLanguage Swift Library
- NLP Utility Library
- Running the Example
- Chapter Wrap Up
Document Question Answering Using Gemini APIs and a Local Embeddings Vector Database
- Gemini API Client
- Text Chunking
- In-Memory Vector Store
- Running the Example
- Chapter Wrap Up
Part 4: The Semantic Web, Knowledge Graphs, and Linked Data
Linked Data and the Semantic Web
- Understanding the Resource Description Framework (RDF)
- Frequently Used Resource Namespaces
- Understanding the SPARQL Query Language
- LLMs and the Semantic Web
- Semantic Web and Linked Data Wrap Up
Querying Knowledge Graphs with SPARQL and Swift
- What We Are Building
- Project Structure
- The Library: Full Source Listing
- Running SPARQL Queries
- The Test Suite
- Tips for Writing SPARQL Queries
- Wrap Up
Part 5: Apple Intelligence — On-Device LLMs with FoundationModels
Using Apple Intelligence’s Default System Model To Build a Chat Command Line Tool
Using Apple Intelligence to Build a Coding Assistant
- Requirements
- Project Layout
- Package.swift
- CodingCLI.swift — Full Listing
- Walking Through the Code
- Running the Tool
- Ideas for Extension
- Wrap Up
Part 6: Applied AI Projects
Knowledge Base Navigator: Building an AI-Powered Information System
- Project Overview
- Project Structure
- Core Implementation
- Running the Application
- Key Takeaways
- Environment Setup
Anomaly Detection
- Motivation
- The Gaussian Model
- Swift Implementation Overview
AnomalyDetectionClass- Preprocessing the Wisconsin Data
- Feature Histograms
- Running the Example
- Interpreting the Metrics
- Summary
AutoContext: Prepare Effective Prompts with Context for LLM Queries
- Project Structure
- Implementing Vectorization of Text and Semantic Similarity
- Core AutoContext Implementation
- The Interactive CLI (
main.swift) - Example Session
- Key Takeaways
- Wrap Up
