Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions firebaseai/FirebaseAIExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 56;
objectVersion = 70;
objects = {

/* Begin PBXBuildFile section */
Expand All @@ -27,7 +27,8 @@
886F95E02B17D5010036F07A /* ConversationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E10F562B1112F600C08E95 /* ConversationViewModel.swift */; };
886F95E12B17D5010036F07A /* ConversationScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88E10F542B1112CA00C08E95 /* ConversationScreen.swift */; };
886F95E32B17D6630036F07A /* GenerativeAIUIComponents in Frameworks */ = {isa = PBXBuildFile; productRef = 886F95E22B17D6630036F07A /* GenerativeAIUIComponents */; };
DE26D95F2DBB3E9F007E6668 /* FirebaseAI in Frameworks */ = {isa = PBXBuildFile; productRef = DE26D95E2DBB3E9F007E6668 /* FirebaseAI */; };
AEF66A332DF220800010A70C /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = AEF66A322DF220800010A70C /* GoogleService-Info.plist */; };
AEF66A362DF222560010A70C /* FirebaseAI in Frameworks */ = {isa = PBXBuildFile; productRef = AEF66A352DF222560010A70C /* FirebaseAI */; };
DEFECAA92D7B4CCD00EF9621 /* ImagenViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFECAA72D7B4CCD00EF9621 /* ImagenViewModel.swift */; };
DEFECAAA2D7B4CCD00EF9621 /* ImagenScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFECAA62D7B4CCD00EF9621 /* ImagenScreen.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -59,18 +60,23 @@
88E10F582B11131900C08E95 /* ChatMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatMessage.swift; sourceTree = "<group>"; };
88E10F5A2B11133E00C08E95 /* MessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageView.swift; sourceTree = "<group>"; };
88E10F5C2B11135000C08E95 /* BouncingDots.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BouncingDots.swift; sourceTree = "<group>"; };
AEF66A322DF220800010A70C /* GoogleService-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
DEFECAA62D7B4CCD00EF9621 /* ImagenScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagenScreen.swift; sourceTree = "<group>"; };
DEFECAA72D7B4CCD00EF9621 /* ImagenViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagenViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFileSystemSynchronizedRootGroup section */
AE36CB182E16CD1C006FC3CB /* GroundingSample */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = GroundingSample; sourceTree = "<group>"; };
/* End PBXFileSystemSynchronizedRootGroup section */

/* Begin PBXFrameworksBuildPhase section */
8848C82C2B0D04BC007B434F /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DE26D95F2DBB3E9F007E6668 /* FirebaseAI in Frameworks */,
886F95D82B17BA420036F07A /* MarkdownUI in Frameworks */,
886F95E32B17D6630036F07A /* GenerativeAIUIComponents in Frameworks */,
AEF66A362DF222560010A70C /* FirebaseAI in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -144,6 +150,7 @@
8848C8262B0D04BC007B434F = {
isa = PBXGroup;
children = (
AE36CB182E16CD1C006FC3CB /* GroundingSample */,
DEFECAA82D7B4CCD00EF9621 /* ImagenScreen */,
88B8A9352B0FCBA700424728 /* GenerativeAIUIComponents */,
869200B22B879C4F00482873 /* GoogleService-Info.plist */,
Expand All @@ -154,6 +161,7 @@
86C1F4822BC726150026816F /* FunctionCallingSample */,
8848C8302B0D04BC007B434F /* Products */,
88209C222B0FBE1700F64795 /* Frameworks */,
AEF66A322DF220800010A70C /* GoogleService-Info.plist */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -302,11 +310,14 @@
);
dependencies = (
);
fileSystemSynchronizedGroups = (
AE36CB182E16CD1C006FC3CB /* GroundingSample */,
);
name = FirebaseAISample;
packageProductDependencies = (
886F95D72B17BA420036F07A /* MarkdownUI */,
886F95E22B17D6630036F07A /* GenerativeAIUIComponents */,
DE26D95E2DBB3E9F007E6668 /* FirebaseAI */,
AEF66A352DF222560010A70C /* FirebaseAI */,
);
productName = GenerativeAISample;
productReference = 8848C82F2B0D04BC007B434F /* FirebaseAISample.app */;
Expand Down Expand Up @@ -339,7 +350,7 @@
packageReferences = (
88209C212B0FBDF700F64795 /* XCRemoteSwiftPackageReference "swift-markdown-ui" */,
DEA09AC32B1FCE22001962D9 /* XCRemoteSwiftPackageReference "NetworkImage" */,
DEFECAAB2D7BB49700EF9621 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */,
AEF66A342DF222560010A70C /* XCLocalSwiftPackageReference "../../sdks/firebase-ios-sdk" */,
);
productRefGroup = 8848C8302B0D04BC007B434F /* Products */;
projectDirPath = "";
Expand All @@ -356,6 +367,7 @@
buildActionMask = 2147483647;
files = (
8848C83A2B0D04BD007B434F /* Preview Assets.xcassets in Resources */,
AEF66A332DF220800010A70C /* GoogleService-Info.plist in Resources */,
8848C8372B0D04BD007B434F /* Assets.xcassets in Resources */,
869200B32B879C4F00482873 /* GoogleService-Info.plist in Resources */,
);
Expand Down Expand Up @@ -593,6 +605,13 @@
};
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
AEF66A342DF222560010A70C /* XCLocalSwiftPackageReference "../../sdks/firebase-ios-sdk" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = "../../sdks/firebase-ios-sdk";
};
/* End XCLocalSwiftPackageReference section */

/* Begin XCRemoteSwiftPackageReference section */
88209C212B0FBDF700F64795 /* XCRemoteSwiftPackageReference "swift-markdown-ui" */ = {
isa = XCRemoteSwiftPackageReference;
Expand All @@ -610,14 +629,6 @@
revision = 7aff8d1b31148d32c5933d75557d42f6323ee3d1;
};
};
DEFECAAB2D7BB49700EF9621 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/firebase/firebase-ios-sdk.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 11.13.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -630,9 +641,8 @@
isa = XCSwiftPackageProductDependency;
productName = GenerativeAIUIComponents;
};
DE26D95E2DBB3E9F007E6668 /* FirebaseAI */ = {
AEF66A352DF222560010A70C /* FirebaseAI */ = {
isa = XCSwiftPackageProductDependency;
package = DEFECAAB2D7BB49700EF9621 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */;
productName = FirebaseAI;
};
/* End XCSwiftPackageProductDependency section */
Expand Down
5 changes: 5 additions & 0 deletions firebaseai/FirebaseAISample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ struct ContentView: View {
} label: {
Label("Chat", systemImage: "ellipsis.message.fill")
}
NavigationLink {
GroundingScreen(firebaseService: firebaseService)
} label: {
Label("Grounding", systemImage: "magnifyingglass")
}
NavigationLink {
FunctionCallingScreen(firebaseService: firebaseService)
} label: {
Expand Down
109 changes: 109 additions & 0 deletions firebaseai/GroundingSample/Screens/GroundingScreen.swift
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))
}
}

#Preview {
NavigationView {
GroundingScreen(firebaseService: FirebaseAI.firebaseAI())
}
}
59 changes: 59 additions & 0 deletions firebaseai/GroundingSample/ViewModels/GroundingViewModel.swift
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
}
}
}
Loading