-
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
base: main
Are you sure you want to change the base?
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.
.clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous)) | ||
} | ||
// Set a max width to prevent the bubble from taking the full screen width. | ||
.frame(maxWidth: UIScreen.main.bounds.width * 0.8, alignment: .trailing) |
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.
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.
@andrewheard wdyt?
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 can take a look into it. Would you mind committing the Xcode project changes?
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.
Done
/// Displays the user's prompt in a chat bubble, aligned to the right. | ||
struct UserPromptView: View { | ||
let prompt: String | ||
|
||
var body: some View { | ||
HStack { | ||
Spacer() // Aligns the bubble to the right | ||
Text(prompt) | ||
.padding(12) | ||
.background(Color.userPromptBackground) | ||
.foregroundColor(Color.userPromptText) | ||
.clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous)) | ||
} | ||
// Set a max width to prevent the bubble from taking the full screen width. | ||
.frame(maxWidth: UIScreen.main.bounds.width * 0.8, alignment: .trailing) | ||
} | ||
} | ||
|
||
/// The content (text and sources) that goes inside the model's response bubble. | ||
struct ModelResponseContentView: View { | ||
let text: String | ||
let groundingMetadata: GroundingMetadata? | ||
|
||
var body: some View { | ||
VStack(alignment: .leading, spacing: 12) { | ||
Text(text) | ||
|
||
if let groundingChunks = groundingMetadata?.groundingChunks, !groundingChunks.isEmpty { | ||
Divider() | ||
VStack(alignment: .leading) { | ||
Text("Sources") | ||
.font(.footnote) | ||
.foregroundColor(.secondary) | ||
|
||
ForEach(0 ..< groundingChunks.count, id: \.self) { index in | ||
if let webChunk = groundingChunks[index].web { | ||
SourceLinkView( | ||
title: webChunk.title ?? "Untitled Source", | ||
uri: webChunk.uri | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// The complete visual component for the model's turn. It handles compliance checks internally. | ||
struct ModelResponseTurnView: View { | ||
let response: GenerateContentResponse | ||
|
||
var body: some View { | ||
// Use `if-let` to safely unwrap the first candidate. This is the correct SwiftUI pattern. | ||
if let candidate = response.candidates.first { | ||
// A response is non-compliant ONLY if groundingMetadata exists but searchEntryPoint is nil. | ||
let isNonCompliant = (candidate.groundingMetadata != nil && candidate.groundingMetadata? | ||
.searchEntryPoint == nil) | ||
|
||
if isNonCompliant { | ||
ComplianceErrorView() | ||
} else { | ||
// This branch handles both compliant grounded responses and non-grounded responses. | ||
HStack(alignment: .top, spacing: 8) { | ||
VStack(alignment: .leading, spacing: 8) { | ||
ModelResponseContentView( | ||
// Use the convenience accessor on the response object. | ||
text: response.text ?? "No text in response.", | ||
groundingMetadata: candidate.groundingMetadata | ||
) | ||
.padding(12) | ||
.background(Color.modelResponseBackground) | ||
.clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous)) | ||
|
||
if let searchEntryPoint = candidate.groundingMetadata?.searchEntryPoint { | ||
WebView(htmlString: searchEntryPoint.renderedContent) | ||
.frame(height: 44) | ||
.clipShape(RoundedRectangle(cornerRadius: 22)) | ||
} | ||
} | ||
} | ||
.frame(maxWidth: UIScreen.main.bounds.width * 0.8, alignment: .leading) | ||
} | ||
} else { | ||
// This `else` branch handles the case where the response has no candidates. | ||
ComplianceErrorView( | ||
message: "The response was blocked or contained no content." | ||
) | ||
} | ||
} | ||
} |
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.
Could we make some minor edits to the existing MessageView
instead of brand new views for this screen?
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 refactored all of this to use the existing ConversationScreen
and MessageView
. Let me know what you think!
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, Daniel! I think this will be much easier to maintain! I've got some minor nits that I'll share on Monday and I hope you don't mind that I'll push a fix (🤞) for the project.pbxproj
to hopefully get it building on CI. We'll still need to swap it back to use SemVer (version 12.0.0
) before merging but will need to wait for firebase-ios-sdk
to get tagged for that.
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!
Add usage of Grounding with Google Search feature to the FirebaseAIExample sample app. See firebase/firebase-ios-sdk#15014