Skip to content

Instantly share code, notes, and snippets.

@searls
Created August 17, 2025 18:57
Show Gist options
  • Save searls/52c5cf53220354cac2e89e9bcf54c27d to your computer and use it in GitHub Desktop.
Save searls/52c5cf53220354cac2e89e9bcf54c27d to your computer and use it in GitHub Desktop.
import Foundation
import FoundationModels
// Phase 1: Define a structure to model a dynamic set of prompts that could be driven by arbitrary data (e.g. flat JSON file, fetched over HTTP, etc)
struct EducationalPromptSet {
let type: String
let instructions: String
let name: String
let description: String
let summaryGuideDescription: String
let confidenceGuideDescription: String
let subComponents: [SubComponentPromptSet]
}
struct SubComponentPromptSet {
let title: String
let bodyGuideDescription: String
}
// Phase 2: Instantiate (manually in this case, but most likely from a Decoder) an instance of a prompt set
let cocktailPromptSet = EducationalPromptSet(
type: "bartender_basic",
instructions: """
You are an expert bartender. Take the provided cocktail name or list of ingredients and explain how to make a delicious cocktail. Be creative!
""",
name: "Cocktail Recipe",
description: "A custom cocktail recipe, tailored to the user's input and communicated in an educational tone and spirit",
summaryGuideDescription: "The summary should describe the history (if applicable) and taste profile of the cocktail",
confidenceGuideDescription: "Range between 0-100 for your confidence in the feasibility of this cocktail based on the prompt",
subComponents: [
SubComponentPromptSet(title: "Ingredients", bodyGuideDescription: "A list of all ingredients in the cocktail"),
SubComponentPromptSet(title: "Steps", bodyGuideDescription: "A list of the steps to make the cocktail"),
SubComponentPromptSet(title: "Prep", bodyGuideDescription: "The bar prep you should have completed in advance of service"),
]
)
// Phase 3: Generate a schema from an instance of a prompt set
let cocktailSchema = DynamicGenerationSchema(
name: cocktailPromptSet.name,
description: cocktailPromptSet.description,
properties: [
DynamicGenerationSchema.Property(
name: "summary",
description: cocktailPromptSet.summaryGuideDescription,
schema: DynamicGenerationSchema(type: String.self)
),
DynamicGenerationSchema.Property(
name: "confidence",
description: cocktailPromptSet.confidenceGuideDescription,
schema: DynamicGenerationSchema(type: Int.self)
),
DynamicGenerationSchema.Property(
name: "subComponents",
description: cocktailPromptSet.confidenceGuideDescription,
schema: DynamicGenerationSchema(
name: "subComponents",
properties: cocktailPromptSet.subComponents.map { subComponentPromptSet in
DynamicGenerationSchema.Property(
name: subComponentPromptSet.title,
description: subComponentPromptSet.bodyGuideDescription,
schema: DynamicGenerationSchema(type: String.self)
)
}
)
)
]
)
// Phase 4: Define result structs that can be instantiated from the model's GeneratedContent
struct EducationalResult : ConvertibleFromGeneratedContent {
let summary: String
let confidence: Int
let subComponents: [SubComponentResult]
init(_ content: GeneratedContent) throws {
summary = try content.value(String.self, forProperty: "summary")
confidence = try content.value(Int.self, forProperty: "confidence")
let subComponentsContent = try content.value(GeneratedContent.self, forProperty: "subComponents")
let properties: [String: GeneratedContent] = {
if case let .structure(properties, _) = subComponentsContent.kind {
return properties
}
return [:]
}()
subComponents = try properties.map { (title, bodyContent) in
try SubComponentResult(title: title, body: bodyContent.value(String.self))
}
}
}
struct SubComponentResult {
let title: String
let body: String
}
import Playgrounds
#Playground {
let session = LanguageModelSession {
cocktailPromptSet.instructions
}
let response = try await session.respond(
to: "Shirley Temple",
schema: GenerationSchema(root: cocktailSchema, dependencies: [])
)
/*
{"summary": "The Shirley Temple is a classic non-alcoholic cocktail that has been delighting children and adults alike since its invention in the 1930s. It is known for its simplicity and refreshing taste, combining ginger ale with a splash of grenadine and a touch of lime juice. This drink is a perfect balance of sweet, tangy, and fizzy, making it a timeless favorite.", "subComponents": {"Ingredients": "Ginger Ale, Grenadine, Lime Juice, Ice", "Prep": "Ensure you have ice and a shaker or mixing glass ready before starting.", "Steps": "1. Fill a glass with ice cubes. 2. Pour in 1 part ginger ale and 1 part grenadine. 3. Add a squeeze of fresh lime juice to enhance the citrus flavor. 4. Stir gently to combine all ingredients."}, "confidence": 100}
*/
let json = response.content.jsonString
/*
EducationalResult(
summary: "The Shirley Temple is a classic and refreshing cocktail that has been delighting children and adults alike for generations. It\'s known for its simplicity, sweet taste, and vibrant orange hue. Made primarily with ginger ale, it\'s a perfect example of a kid-friendly drink that doesn\'t compromise on flavor. The combination of ginger ale and grenadine creates a visually appealing and sweet-tart beverage, making it a staple at parties, brunches, and any occasion where a fun and easy drink is needed.",
confidence: 100,
subComponents: [
SubComponentResult(title: "Steps", body: "1. In a tall glass filled with ice, pour 2 oz of ginger ale. 2. Add 1 oz of grenadine carefully, swirling gently to combine. 3. Garnish with an orange slice and a cherry on top."),
SubComponentResult(title: "Prep", body: "Ensure you have fresh ginger ale and grenadine ready to go."),
SubComponentResult(title: "Ingredients", body: "2 oz ginger ale, 1 oz grenadine, Orange slice, Cherry")
])
*/
let cocktailResult = try EducationalResult(response.content)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment