-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[FirebaseAI] Add usage of Grounding with Google Search #1724
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
Conversation
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.
Summary of Changes
Hello @dlarocque, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request significantly enhances the FirebaseAIExample sample application by integrating and demonstrating the 'Grounding with Google Search' feature. It introduces a new dedicated screen and associated logic, allowing users to interact with a generative AI model that leverages Google Search for grounded responses, complete with source attribution and search suggestions.
Highlights
- Google Search Grounding Integration: This pull request introduces the 'Grounding with Google Search' feature into the FirebaseAIExample sample application, allowing the AI model to provide responses grounded in real-time search results.
- New Grounding UI: A dedicated
GroundingScreen
has been added to the sample app, providing a user interface to interact with the grounded AI model, including input fields, response display, and source attribution. - Grounded Response Logic: A new
GroundingViewModel
manages the state and interaction with the FirebaseAI SDK, configuring theGenerativeModel
to use Google Search as a tool for generating grounded content. - Reusable UI Components: Several new SwiftUI views (
UserPromptView
,ModelResponseTurnView
,SourceLinkView
,ComplianceErrorView
) and aWKWebView
wrapper are introduced to elegantly display chat interactions, model responses, attributed sources, and embedded search suggestions. - Theming and Styling: New
Color
andUIColor
extensions are added toUIConstants.swift
to ensure consistent visual theming across the new Grounding feature, supporting both light and dark modes.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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 adds a new example for Grounding with Google Search. The code is well-organized. The feedback focuses on improving code safety by removing a risky force-unwrap and replacing a deprecated API to ensure future compatibility.
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.
Thanks!
firebaseai/ChatExample/Views/Grounding/GroundedResponseView.swift
Outdated
Show resolved
Hide resolved
import SwiftUI | ||
|
||
/// A view that displays a chat message that is grounded in Google Search. | ||
struct GroundedResponseView: View { |
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.
I think it might be better to enhance ResponseTextView to provide a slot for an auxiliary view.
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.
We can do that in a follow-up PR.
let participant: Participant | ||
var pending = false | ||
|
||
static func pending(participant: Participant) -> ChatMessage { | ||
Self(message: "", participant: participant, pending: true) | ||
} | ||
|
||
static func == (lhs: ChatMessage, rhs: ChatMessage) -> Bool { |
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.
This equality operator is only here because GroundingMetadata
doesn't conform to Equatable
. Since it's a rather complex type, I suggest simplifying this ==
implementation to only check equality of the id
attribute. This is sufficient, since each chat message is uniquely identified by its id
attribute.
Let's also call this out:
// For chat messages, ID-based equality is appropriate since each message should be unique
static func == (lhs: ChatMessage, rhs: ChatMessage) -> Bool {
lhs.id == rhs.id
}
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.
If this implementation only compares the id
's, won't the view not update if we update a ChatMessage
field like text
(for example, in a case where we are streaming a chat message, and updating the text
field as each chunk arrives)?
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.
I think @dlarocque is right. As another example, if the equality check doesn't include pending
then the following won't trigger:
.onChange(of: viewModel.messages, perform: { newValue in | |
if viewModel.hasError { | |
// wait for a short moment to make sure we can actually scroll to the bottom | |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { | |
withAnimation { | |
scrollViewProxy.scrollTo("errorView", anchor: .bottom) | |
} | |
focusedField = .message | |
} | |
} else { | |
guard let lastMessage = viewModel.messages.last else { return } | |
// wait for a short moment to make sure we can actually scroll to the bottom | |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { | |
withAnimation { | |
scrollViewProxy.scrollTo(lastMessage.id, anchor: .bottom) | |
} | |
focusedField = .message | |
} | |
} | |
}) |
This results in MessageContentView
showing the bouncing dots forever:
quickstart-ios/firebaseai/ChatExample/Views/MessageView.swift
Lines 42 to 44 in 0485eec
if message.pending { | |
BouncingDots() | |
} else { |
How about we add // TODO(andrewheard): Add Equatable conformance to GroundingMetadata and remove this
instead? Although GroundingMetadata
is quite a complex type, all the leaf nodes are basic types so it's quite easy to make it conform and Apple actually recommends doing so:
Conforming to the
Equatable
andHashable
protocols is straightforward and makes it easier to use your own types in Swift. It’s a good idea for all your custom model types to conform.
-- https://developer.apple.com/documentation/swift/adopting-common-protocols
I think it's something we should consider for our APIs going forward.
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.
Good point - I think I got too used to how this works in the new world (with the Observable macro), where this does indeed work.
So we've got three options:
- Go with a slightly more complete (but still incomplete)
==
implementation - Make sure
GroundingMetadata
conforms toEquatable
- Migrate to the Observation framework
Since not everybody will be able to use the new Observation framework, we should probably implement (2) in the short term, which also doesn't hurt once we're able to migrate to the Observation framework.
I'm going to merge as-is since the grounding feature is now released and documented. We can follow up with the outstanding comments in a separate PR. |
Add usage of Grounding with Google Search feature to the FirebaseAIExample sample app. See firebase/firebase-ios-sdk#15014