-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
15d6f04
[FirebaseAI] Add usage of Grounding with Google Search
dlarocque ec1c964
Add xcode project changes
dlarocque d9fe94d
remove app bar, user icon, candidate extension
dlarocque 8b176d1
style
dlarocque 9f02ca3
Refactor to improve reusability!
dlarocque 2c78802
set default model
dlarocque 5be4ca3
Merge branch 'main' into dl/grounding
dlarocque c9d86e0
Temporarily use `CocoaPods-12.0.0` branch for SPM dependency
andrewheard 99c1362
Temporarily use commit SHA before Analytics 12.0 for SPM dependency
andrewheard fb77255
Refactor
dlarocque 3db183b
Update grounding PR branch to use Firebase 12 release
paulb777 118ee3c
Merge pull request #1730 from firebase/pb-grounding-12
dlarocque 58580a7
Review fixes
dlarocque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
firebaseai/GroundingSample/Screens/GroundingScreen.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import FirebaseAI | ||
import SwiftUI | ||
|
||
struct GroundingScreen: View { | ||
@StateObject var viewModel: GroundingViewModel | ||
|
||
init(firebaseService: FirebaseAI) { | ||
_viewModel = StateObject(wrappedValue: GroundingViewModel(firebaseService: firebaseService)) | ||
} | ||
|
||
var body: some View { | ||
VStack(spacing: 0) { | ||
Divider() | ||
|
||
// Main content | ||
ScrollView { | ||
VStack(spacing: 20) { | ||
if viewModel.inProgress { | ||
ProgressView().padding() | ||
} | ||
|
||
VStack(spacing: 20) { | ||
if let response = viewModel.response { | ||
// User Prompt turn | ||
UserPromptView(prompt: viewModel.sentPrompt) | ||
.frame(maxWidth: .infinity, alignment: .trailing) | ||
|
||
// Model Response turn (handles compliance internally) | ||
ModelResponseTurnView(response: response) | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
|
||
} else if let errorMessage = viewModel.errorMessage { | ||
Text(errorMessage) | ||
.foregroundColor(.red) | ||
.padding() | ||
} | ||
} | ||
.padding() | ||
} | ||
} | ||
.background(Color.appBackground) | ||
.onTapGesture { | ||
hideKeyboard() | ||
} | ||
|
||
// Input Field | ||
GroundingInputView( | ||
userInput: $viewModel.userInput, | ||
isGenerating: viewModel.inProgress | ||
) { | ||
Task { | ||
await viewModel.generateGroundedResponse() | ||
} | ||
} | ||
} | ||
.navigationTitle("Grounding") | ||
.navigationBarTitleDisplayMode(.inline) | ||
} | ||
|
||
private func hideKeyboard() { | ||
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), | ||
to: nil, from: nil, for: nil) | ||
} | ||
} | ||
|
||
private struct GroundingInputView: View { | ||
@Binding var userInput: String | ||
var isGenerating: Bool | ||
var onSend: () -> Void | ||
|
||
var body: some View { | ||
HStack { | ||
TextField("Ask a question...", text: $userInput) | ||
.textFieldStyle(.plain) | ||
.padding(10) | ||
.background(Color.inputBackground) | ||
.cornerRadius(20) | ||
|
||
Button(action: onSend) { | ||
Image(systemName: "arrow.up.circle.fill") | ||
.font(.title) | ||
.foregroundColor(userInput.isEmpty ? .gray : .accentColor) | ||
} | ||
.disabled(userInput.isEmpty || isGenerating) | ||
} | ||
.padding() | ||
.background(Color.appBackground.shadow(radius: 2, y: -1)) | ||
} | ||
} | ||
dlarocque marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
#Preview { | ||
NavigationView { | ||
GroundingScreen(firebaseService: FirebaseAI.firebaseAI()) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
firebaseai/GroundingSample/ViewModels/GroundingViewModel.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import FirebaseAI | ||
import Foundation | ||
import OSLog | ||
|
||
@MainActor | ||
class GroundingViewModel: ObservableObject { | ||
private var logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "generative-ai") | ||
|
||
@Published var userInput: String = "What's the weather in Chicago this weekend?" | ||
@Published var sentPrompt: String = "" | ||
|
||
@Published var response: GenerateContentResponse? | ||
@Published var errorMessage: String? | ||
@Published var inProgress = false | ||
|
||
private let model: GenerativeModel | ||
|
||
init(firebaseService: FirebaseAI) { | ||
model = firebaseService.generativeModel( | ||
modelName: "gemini-2.5-flash", | ||
tools: [.googleSearch()] | ||
) | ||
} | ||
|
||
func generateGroundedResponse() async { | ||
guard !userInput.isEmpty else { return } | ||
|
||
inProgress = true | ||
defer { inProgress = false } | ||
|
||
errorMessage = nil | ||
response = nil | ||
sentPrompt = userInput | ||
|
||
do { | ||
let result = try await model.generateContent(userInput) | ||
|
||
response = result | ||
userInput = "" // Clear input field on success | ||
} catch { | ||
logger.error("Error generating content: \(error)") | ||
errorMessage = error.localizedDescription | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.