Slide 1

Slide 1 text

@peterfriese.dev Created by Mamank from Noun Project https: / / peterfriese.dev peterfriese Peter Friese, Staff Developer Relations Engineer, Google AI Development with Firebase

Slide 2

Slide 2 text

Friendly Meals • Collect favourite recipes

Slide 3

Slide 3 text

Friendly Meals • Collect favourite recipes • View preparation instructions

Slide 4

Slide 4 text

121 AI count

Slide 5

Slide 5 text

Friendly Meals Generate recipes based on what’s in your fridge Include an inspirational image of the dish Base the recipe on a curated set of recipes Eat pizza 🍕

Slide 6

Slide 6 text

Friendly Meals Eat pizza 🍕 Retrieval Augmented Generation Generate text from text Analyse images Generate images from text

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Setting up the model class RecipeGenerationService { private let vertexAI = VertexAI.vertexAI() private lazy var model = vertexAI .generativeModel(modelName: "gemini-2.0-flash") func foo() async throws { let prompt = "Hello World!" let response = try await model.generateContent(prompt) print(response.text) } }

Slide 11

Slide 11 text

Generate text from text func generateRecipe(from ingredients: String, cuisine: Cuisine, mealType: MealType, servings: Int) async throws - > String { let prompt = """ Create a \(cuisine.rawValue) \(mealType.rawValue.lowercased()) recipe for \(servings) people using these ingredients: \(ingredients). Generate: 1. A creative title that describes the dish 2. A brief, appetizing description 3. Estimated cooking time in minutes 4. List of ingredients with measurements 5. Step-by-step cooking instructions 6. Include the cuisine type ("\(cuisine.rawValue)") 7. For the imageURL, provide a URL to a high-quality food photo from Pexels.com that most closely matches this exact \(cuisine.rawValue) dish. The image should show a finished, plated dish that matches the recipe's style and ingredients. """ let response = try await model.generateContent(prompt) return response.text ? ? "" }

Slide 12

Slide 12 text

Analyse images private lazy var visionModel = vertexAI.generativeModel( modelName: "gemini-2.0-flash", generationConfig: GenerationConfig() ) func analyzeImage(_ image: UIImage) async throws -> String { let prompt = """ Please analyze this image and list all visible food ingredients. \ Format the response as a comma-separated list of ingredients. \ Be specific with measurements where possible, but focus on identifying the \ ingredients accurately. """ let response = try await visionModel.generateContent(prompt, image) return response.text ?? "" } Multimodal input

Slide 13

Slide 13 text

Generate images from text private lazy var imagenModel = vertexAI.imagenModel( modelName: "imagen-3.0-generate-002", generationConfig: ImagenGenerationConfig(numberOfImages: 1) ) func generateImage(for recipe: Recipe) async throws -> UIImage? { let prompt = """ A professional food photography shot of \(recipe.title). \ The dish should be \(recipe.description). \ Style: High-end food photography, restaurant-quality plating, soft natural \ lighting, shot from above on a clean background, showing the complete \ plated dish. \ Cuisine style: \(recipe.cuisine.rawValue) """ let response = try await imagenModel.generateImages(prompt: prompt) return response.images.first.flatMap { UIImage(data: $0.data) } }

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Setting up the model const ai = genkit({ plugins: [ vertexAI({ location: "us-central1" }), ], model: ge mi ni20Flash }); Unified model interface

Slide 18

Slide 18 text

