-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[AI] Server Prompt Templates #15402
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
[AI] Server Prompt Templates #15402
Conversation
Generated by 🚫 Danger |
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces server-side prompt templates, a significant feature addition. The changes include new models (TemplateGenerativeModel, TemplateImagenModel), request types, and chat session logic to support using templates with variables. The code is well-structured, with good separation of concerns, such as moving history management into its own History class. I've identified a couple of areas where force unwraps on URL creation could be made safer. Overall, this is a solid implementation with good test coverage.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for server-side prompt templates for both generative and image generation models. This is a significant feature addition, including new model types like TemplateGenerativeModel and TemplateImagenModel, new request structures, and a TemplateChatSession for conversational AI with templates. The changes also involve some nice refactoring, such as abstracting history management into a dedicated History class. The overall implementation is well-structured and comes with a good set of unit and integration tests. My feedback is focused on improving robustness by avoiding force-unwraps in URL construction, ensuring proper URL encoding of user-provided data, and making the TemplateVariable type more versatile by handling Float values. I've also included a minor suggestion to clean up test code.
FirebaseAI/Tests/TestApp/Tests/Integration/ServerPromptTemplateIntegrationTests.swift
Outdated
Show resolved
Hide resolved
|
/gemini summarize |
|
@andrewheard @cynthiajoan Still some test tweaks once the backend supports both API providers with the same templates and this won't merge until after M172, but should now be ready for review. |
Updated `GenerateImagesRequest` to encode the `variables` property as `inputs`, similar to how `TemplateGenerateContentRequest` handles it. This ensures consistency across the API.
Updated `GenerateImagesRequest` to correctly construct the URL, including the `projectID` and `location`. This mirrors the URL construction in `TemplateGenerateContentRequest` and fixes a 404 error that was occurring in the `testGenerateImages` integration test. The `TemplateImagenModel` was also updated to pass the `projectID` to the `GenerateImagesRequest` initializer.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a significant enhancement to the Firebase AI SDK by adding support for server-side prompt templates. The changes are well-structured, with new classes for template-based models (TemplateGenerativeModel, TemplateImagenModel) and a dedicated TemplateChatSession. The refactoring of chat history into a separate History class is a great improvement for modularity and thread safety. Additionally, the move from force-unwrapping URLs to a throwing mechanism enhances the robustness of the networking layer. My review focuses on ensuring URL components are properly encoded, maintaining consistency in error handling, and improving the maintainability of test utilities. Overall, this is a high-quality contribution.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces significant new functionality by adding support for server-side prompt templates. The changes are well-structured, with new classes for template-based models (TemplateGenerativeModel, TemplateImagenModel) and chat sessions (TemplateChatSession). The refactoring of chat history management into a dedicated History class is a great improvement for modularity and thread safety. The move away from force-unwrapping URLs to throwing functions is also a commendable improvement in robustness. My review focuses on further improving the robustness of URL construction and enhancing test maintainability.
FirebaseAI/Tests/TestApp/Tests/Integration/ServerPromptTemplateIntegrationTests.swift
Outdated
Show resolved
Hide resolved
Co-authored-by: Andrew Heard <[email protected]>
Apple implementation of Server Prompt Templates inspired by Flutterfire PR.
Googlers: see API review at go/firebase-ai-server-prompt-template
Summary of Changes
This pull request significantly enhances the Firebase AI SDK by introducing support for server-side prompt templates. This allows developers to define and use reusable templates for generative AI models, making it easier to manage complex prompts and integrate dynamic data. The changes include new model types for generative and image AI, a dedicated chat session for template-based conversations, and a refactored, thread-safe history management system. These additions provide a more robust and flexible framework for building AI-powered applications.
Highlights
TemplateGenerativeModelandTemplateImagenModelclasses to support server-side prompt templates, allowing for more flexible and dynamic AI interactions.TemplateChatSessionto enable conversational interactions with generative models using predefined templates and dynamic variables, supporting both single messages and streaming responses.Historyclass, improving modularity, reusability, and thread safety for managing conversation history in both standard and template-based chat sessions.TemplateVariableenum to handle various data types (String, Int, Double, Bool, Array, Dictionary) for substituting values into prompt templates, including automatic conversion forFloattoDouble.TemplateGenerateContentRequest,TemplateGenerateImagesRequest) and internal API method enums (APIMethod,ImageAPIMethod) to support the new template-based API endpoints.Changelog
Historyclass for managing chat history, removing internal history management logic and related locks.appendHistorycalls to use the_historyinstance of the newHistoryclass.templateGenerativeModel()andtemplateImagenModel()to initialize new template-based AI models.APIMethodenum, which has been moved to a new, dedicated internal file.firebaseInfoproperty fromprivate lettoletto allow access from newly introduced template models.Historyclass to encapsulate chat history management, including thread-safe appending ofModelContentand aggregation of content chunks.TemplateChatSessionfor managing chat conversations with server-side prompt templates, providingsendMessageandsendMessageStreammethods.TemplateGenerativeModelfor interacting with generative AI models using templates, including methods for content generation and starting chat sessions.TemplateImagenModelfor interacting with image generation models using templates.TemplateVariableenum to handle various data types for template variables, including a conversion fromFloattoDoubleduring initialization.APIMethodenum (generateContent, streamGenerateContent, countTokens) here fromGenerateContentRequest.swift.ImageAPIMethodenum specifically for image generation API methods, such astemplatePredict.TemplateGenerativeModelandTemplateImagenModel, covering text generation, image generation, and chat sessions with templates and media.TemplateChatSessionclass, verifyingsendMessageandsendMessageStreamfunctionality.TemplateGenerativeModelclass, testinggenerateContentandgenerateContentStreammethods.TemplateImagenModelclass, specifically testing thegenerateImagesmethod.httpRequestHandlerto include anisTemplateRequestparameter, allowing for conditional URL path assertions to support template-based requests in mock tests.collectTextFromStreamto simplify collecting text fromAsyncThrowingStreamresponses in tests.Activity
andrewheardsuggested extracting chat history management into a separate type, which was subsequently implemented bypaulb777.gemini-code-assist[bot]highlighted potential issues with force unwrapping URLs in new request types and suggested percent-encoding template names for robustness.paulb777acknowledged these, noting consistency with existing code.gemini-code-assist[bot]to handleFloatvalues inTemplateVariableinitialization was addressed and implemented in the changes.gemini-code-assist[bot]included aTODOfor streaming and a hardcoded path in a test utility.