Dynamic Guided Generation

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

Dynamic Guided Generation

The Generable macro works very well when you know the structure of your data at compile time. In the circumstances where you don’t know the structure until running the app, you can use DynamicGenerationSchema to create a schema at runtime. This produces a result similar to what you’ve done. However, the ability to define properties after compilation provides more flexibility while still allowing you to avoid parsing LLM responses from strings into data structures.

@State var ingredients: String = "lamb, salmon, duck"
Text("Comma separated list of possible ingredients.")
TextField("Ingredients for Special", text: $ingredients)
var ingredientArray: [String] {
  let array = ingredients.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
  if array.isEmpty {
    return ["lamb", "salmon", "duck"]
  } else {
    return array
  }
}
func generateMenuSpecial() {
  // 1
  let specialMealSchema = DynamicGenerationSchema(
    name: "specialmenuitem",
    // 2
    properties: [
      // 3
      DynamicGenerationSchema.Property(
        name: "ingredients",
        // 4
        schema: DynamicGenerationSchema(
          name: "ingredients",
          anyOf: ingredientArray
        )
      ),
      // 5
      DynamicGenerationSchema.Property(
        name: "name",
        schema: DynamicGenerationSchema(type: String.self)
      ),
      DynamicGenerationSchema.Property(
        name: "description",
        schema: DynamicGenerationSchema(type: String.self)
      ),
      DynamicGenerationSchema.Property(
        name: "price",
        schema: DynamicGenerationSchema(type: Decimal.self)
      )
    ]
  )
}
// 1
let schema = try? GenerationSchema(root: specialMealSchema, dependencies: [])
// 2
guard let schema = schema else { return }
// 3
let session = LanguageModelSession(instructions: "You are a helpful model assisting with generating realistic restaurant menus.")
let specialPrompt = "Produce a lunch special menu item that is focused on the specified ingredient."
let response = try? await session.respond(to: specialPrompt, schema: schema)
let name = try? response?.content.value(String.self, forProperty: "name")
let ingredients = try? response?.content.value(String.self, forProperty: "ingredients")
let description = try? response?.content.value(String.self, forProperty: "description")
let price = try? response?.content.value(Decimal.self, forProperty: "price")
let specialItem = MenuItem(
  name: name ?? "",
  description: description ?? "",
  ingredients: ingredients == nil ? [] : [ingredients!],
  cost: price ?? 0.0
)

special = specialItem
@State var special: MenuItem?
await generateMenuSpecial()
if let special = special {
  MenuItemView(menuItem: special.asPartiallyGenerated())
  Text("Today's Special")
    .font(.title2)
  Divider()
}
Including the Dynamically Generated Menu Item
Ufhcadohl lwe Pdsowaziqcv Zonuvoxuy Dude Iqid

See forum comments
Download course materials from Github
Previous: Streaming Guided Generation Data Next: Building Tools for Foundation Models