Skip to content

Commit 8fa8f78

Browse files
committed
Update sample to use GoogleGenerativeAI instead of FirebaseAI
1 parent 44a40da commit 8fa8f78

File tree

10 files changed

+195
-159
lines changed

10 files changed

+195
-159
lines changed

Examples/GenerativeAISample/ChatSample/Screens/ConversationScreen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// REMOVED: `import GoogleGenerativeAI` -- was not used in this file
1516
import GenerativeAIUIComponents
16-
import GoogleGenerativeAI
1717
import SwiftUI
1818

1919
struct ConversationScreen: View {

Examples/GenerativeAISample/ChatSample/ViewModels/ConversationViewModel.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import FirebaseAI // REPLACED: `GoogleGenerativeAI` with `FirebaseAI`
1516
import Foundation
16-
import GoogleGenerativeAI
1717
import UIKit
1818

1919
@MainActor
@@ -36,7 +36,12 @@ class ConversationViewModel: ObservableObject {
3636
private var chatTask: Task<Void, Never>?
3737

3838
init() {
39-
model = GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: APIKey.default)
39+
// BEFORE
40+
// model = GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: APIKey.default)
41+
42+
// AFTER
43+
model = FirebaseAI.firebaseAI().generativeModel(modelName: "gemini-2.0-flash")
44+
4045
chat = model.startChat()
4146
}
4247

@@ -79,7 +84,7 @@ class ConversationViewModel: ObservableObject {
7984
messages.append(systemMessage)
8085

8186
do {
82-
let responseStream = chat.sendMessageStream(text)
87+
let responseStream = try chat.sendMessageStream(text) // ADDED: `try`
8388
for try await chunk in responseStream {
8489
messages[messages.count - 1].pending = false
8590
if let text = chunk.text {

Examples/GenerativeAISample/ChatSample/Views/ErrorDetailsView.swift

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import GoogleGenerativeAI
15+
import FirebaseAI // REPLACED: `GoogleGenerativeAI` with `FirebaseAI`
1616
import MarkdownUI
1717
import SwiftUI
1818

19-
extension SafetySetting.HarmCategory: CustomStringConvertible {
19+
extension HarmCategory: CustomStringConvertible { // REMOVED: `SafetySetting.`
2020
public var description: String {
2121
switch self {
2222
case .dangerousContent: "Dangerous content"
2323
case .harassment: "Harassment"
2424
case .hateSpeech: "Hate speech"
2525
case .sexuallyExplicit: "Sexually explicit"
26-
case .unknown: "Unknown"
27-
case .unspecified: "Unspecified"
26+
case .civicIntegrity: "Civic integrity" // ADDED
27+
// REMOVED: case .unspecified: "Unspecified"
28+
default: "Unknown" // REPLACED `case .unknown`
2829
}
2930
}
3031
}
@@ -36,8 +37,7 @@ extension SafetyRating.HarmProbability: CustomStringConvertible {
3637
case .low: "Low"
3738
case .medium: "Medium"
3839
case .negligible: "Negligible"
39-
case .unknown: "Unknown"
40-
case .unspecified: "Unspecified"
40+
default: "Unknown" // REPLACED: `case .unknown`
4141
}
4242
}
4343
}
@@ -102,6 +102,17 @@ struct ErrorDetailsView: View {
102102
value: underlyingError.localizedDescription)
103103
}
104104

105+
// ADDED
106+
case let GenerateContentError.promptImageContentError(underlying: underlyingError):
107+
Section("Error Type") {
108+
Text("Creating prompt image content failed")
109+
}
110+
111+
Section("Details") {
112+
SubtitleFormRow(title: "Error description",
113+
value: underlyingError.localizedDescription)
114+
}
115+
105116
case let GenerateContentError.promptBlocked(response: generateContentResponse):
106117
Section("Error Type") {
107118
Text("Your prompt was blocked")
@@ -142,36 +153,9 @@ struct ErrorDetailsView: View {
142153
SafetyRatingsSection(ratings: ratings)
143154
}
144155

145-
case GenerateContentError.invalidAPIKey:
146-
Section("Error Type") {
147-
Text("Invalid API Key")
148-
}
149-
150-
Section("Details") {
151-
SubtitleFormRow(title: "Error description", value: error.localizedDescription)
152-
SubtitleMarkdownFormRow(
153-
title: "Help",
154-
value: """
155-
Please provide a valid value for `API_KEY` in the `GenerativeAI-Info.plist` file.
156-
"""
157-
)
158-
}
159-
160-
case GenerateContentError.unsupportedUserLocation:
161-
Section("Error Type") {
162-
Text("Unsupported User Location")
163-
}
156+
// REMOVED: `GenerateContentError.invalidAPIKey`
164157

165-
Section("Details") {
166-
SubtitleFormRow(title: "Error description", value: error.localizedDescription)
167-
SubtitleMarkdownFormRow(
168-
title: "Help",
169-
value: """
170-
The API is unsupported in your location (country / territory); please see the list of
171-
[available regions](https://ai.google.dev/available_regions#available_regions).
172-
"""
173-
)
174-
}
158+
// REMOVED: `GenerateContentError.unsupportedUserLocation`
175159

176160
default:
177161
Section("Error Type") {
@@ -192,23 +176,32 @@ struct ErrorDetailsView: View {
192176
#Preview("Response Stopped Early") {
193177
let error = GenerateContentError.responseStoppedEarly(
194178
reason: .maxTokens,
195-
response: GenerateContentResponse(candidates: [
196-
CandidateResponse(content: ModelContent(role: "model", [
197-
"""
198-
A _hypothetical_ model response.
199-
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
200-
""",
201-
]),
202-
safetyRatings: [
203-
SafetyRating(category: .dangerousContent, probability: .high),
204-
SafetyRating(category: .harassment, probability: .low),
205-
SafetyRating(category: .hateSpeech, probability: .low),
206-
SafetyRating(category: .sexuallyExplicit, probability: .low),
179+
response: GenerateContentResponse(
180+
candidates: [
181+
Candidate( // REPLACED: `CandidateResponse` with `Candidate`
182+
content: ModelContent(role: "model", parts: [ // ADDED: `parts: `
183+
"""
184+
A _hypothetical_ model response.
185+
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
186+
""",
187+
]),
188+
safetyRatings: [
189+
// ADDED: `probabilityScore`, `severity`, `severityScore`, and `blocked`
190+
SafetyRating(category: .dangerousContent, probability: .high, probabilityScore: 0.0,
191+
severity: .negligible, severityScore: 0.0, blocked: false),
192+
SafetyRating(category: .harassment, probability: .low, probabilityScore: 0.0,
193+
severity: .negligible, severityScore: 0.0, blocked: false),
194+
SafetyRating(category: .hateSpeech, probability: .low, probabilityScore: 0.0,
195+
severity: .negligible, severityScore: 0.0, blocked: false),
196+
SafetyRating(category: .sexuallyExplicit, probability: .low, probabilityScore: 0.0,
197+
severity: .negligible, severityScore: 0.0, blocked: false),
198+
],
199+
finishReason: FinishReason.maxTokens,
200+
citationMetadata: nil
201+
),
207202
],
208-
finishReason: FinishReason.maxTokens,
209-
citationMetadata: nil),
210-
],
211-
promptFeedback: nil)
203+
promptFeedback: nil
204+
)
212205
)
213206

214207
return ErrorDetailsView(error: error)
@@ -217,17 +210,24 @@ struct ErrorDetailsView: View {
217210
#Preview("Prompt Blocked") {
218211
let error = GenerateContentError.promptBlocked(
219212
response: GenerateContentResponse(candidates: [
220-
CandidateResponse(content: ModelContent(role: "model", [
213+
// REPLACED: `CandidateResponse` with `Candidate`
214+
Candidate(content: ModelContent(role: "model", parts: [ // ADDED: `parts: `
221215
"""
222216
A _hypothetical_ model response.
223217
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
224218
""",
225219
]),
226220
safetyRatings: [
227-
SafetyRating(category: .dangerousContent, probability: .high),
228-
SafetyRating(category: .harassment, probability: .low),
229-
SafetyRating(category: .hateSpeech, probability: .low),
230-
SafetyRating(category: .sexuallyExplicit, probability: .low),
221+
// ADDED: `probabilityScore`, `severity`, `severityScore`, and `blocked`
222+
SafetyRating(category: .dangerousContent, probability: .high, probabilityScore: 0.0,
223+
severity: .negligible, severityScore: 0.0, blocked: false),
224+
SafetyRating(category: .harassment, probability: .low, probabilityScore: 0.0,
225+
severity: .negligible, severityScore: 0.0, blocked: false),
226+
SafetyRating(category: .hateSpeech, probability: .low, probabilityScore: 0.0,
227+
severity: .negligible, severityScore: 0.0, blocked: false),
228+
SafetyRating(category: .sexuallyExplicit, probability: .low,
229+
probabilityScore: 0.0, severity: .negligible, severityScore: 0.0,
230+
blocked: false),
231231
],
232232
finishReason: FinishReason.other,
233233
citationMetadata: nil),
@@ -238,10 +238,6 @@ struct ErrorDetailsView: View {
238238
return ErrorDetailsView(error: error)
239239
}
240240

241-
#Preview("Invalid API Key") {
242-
ErrorDetailsView(error: GenerateContentError.invalidAPIKey(message: "Fix API key placeholder"))
243-
}
241+
// REMOVED: Preview for `GenerateContentError.invalidAPIKey`
244242

245-
#Preview("Unsupported User Location") {
246-
ErrorDetailsView(error: GenerateContentError.unsupportedUserLocation)
247-
}
243+
// REMOVED: Preview for `GenerateContentError.unsupportedUserLocation`

Examples/GenerativeAISample/ChatSample/Views/ErrorView.swift

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import GoogleGenerativeAI
15+
import FirebaseAI // REPLACED: `GoogleGenerativeAI` with `FirebaseAI`
1616
import SwiftUI
1717

1818
struct ErrorView: View {
@@ -36,23 +36,32 @@ struct ErrorView: View {
3636
#Preview {
3737
NavigationView {
3838
let errorPromptBlocked = GenerateContentError.promptBlocked(
39-
response: GenerateContentResponse(candidates: [
40-
CandidateResponse(content: ModelContent(role: "model", [
41-
"""
42-
A _hypothetical_ model response.
43-
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
44-
""",
45-
]),
46-
safetyRatings: [
47-
SafetyRating(category: .dangerousContent, probability: .high),
48-
SafetyRating(category: .harassment, probability: .low),
49-
SafetyRating(category: .hateSpeech, probability: .low),
50-
SafetyRating(category: .sexuallyExplicit, probability: .low),
39+
response: GenerateContentResponse(
40+
candidates: [
41+
Candidate( // REPLACED: `CandidateResponse` with `Candidate`
42+
content: ModelContent(role: "model", parts: [ // ADDED: `parts: `
43+
"""
44+
A _hypothetical_ model response.
45+
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
46+
""",
47+
]),
48+
safetyRatings: [
49+
// ADDED: `probabilityScore`, `severity`, `severityScore`, `blocked`
50+
SafetyRating(category: .dangerousContent, probability: .high, probabilityScore: 0.0,
51+
severity: .negligible, severityScore: 0.0, blocked: false),
52+
SafetyRating(category: .harassment, probability: .low, probabilityScore: 0.0,
53+
severity: .negligible, severityScore: 0.0, blocked: false),
54+
SafetyRating(category: .hateSpeech, probability: .low, probabilityScore: 0.0,
55+
severity: .negligible, severityScore: 0.0, blocked: false),
56+
SafetyRating(category: .sexuallyExplicit, probability: .low, probabilityScore: 0.0,
57+
severity: .negligible, severityScore: 0.0, blocked: false),
58+
],
59+
finishReason: FinishReason.other,
60+
citationMetadata: nil
61+
),
5162
],
52-
finishReason: FinishReason.other,
53-
citationMetadata: nil),
54-
],
55-
promptFeedback: nil)
63+
promptFeedback: nil
64+
)
5665
)
5766
List {
5867
MessageView(message: ChatMessage.samples[0])

Examples/GenerativeAISample/FunctionCallingSample/Screens/FunctionCallingScreen.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// REMOVED: `import GoogleGenerativeAI` -- was not used in this file
16+
1517
import GenerativeAIUIComponents
16-
import GoogleGenerativeAI
1718
import SwiftUI
1819

1920
struct FunctionCallingScreen: View {

0 commit comments

Comments
 (0)