-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[AI] Structured output from FirebaseGenerable #15650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
paulb777
wants to merge
15
commits into
ah/ai-generable
Choose a base branch
from
pb-ah-generable
base: ah/ai-generable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9186914
[AI] Structured output from FirebaseGenerable
paulb777 ee686c1
review
paulb777 18b6f72
Add test for Generable struct
paulb777 e92aa43
Add Generable test
paulb777 dfcc8f2
doc fixes
paulb777 1c3be4b
review
paulb777 87efdbb
review
paulb777 1756fee
review
paulb777 3c5055c
review
paulb777 6336182
review
paulb777 6b3b11b
review
paulb777 6100faf
Add @Guide testing
paulb777 51bac30
More Guide tests
paulb777 57697db
review
paulb777 6a7c1b3
style
paulb777 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
FirebaseAI/Tests/TestApp/Tests/Integration/GenerateObjectIntegrationTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import FirebaseAILogic | ||
| import FirebaseAITestApp | ||
| import FirebaseAuth | ||
| import FirebaseCore | ||
| import Testing | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| struct Recipe: FirebaseGenerable, Equatable { | ||
| let name: String | ||
| let ingredients: [String] | ||
|
|
||
| static var jsonSchema: JSONSchema { | ||
| JSONSchema( | ||
| type: Self.self, | ||
| properties: [ | ||
| JSONSchema.Property(name: "name", type: String.self), | ||
| JSONSchema.Property(name: "ingredients", type: [String].self), | ||
| ] | ||
| ) | ||
| } | ||
|
|
||
| init(_ content: ModelOutput) throws { | ||
| name = try content.value(forProperty: "name") | ||
| ingredients = try content.value(forProperty: "ingredients") | ||
| } | ||
|
|
||
| var modelOutput: ModelOutput { | ||
| let properties: [(String, any ConvertibleToModelOutput)] = [ | ||
paulb777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ("name", name), | ||
| ("ingredients", ingredients), | ||
| ] | ||
| return ModelOutput(properties: properties, uniquingKeysWith: { _, second in second }) | ||
| } | ||
| } | ||
|
|
||
| @Suite(.serialized) | ||
| struct GenerateObjectIntegrationTests { | ||
| // Set temperature to 0 for deterministic output. | ||
| let generationConfig = GenerationConfig(temperature: 0.0, topP: 0.0, topK: 1) | ||
| let safetySettings = [ | ||
| SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove), | ||
| SafetySetting(harmCategory: .hateSpeech, threshold: .blockLowAndAbove), | ||
| SafetySetting(harmCategory: .sexuallyExplicit, threshold: .blockLowAndAbove), | ||
| SafetySetting(harmCategory: .dangerousContent, threshold: .blockLowAndAbove), | ||
| ] | ||
|
|
||
| @Test(arguments: [ | ||
| (InstanceConfig.vertexAI_v1beta, ModelNames.gemini2_5_Flash), | ||
| (InstanceConfig.googleAI_v1beta, ModelNames.gemini2_5_FlashLite), | ||
| (InstanceConfig.googleAI_v1beta, ModelNames.gemini3FlashPreview), | ||
| ]) | ||
| func generateObject_recipe(_ config: InstanceConfig, modelName: String) async throws { | ||
| let model = FirebaseAI.componentInstance(config).generativeModel( | ||
| modelName: modelName, | ||
| generationConfig: generationConfig, | ||
| safetySettings: safetySettings | ||
| ) | ||
| let prompt = "Generate a recipe for chocolate chip cookies." | ||
|
|
||
| let response = try await model.generateObject(Recipe.self, parts: prompt) | ||
|
|
||
| let recipe = response.content | ||
| #expect(!recipe.name.isEmpty) | ||
| #expect(!recipe.ingredients.isEmpty) | ||
| #expect(recipe.name.lowercased().contains("cookie")) | ||
|
|
||
| // verify rawContent structure | ||
| let rawContent = response.rawContent | ||
| guard case .structure = rawContent.kind else { | ||
| Issue.record("Raw content should be a structure") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| #if canImport(FoundationModels) | ||
| @Test(arguments: [ | ||
| (InstanceConfig.vertexAI_v1beta, ModelNames.gemini2_5_Flash), | ||
| (InstanceConfig.googleAI_v1beta, ModelNames.gemini2_5_FlashLite), | ||
| (InstanceConfig.googleAI_v1beta, ModelNames.gemini3FlashPreview), | ||
| ]) | ||
paulb777 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @available(iOS 26.0, macOS 26.0, *) | ||
| func generateObject_macroRecipe(_ config: InstanceConfig, modelName: String) async throws { | ||
| let model = FirebaseAI.componentInstance(config).generativeModel( | ||
| modelName: modelName, | ||
| generationConfig: generationConfig, | ||
| safetySettings: safetySettings | ||
| ) | ||
| let prompt = "Generate a recipe for brownies." | ||
|
|
||
| // MacroRecipe is defined below | ||
| let response = try await model.generateObject(MacroRecipe.self, parts: prompt) | ||
|
|
||
| let recipe = response.content | ||
| #expect(!recipe.name.isEmpty) | ||
| #expect(!recipe.ingredients.isEmpty) | ||
| #expect(recipe.name.lowercased().contains("brownie")) | ||
paulb777 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| #endif | ||
| } | ||
|
|
||
| #if canImport(FoundationModels) | ||
| import FoundationModels | ||
|
|
||
| @available(iOS 26.0, macOS 26.0, *) | ||
| @Generable | ||
| struct MacroRecipe: Equatable { | ||
| let name: String | ||
| let ingredients: [String] | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.