Skip to content

rudrankriyam/Foundation-Models-Framework-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foundation Models Framework Example

One-shot prompt interface showing a haiku generation example with prompt input, reset/run buttons, suggestions, and resulting haiku about destiny
One-shot Prompting
Foundation Models examples page displaying six categories: One-shot, Business Ideas, Creative Writing, Structured Data, Streaming Response, Generation Guides, Model Availability, and Generation Options
Foundation Models
Tools page showing various utility options including Weather, Web Search, Contacts, Calendar, Reminders, Location, Health, Music, and Web Metadata tools
Tools
Chat interface displaying a conversation about the meaning of life, with user messages on the right and AI responses on the left, including a detailed philosophical answer
Chat Interface

Note:- For folks starring it, please do not judge the codebase. I have to do some refactoring that I vibe-coded to ship this, fast. Okay?

A practical iOS app demonstrating Apple's Foundation Models framework with various examples of on-device AI capabilities.

Exploring AI for iOS Development

Love this project? Check out my books to explore more of AI and iOS development:

Requirements

  • iOS 26.0+ or macOS 26.0+ (Xcode 26.0+)
  • Beta 5 version of the framework is required
  • Apple Intelligence enabled
  • Compatible Apple device with Apple Silicon

Try it on TestFlight

You can now try Foundation Lab on TestFlight! Join the beta: https://testflight.apple.com/join/JWR9FpP3

Features

Core AI Capabilities

  • Basic Chat: Simple conversational interactions
  • Structured Data Generation: Type-safe data generation with @Generable
  • Generation Guides: Constrained outputs with @Guide annotations
  • Streaming Responses: Real-time response streaming
  • Tool Calling: Custom tools for extended functionality
  • Model Availability: System capability checking

Creative Features

  • Creative Writing: Story outline and narrative generation
  • Business Ideas: Startup concept and business plan generation

Custom Tools

  • Weather Tool: Multi-city weather information with simulated data
  • Web Search Tool: Real-time web search using Exa AI API

Usage Examples

Basic Chat

let session = LanguageModelSession()
let response = try await session.respond(
    to: "Suggest a catchy name for a new coffee shop."
)
print(response.content)

Structured Data Generation

let session = LanguageModelSession()
let bookInfo = try await session.respond(
    to: "Suggest a sci-fi book.",
    generating: BookRecommendation.self
)
print("Title: \(bookInfo.content.title)")
print("Author: \(bookInfo.content.author)")

Tool Calling

// Weather Tool
let weatherSession = LanguageModelSession(tools: [WeatherTool()])
let weatherResponse = try await weatherSession.respond(
    to: "Is it hotter in New Delhi or Cupertino?"
)
print(weatherResponse.content)

// Web Search Tool
let webSession = LanguageModelSession(tools: [WebTool()])
let webResponse = try await webSession.respond(
    to: "Search for the latest WWDC 2025 announcements"
)
print(webResponse.content)

// Multiple Tools Example
let multiSession = LanguageModelSession(tools: [
    WeatherTool(),
    WebTool()
])
let multiResponse = try await multiSession.respond(
    to: "Check the weather in Tokyo and search for tourist attractions there"
)
print(multiResponse.content)

// Web Metadata Tool Example
let metadataSession = LanguageModelSession(tools: [WebMetadataTool()])
let metadataResponse = try await metadataSession.respond(
    to: "Generate a Twitter post for https://www.apple.com/newsroom/"
)
print(metadataResponse.content)

Streaming Responses

let session = LanguageModelSession()
let stream = session.streamResponse(to: "Write a short poem about technology.")

for try await partialText in stream {
    print("Partial: \(partialText)")
}

Data Models

The app includes comprehensive @Generable data models:

Book Recommendations

@Generable
struct BookRecommendation {
    @Guide(description: "The title of the book")
    let title: String
    
    @Guide(description: "The author's name")
    let author: String
    
    @Guide(description: "Genre of the book")
    let genre: Genre
}

Product Reviews

@Generable
struct ProductReview {
    @Guide(description: "Product name")
    let productName: String
    
    @Guide(description: "Rating from 1 to 5")
    let rating: Int
    
    @Guide(description: "Key pros of the product")
    let pros: [String]
}

Custom Tools

Weather Tool

Provides real-time weather information using OpenMeteo API:

  • Fetches current weather for any city worldwide
  • Temperature, humidity, wind speed, and weather conditions
  • Automatic geocoding for city names
  • No API key required

Web Search Tool

Real-time web search by Exa AI:

  • Returns text content from web pages
  • Configurable number of results (default: 5)
  • Supports complex search queries and current events

Setup Requirements:

  1. Get an API key from Exa AI
  2. Add your API key in the app's Settings screen
  3. The tool will automatically use the stored API key for searches

Timer Tool

Time-based operations and calculations:

  • Get current time in any timezone
  • Calculate time differences between dates
  • Format durations in human-readable format
  • No external dependencies or API keys required

Math Tool

Mathematical calculations and conversions:

  • Basic arithmetic operations (add, subtract, multiply, divide, power, sqrt)
  • Statistical calculations (mean, median, standard deviation)
  • Unit conversions (temperature, length, weight)
  • Works entirely offline

Text Tool

Text manipulation and analysis:

  • Analyze text (word count, character count, statistics)
  • Transform text (uppercase, lowercase, camelCase, snake_case)
  • Format text (trim, wrap, truncate, remove newlines)
  • No external dependencies

Web Metadata Tool

Webpage metadata extraction and social media summary generation:

  • Fetches title, description, and metadata from any URL
  • Generates AI-powered summaries optimized for social media
  • Supports platform-specific formatting (Twitter, LinkedIn, Facebook)
  • Automatic hashtag generation
  • No API key required

Getting Started

  1. Clone the repository
  2. Open FMF.xcodeproj in Xcode
  3. Ensure you have a device with Apple Intelligence enabled
  4. Build and run the project
  5. (Optional) For web search functionality:
    • Get an API key from Exa AI
    • Tap the gear icon in the app to access Settings
    • Enter your Exa API key in the settings screen
  6. Explore the different AI capabilities through the example buttons

Note: All features except web search work without any additional setup. The web search tool requires an Exa API key for functionality.

Contributing

Contributions are welcome! Please feel free to submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.