firebase-ios-sdk
firebase-ios-sdk copied to clipboard
FirebaseGenerable
wip
Add func generateObject<T: FirebaseGenerable>(as type: T.Type, from prompt: String) async throws -> T
The new tests pass locally
Summary of Changes
This pull request significantly enhances the Firebase AI SDK by introducing the capability to generate structured Swift objects directly from generative model responses. It achieves this through a new FirebaseGenerable protocol and a Swift macro that automatically creates a schema for Decodable types. This feature streamlines the development workflow for applications requiring structured data output from AI models, reducing boilerplate and improving type safety.
Highlights
- Structured Object Generation: Introduced a new
generateObjectfunction inGenerativeModelthat allows generating structured Swift objects directly from the model's response, automating schema generation and JSON decoding. - FirebaseGenerable Protocol and Macro: Added a
FirebaseGenerableprotocol and an associated Swift macro (@FirebaseGenerable) to automatically derive aSchemaforDecodabletypes, simplifying the process of defining the expected output structure for the model. - Enhanced Configuration Inheritance: Improved the
GenerationConfigto allow for proper inheritance of existing settings when creating a new configuration for object generation, ensuring that parameters like temperature and token limits are respected. - Robust Schema Handling for Optional Types: Implemented correct schema generation for optional properties within
FirebaseGenerabletypes, ensuring that the model understands which fields can be null. - Comprehensive Error Reporting: Added specific error types (
GenerateObjectError) to provide clearer feedback when object generation fails due to issues like missing response text or JSON decoding problems.