Skip to content

[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

Merged
merged 13 commits into from
Jul 22, 2025
Merged

Conversation

dlarocque
Copy link
Contributor

@dlarocque dlarocque commented Jul 3, 2025

Add usage of Grounding with Google Search feature to the FirebaseAIExample sample app. See firebase/firebase-ios-sdk#15014

@dlarocque dlarocque requested review from paulb777 and andrewheard July 3, 2025 18:52
Copy link

@gemini-code-assist gemini-code-assist bot left a 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 the GenerativeModel to use Google Search as a tool for generating grounded content.
  • Reusable UI Components: Several new SwiftUI views (UserPromptView, ModelResponseTurnView, SourceLinkView, ComplianceErrorView) and a WKWebView wrapper are introduced to elegantly display chat interactions, model responses, attributed sources, and embedded search suggestions.
  • Theming and Styling: New Color and UIColor extensions are added to UIConstants.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

  1. 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.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

@dlarocque dlarocque marked this pull request as draft July 3, 2025 19:08
@dlarocque dlarocque marked this pull request as ready for review July 3, 2025 19:33
Copy link
Member

@paulb777 paulb777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

import SwiftUI

/// A view that displays a chat message that is grounded in Google Search.
struct GroundedResponseView: View {
Copy link
Contributor

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.

Copy link
Contributor

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 {
Copy link
Contributor

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
  }

Copy link
Contributor Author

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)?

Copy link
Contributor

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:

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 and Hashable 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.

Copy link
Contributor

@peterfriese peterfriese Jul 16, 2025

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:

  1. Go with a slightly more complete (but still incomplete) == implementation
  2. Make sure GroundingMetadata conforms to Equatable
  3. 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.

@paulb777
Copy link
Member

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.

@paulb777 paulb777 merged commit 979388a into main Jul 22, 2025
6 checks passed
@paulb777 paulb777 deleted the dl/grounding branch July 22, 2025 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants