-
Notifications
You must be signed in to change notification settings - Fork 1.7k
FirebaseGenerable #15527
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
12
commits into
main
Choose a base branch
from
pb-schemas
base: main
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
FirebaseGenerable #15527
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a66243a
Add a json schema integration test
paulb777 00d8d4a
Add FirebaseGenerable
paulb777 4d82d6d
generateObject returning a FirebaseGenerable
paulb777 a950496
Update FirebaseAILogicMacros/Sources/FirebaseAILogicMacro/FirebaseGen…
paulb777 d215a06
review checkpoint
paulb777 b259339
address code-assist review
paulb777 f56a133
review checkpoint
paulb777 f931ce6
more review feedback
paulb777 c7e86f1
review
paulb777 6587027
review
paulb777 e35d690
review
paulb777 4ae7933
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // 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 Foundation | ||
|
|
||
| enum GenerateObjectError: Error, CustomStringConvertible { | ||
| case responseTextError(String) | ||
| case jsonDecodingError(Error) | ||
|
|
||
| var description: String { | ||
| switch self { | ||
| case let .responseTextError(message): | ||
| return message | ||
| case let .jsonDecodingError(error): | ||
| return "Failed to decode JSON: \(error)" | ||
| } | ||
| } | ||
| } |
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,62 @@ | ||
| // 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 Foundation | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| public extension GenerativeModel { | ||
| /// Generates a structured object of a specified type that conforms to ``FirebaseGenerable``. | ||
| /// | ||
| /// This method simplifies the process of generating structured data by handling the schema | ||
| /// generation, API request, and JSON decoding automatically. | ||
| /// | ||
| /// - Parameters: | ||
| /// - type: The `FirebaseGenerable` type to generate. | ||
| /// - prompt: The text prompt to send to the model. | ||
| /// - Returns: An instance of the requested type, decoded from the model's JSON response. | ||
| /// - Throws: A ``GenerateContentError`` if the model fails to generate the content or if | ||
| /// the response cannot be decoded into the specified type. | ||
| func generateObject<T: FirebaseGenerable>(as type: T.Type, | ||
| from prompt: String) async throws -> T { | ||
| let model = GenerativeModel( | ||
| modelName: modelName, | ||
| modelResourceName: modelResourceName, | ||
| firebaseInfo: generativeAIService.firebaseInfo, | ||
| apiConfig: apiConfig, | ||
| generationConfig: GenerationConfig( | ||
| responseMIMEType: "application/json", | ||
| responseSchema: T.firebaseGenerationSchema | ||
| ), | ||
| safetySettings: safetySettings, | ||
| tools: tools, | ||
| toolConfig: toolConfig, | ||
| systemInstruction: systemInstruction, | ||
| requestOptions: requestOptions | ||
| ) | ||
| let response = try await model.generateContent(prompt) | ||
|
|
||
| guard let text = response.text, let data = text.data(using: .utf8) else { | ||
| throw GenerateContentError.internalError( | ||
| underlying: GenerateObjectError.responseTextError("Failed to get response text or data.") | ||
| ) | ||
| } | ||
|
|
||
| do { | ||
| return try JSONDecoder().decode(T.self, from: data) | ||
| } catch { | ||
| throw GenerateContentError | ||
| .internalError(underlying: GenerateObjectError.jsonDecodingError(error)) | ||
| } | ||
| } | ||
| } | ||
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,22 @@ | ||
| // 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 Foundation | ||
|
|
||
| /// A type that can be generated by a large language model. | ||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| public protocol FirebaseGenerable: Decodable { | ||
| /// A schema describing the structure of the generated type. | ||
| static var firebaseGenerationSchema: Schema { get } | ||
| } |
23 changes: 23 additions & 0 deletions
23
FirebaseAI/Sources/Types/Public/FirebaseGenerableMacro.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,23 @@ | ||
| // 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 Foundation | ||
|
|
||
| /// A macro that makes a ``Decodable`` type ``FirebaseGenerable``. | ||
| @attached(member, names: named(firebaseGenerationSchema)) | ||
| @attached(extension, conformances: FirebaseGenerable) | ||
| public macro FirebaseGenerable() = #externalMacro( | ||
| module: "FirebaseAILogicMacro", | ||
| type: "FirebaseGenerableMacro" | ||
| ) |
27 changes: 27 additions & 0 deletions
27
FirebaseAI/Sources/Types/Public/Schema+FirebaseGenerable.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,27 @@ | ||
| // 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 Foundation | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
| public extension Schema { | ||
| /// Returns a `Schema` representing a `Decodable` type. | ||
| static func from<T: Decodable>(_ type: T.Type) -> Schema { | ||
| if let type = type as? FirebaseGenerable.Type { | ||
| return type.firebaseGenerationSchema | ||
| } | ||
|
|
||
| return .object(properties: [:]) | ||
paulb777 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
Oops, something went wrong.
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.