Generating Custom Data Structures

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Generating Custom Data Structures

To explore the value of guided generation, first consider the case of developing a data structure without it. Run the app and enter the following prompt:

Create a menu for lunch at a casual dining restaurant
Menu created by asking the chat app.
Joqe lqeurav md obxevx kxo xbum ujl.

import Foundation
import FoundationModels

enum MealType: String {
  case breakfast = "Breakfast"
  case lunch = "Lunch"
  case dinner = "Dinner"
}

struct MenuItem {
  let name: String
  let description: String
  let ingredients: [String]
  let cost: Decimal
}

struct RestaurantMenu {
  let type: MealType
  let menu: [MenuItem]
}
@Generable(description: "A menu of offerings for a restaurant for a single meal.")
@Generable
@Generable(description: "A single dish for a restaurant menu.")
@Generable(description: "A single dish for a restaurant menu.")
struct MenuItem {
  @Guide(description: "Name for this dish.")
  let name: String

  @Guide(description: "The description of this dish in a style appropriate for a restaurant menu.")
  let description: String

  @Guide(description: "The main ingredients for this dish.")
  let ingredients: [String]

  @Guide(description: "A cost for this dish in US dollars, which should be appropriate for the ingredients", )
  let cost: Decimal
}
@Generable(description: "A menu of offerings for a restaurant for a single meal.")
struct RestaurantMenu {
  let type: MealType

  @Guide(description: "A list of menu items, appropriate for the selected type of meal.", .count(4...8))
  let menu: [MenuItem]
}
import SwiftUI
import FoundationModels

struct MenuItemView: View {
  var menuItem: MenuItem

  var body: some View {
      HStack {
        Text(menuItem.name)
        Spacer()
        Text(menuItem.cost, format: .currency(code: "USD"))
      }
      .font(.title)
    Text(menuItem.description)
      .frame(maxWidth: .infinity, alignment: .leading)
      .padding(.leading, 15.0)
      .font(.headline)
    Text(menuItem.ingredients.joined(separator: " • "))
      .font(.subheadline)
  }
}

#Preview {
  let item = MenuItem(
    name: "Caesar Salad",
    description: "Romaine lettuce tossed in Caesar dressing with parmesan cheese and croutons.",
    ingredients: ["romaine lettuce", "Caesar dressing", "parmesan cheese", "croutons"],
    cost: 10.0
  )
  MenuItemView(menuItem: item)
}
@State var menu: RestaurantMenu?
// 1
func generateLunchMenu() async {
  // 2
  let session = LanguageModelSession(instructions: "You are a helpful model assisting with generating realistic restaurant menus.")
  // 3
  let prompt = "Create a menu for lunch at a casual dining restaurant"
  // 4
  let response = try? await session.respond(to: prompt, generating: RestaurantMenu.self)
  // 5
  menu = response?.content
}
Task {
  await generateLunchMenu()
}
if let menu = menu {
  ScrollView {
    ForEach(menu.menu, id: \.name) { item in
      MenuItemView(menuItem: item)
      Divider()
    }
  }
}
Complete generated menu.
Sogznexi kobugapih xixe.

See forum comments
Download course materials from Github
Previous: Introducing Guided Generation Next: Streaming Guided Generation Data