const detectIngredientsFlow = ai.defineFlo w ({ name: "detectIngredientsFlow", inputSchema: z.object({ im age: z.string().describe("Base64 encoded im age of a fridge") }), outputSchema: ingredientSchema, }, async (input) => { const prompt = `Analyze this im age of a fridge or food items and list all the ingredients you can identify. For each ingredient, provide: 1. The name of the ingredient 2. The amount and unit (if visible, e.g., "2 liters”, "500 grams") Format the response as a structured list of ingredients.`; const { output } = await ai.generate({ prompt: [ { media: { url: input. i m age } Multimodal prompts in Genkit Genkit flow

Slide 19

Slide 19 text

const detectIngredientsFlow = ai.defineFlo w ({ name: "detectIngredientsFlow", inputSchema: z.object({ im age: z.string().describe("Base64 encoded im age of a fridge") }), outputSchema: ingredientSchema, }, async (input) => { const prompt = `Analyze this im age of a fridge or food items and list all the ingredients you can identify. For each ingredient, provide: 1. The name of the ingredient 2. The amount and unit (if visible, e.g., "2 liters”, "500 grams") Format the response as a structured list of ingredients.`; const { output } = await ai.generate({ prompt: [ { media: { url: input. i m age } Multimodal prompts in Genkit Input schema

Slide 20

Slide 20 text

const detectIngredientsFlow = ai.defineFlo w ({ name: "detectIngredientsFlow", inputSchema: z.object({ im age: z.string().describe("Base64 encoded im age of a fridge") }), outputSchema: ingredientSchema, }, async (input) => { const prompt = `Analyze this im age of a fridge or food items and list all the ingredients you can identify. For each ingredient, provide: 1. The name of the ingredient 2. The amount and unit (if visible, e.g., "2 liters”, "500 grams") Format the response as a structured list of ingredients.`; const { output } = await ai.generate({ prompt: [ { media: { url: input. i m age } Multimodal prompts in Genkit Prompt

Slide 21

Slide 21 text

const detectIngredientsFlow = ai.defineFlo w ({ name: "detectIngredientsFlow", inputSchema: z.object({ im age: z.string().describe("Base64 encoded im age of a fridge") }), outputSchema: ingredientSchema, }, async (input) => { const prompt = `Analyze this im age of a fridge or food items and list all the ingredients you can identify. For each ingredient, provide: 1. The name of the ingredient 2. The amount and unit (if visible, e.g., "2 liters”, "500 grams") Format the response as a structured list of ingredients.`; const { output } = await ai.generate({ prompt: [ { media: { url: input. i m age } Multimodal prompts in Genkit Call the model

Slide 22

Slide 22 text

2. The amount and unit (if visible, e.g., "2 liters”, "500 grams") Format the response as a structured list of ingredients.`; const { output } = await ai.generate({ prompt: [ { media: { url: input. i m age } }, { text: prompt } ], output: { schema: ingredientSchema } }); if (!output) { throw new Error("Failed to detect ingredients"); } return output; }); Multimodal prompts in Genkit Multimodal prompt

Slide 23

Slide 23 text

Vector Embeddings Task: Find all words that are food in the following sentence

Slide 24

Slide 24 text

Task: Find all words that are food in the following sentence “I went down to Aberystwyth on foot to buy some welsh cakes and a few berries. When I finished doing my groceries, I had a latte at Coffee #1, where I met a few other speakers.”

Slide 25

Slide 25 text

Task: Find all words that are food in the following sentence “I went down to Aberystwyth on foot to buy some welsh cakes and a few berries. When I finished doing my groceries, I had a latte at Coffee #1, where I met a few other speakers.”

Slide 26

Slide 26 text

Task: Find all words that are food in the following sentence “I went down to Aberystwyth on foot to buy some welsh cakes and a few berries. When I finished doing my groceries, I had a latte at Coffee #1, where I met a few other speakers.”

Slide 27

Slide 27 text

Vector embedding: A numerical representation of a word, sentence, or any other unit of text.

Slide 28

Slide 28 text

Vector embedding for food: [-0.018035058, 0.013980114, -0.01309541, 0.024956783, 0.02708295, -0.074924484, 0.03496225, 0.0125780115, . .. ]

Slide 29

Slide 29 text

Vector embedding for food: [-0.018035058, 0.013980114, -0.01309541, 0.024956783, 0.02708295, -0.074924484, 0.03496225, 0.0125780115, . .. ] Vector embedding for foot: [-0.016025933, 0.008207399, -0.03572462, 0.020942606, -0.0003162824, -0.041694388, 0.050102886, 0.007380137, . .. ]

Slide 30

Slide 30 text

[51.50721, -0.12758] Coordinates for London

Slide 31

Slide 31 text

Paris [48.85661, 2.35222] London [51.50721, -0.12758] New York [40.71277, -74.00597] Boston [42.36008, -71.05888]

Slide 32

Slide 32 text

Paris [48.85661, 2.35222] London [51.50721, -0.12758] New York [40.71277, -74.00597] Boston [42.36008, -71.05888]

Slide 33

Slide 33 text

Source: https://www.learndatasci.com/glossary/cosine-similarity/ Cosine vector similarity

Slide 34

Slide 34 text

const computeIngredients E m beddings = async (ingredientsList: string[]): Pro mi se => { const text = ingredientsList.join(", "); const result = await ai.embed({ embedder: text Em bedding005, content: text, }); return result[0].embedding; }; Computing embeddings

Slide 35

Slide 35 text

const recipeRetriever = defineFirestoreRetriever(ai, { name: 'recipeRetriever', firestore, collection: 'recipes', contentField: 'recipe', vectorField: 'ingredients E m beddings', embedder: text Em bedding005, distanceMeasure: 'COSINE' }); Retrieving documents

Slide 36

Slide 36 text

Genkit Developer UI

Slide 37

Slide 37 text

const inventRecipeFlow = ai.defineFlo w ({ name: "inventRecipeFlow", inputSchema: im ageBasedRecipeSchema, outputSchema: recipeSearchSchema, }, async (input) => { const detectedIngredients = await detectIngredientsFlo w ({ im age: input. im age }); const inspirationRecipes = await findRecipesFlo w ({ ingredientsList: detectedIngredients.ingredients. m ap(i => i.name) }); const generatedRecipe = await inventRecipeFlo w ({ inspirationRecipes, cuisine: input.cuisine, mea l T ype: input. m ea l T ype, servings: input.servings }); return generatedRecipe; }); Nested flows in Genkit

Slide 38

Slide 38 text

export const inventRecipe = onCallGenkit({ secrets: [apiKey], }, inventRecipeFlo w ); Calling Genkit flows from Swift let request = GenerateRecipeRequest( image: base64String, cuisine: cuisine.rawValue, mealType: mealType.rawValue.lowercased(), servings: servings, dietaryRestrictions: dietaryRestrictions ) let generateRecipe: Callable = functions.httpsCallable("generateRecipe") let response = try await generateRecipe(request) guard let generatedRecipe = response.recipes.first else { // handle error } return generatedRecipe } Expose Genkit flow as callable function

Slide 39

Slide 39 text

export const inventRecipe = onCallGenkit({ secrets: [apiKey], }, inventRecipeFlo w ); Calling Genkit flows from Swift let request = GenerateRecipeRequest( image: base64String, cuisine: cuisine.rawValue, mealType: mealType.rawValue.lowercased(), servings: servings, dietaryRestrictions: dietaryRestrictions ) let generateRecipe: Callable = functions.httpsCallable("generateRecipe") let response = try await generateRecipe(request) guard let generatedRecipe = response.recipes.first else { // handle error } return generatedRecipe } Define remote function in Swift

Slide 40

Slide 40 text

export const inventRecipe = onCallGenkit({ secrets: [apiKey], }, inventRecipeFlo w ); Calling Genkit flows from Swift let request = GenerateRecipeRequest( image: base64String, cuisine: cuisine.rawValue, mealType: mealType.rawValue.lowercased(), servings: servings, dietaryRestrictions: dietaryRestrictions ) let generateRecipe: Callable = functions.httpsCallable("generateRecipe") let response = try await generateRecipe(request) guard let generatedRecipe = response.recipes.first else { // handle error } return generatedRecipe } Call the function

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Demo!l

Slide 44

Slide 44 text

@peterfriese.dev Created by Mamank from Noun Project https: / / peterfriese.dev peterfriese Peter Friese, Staff Developer Relations Engineer, Google AI Development with Firebase

Slide 45

Slide 45 text

Thanks! @peterfriese.dev Created by Mamank from Noun Project https: / / peterfriese.dev peterfriese