Skip to content

[Example] Migration from GoogleGenerativeAI to FirebaseAI #226

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions .github/workflows/samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
matrix:
# Test build with debug and release configs (whether or not DEBUG is set and optimization level)
build: [build, archive]
os: [macos-13]
os: [macos-14]
include:
- os: macos-13
xcode: Xcode_15.0.1
- os: macos-14
xcode: Xcode_16.2
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
36 changes: 0 additions & 36 deletions Examples/GenerativeAISample/APIKey/APIKey.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// REMOVED: `import GoogleGenerativeAI` -- was not used in this file
import GenerativeAIUIComponents
import GoogleGenerativeAI
import SwiftUI

struct ConversationScreen: View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import FirebaseAI // REPLACED: `GoogleGenerativeAI` with `FirebaseAI`
import Foundation
import GoogleGenerativeAI
import UIKit

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

init() {
model = GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: APIKey.default)
// BEFORE
// model = GenerativeModel(name: "gemini-1.5-flash-latest", apiKey: APIKey.default)

// AFTER
model = FirebaseAI.firebaseAI().generativeModel(modelName: "gemini-2.0-flash")

chat = model.startChat()
}

Expand Down Expand Up @@ -79,7 +84,7 @@ class ConversationViewModel: ObservableObject {
messages.append(systemMessage)

do {
let responseStream = chat.sendMessageStream(text)
let responseStream = try chat.sendMessageStream(text) // ADDED: `try`
for try await chunk in responseStream {
messages[messages.count - 1].pending = false
if let text = chunk.text {
Expand Down
120 changes: 58 additions & 62 deletions Examples/GenerativeAISample/ChatSample/Views/ErrorDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import GoogleGenerativeAI
import FirebaseAI // REPLACED: `GoogleGenerativeAI` with `FirebaseAI`
import MarkdownUI
import SwiftUI

extension SafetySetting.HarmCategory: CustomStringConvertible {
extension HarmCategory: CustomStringConvertible { // REMOVED: `SafetySetting.`
public var description: String {
switch self {
case .dangerousContent: "Dangerous content"
case .harassment: "Harassment"
case .hateSpeech: "Hate speech"
case .sexuallyExplicit: "Sexually explicit"
case .unknown: "Unknown"
case .unspecified: "Unspecified"
case .civicIntegrity: "Civic integrity" // ADDED
// REMOVED: case .unspecified: "Unspecified"
default: "Unknown" // REPLACED `case .unknown`
}
}
}
Expand All @@ -36,8 +37,7 @@ extension SafetyRating.HarmProbability: CustomStringConvertible {
case .low: "Low"
case .medium: "Medium"
case .negligible: "Negligible"
case .unknown: "Unknown"
case .unspecified: "Unspecified"
default: "Unknown" // REPLACED: `case .unknown`
}
}
}
Expand Down Expand Up @@ -102,6 +102,17 @@ struct ErrorDetailsView: View {
value: underlyingError.localizedDescription)
}

// ADDED
case let GenerateContentError.promptImageContentError(underlying: underlyingError):
Section("Error Type") {
Text("Creating prompt image content failed")
}

Section("Details") {
SubtitleFormRow(title: "Error description",
value: underlyingError.localizedDescription)
}

case let GenerateContentError.promptBlocked(response: generateContentResponse):
Section("Error Type") {
Text("Your prompt was blocked")
Expand Down Expand Up @@ -142,36 +153,9 @@ struct ErrorDetailsView: View {
SafetyRatingsSection(ratings: ratings)
}

case GenerateContentError.invalidAPIKey:
Section("Error Type") {
Text("Invalid API Key")
}

Section("Details") {
SubtitleFormRow(title: "Error description", value: error.localizedDescription)
SubtitleMarkdownFormRow(
title: "Help",
value: """
Please provide a valid value for `API_KEY` in the `GenerativeAI-Info.plist` file.
"""
)
}

case GenerateContentError.unsupportedUserLocation:
Section("Error Type") {
Text("Unsupported User Location")
}
// REMOVED: `GenerateContentError.invalidAPIKey`

Section("Details") {
SubtitleFormRow(title: "Error description", value: error.localizedDescription)
SubtitleMarkdownFormRow(
title: "Help",
value: """
The API is unsupported in your location (country / territory); please see the list of
[available regions](https://ai.google.dev/available_regions#available_regions).
"""
)
}
// REMOVED: `GenerateContentError.unsupportedUserLocation`

default:
Section("Error Type") {
Expand All @@ -192,23 +176,32 @@ struct ErrorDetailsView: View {
#Preview("Response Stopped Early") {
let error = GenerateContentError.responseStoppedEarly(
reason: .maxTokens,
response: GenerateContentResponse(candidates: [
CandidateResponse(content: ModelContent(role: "model", [
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
SafetyRating(category: .dangerousContent, probability: .high),
SafetyRating(category: .harassment, probability: .low),
SafetyRating(category: .hateSpeech, probability: .low),
SafetyRating(category: .sexuallyExplicit, probability: .low),
response: GenerateContentResponse(
candidates: [
Candidate( // REPLACED: `CandidateResponse` with `Candidate`
content: ModelContent(role: "model", parts: [ // ADDED: `parts: `
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
// ADDED: `probabilityScore`, `severity`, `severityScore`, and `blocked`
SafetyRating(category: .dangerousContent, probability: .high, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .harassment, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .hateSpeech, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .sexuallyExplicit, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
],
finishReason: FinishReason.maxTokens,
citationMetadata: nil
),
],
finishReason: FinishReason.maxTokens,
citationMetadata: nil),
],
promptFeedback: nil)
promptFeedback: nil
)
)

return ErrorDetailsView(error: error)
Expand All @@ -217,17 +210,24 @@ struct ErrorDetailsView: View {
#Preview("Prompt Blocked") {
let error = GenerateContentError.promptBlocked(
response: GenerateContentResponse(candidates: [
CandidateResponse(content: ModelContent(role: "model", [
// REPLACED: `CandidateResponse` with `Candidate`
Candidate(content: ModelContent(role: "model", parts: [ // ADDED: `parts: `
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
SafetyRating(category: .dangerousContent, probability: .high),
SafetyRating(category: .harassment, probability: .low),
SafetyRating(category: .hateSpeech, probability: .low),
SafetyRating(category: .sexuallyExplicit, probability: .low),
// ADDED: `probabilityScore`, `severity`, `severityScore`, and `blocked`
SafetyRating(category: .dangerousContent, probability: .high, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .harassment, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .hateSpeech, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .sexuallyExplicit, probability: .low,
probabilityScore: 0.0, severity: .negligible, severityScore: 0.0,
blocked: false),
],
finishReason: FinishReason.other,
citationMetadata: nil),
Expand All @@ -238,10 +238,6 @@ struct ErrorDetailsView: View {
return ErrorDetailsView(error: error)
}

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

#Preview("Unsupported User Location") {
ErrorDetailsView(error: GenerateContentError.unsupportedUserLocation)
}
// REMOVED: Preview for `GenerateContentError.unsupportedUserLocation`
43 changes: 26 additions & 17 deletions Examples/GenerativeAISample/ChatSample/Views/ErrorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import GoogleGenerativeAI
import FirebaseAI // REPLACED: `GoogleGenerativeAI` with `FirebaseAI`
import SwiftUI

struct ErrorView: View {
Expand All @@ -36,23 +36,32 @@ struct ErrorView: View {
#Preview {
NavigationView {
let errorPromptBlocked = GenerateContentError.promptBlocked(
response: GenerateContentResponse(candidates: [
CandidateResponse(content: ModelContent(role: "model", [
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
SafetyRating(category: .dangerousContent, probability: .high),
SafetyRating(category: .harassment, probability: .low),
SafetyRating(category: .hateSpeech, probability: .low),
SafetyRating(category: .sexuallyExplicit, probability: .low),
response: GenerateContentResponse(
candidates: [
Candidate( // REPLACED: `CandidateResponse` with `Candidate`
content: ModelContent(role: "model", parts: [ // ADDED: `parts: `
"""
A _hypothetical_ model response.
Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
""",
]),
safetyRatings: [
// ADDED: `probabilityScore`, `severity`, `severityScore`, `blocked`
SafetyRating(category: .dangerousContent, probability: .high, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .harassment, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .hateSpeech, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
SafetyRating(category: .sexuallyExplicit, probability: .low, probabilityScore: 0.0,
severity: .negligible, severityScore: 0.0, blocked: false),
],
finishReason: FinishReason.other,
citationMetadata: nil
),
],
finishReason: FinishReason.other,
citationMetadata: nil),
],
promptFeedback: nil)
promptFeedback: nil
)
)
List {
MessageView(message: ChatMessage.samples[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// REMOVED: `import GoogleGenerativeAI` -- was not used in this file

import GenerativeAIUIComponents
import GoogleGenerativeAI
import SwiftUI

struct FunctionCallingScreen: View {
Expand Down
Loading