|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import FirebaseAI |
| 16 | +import FirebaseCore |
| 17 | +import XCTest |
| 18 | +#if canImport(AppKit) |
| 19 | + import AppKit // For NSImage extensions. |
| 20 | +#elseif canImport(UIKit) |
| 21 | + import UIKit // For UIImage extensions. |
| 22 | +#endif |
| 23 | + |
| 24 | +@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) |
| 25 | +final class APITests: XCTestCase { |
| 26 | + func codeSamples() async throws { |
| 27 | + let app = FirebaseApp.app() |
| 28 | + let config = GenerationConfig(temperature: 0.2, |
| 29 | + topP: 0.1, |
| 30 | + topK: 16, |
| 31 | + candidateCount: 4, |
| 32 | + maxOutputTokens: 256, |
| 33 | + stopSequences: ["..."], |
| 34 | + responseMIMEType: "text/plain") |
| 35 | + let filters = [SafetySetting(harmCategory: .dangerousContent, threshold: .blockOnlyHigh)] |
| 36 | + let systemInstruction = ModelContent( |
| 37 | + role: "system", |
| 38 | + parts: TextPart("Talk like a pirate.") |
| 39 | + ) |
| 40 | + |
| 41 | + let requestOptions = RequestOptions() |
| 42 | + let _ = RequestOptions(timeout: 30.0) |
| 43 | + |
| 44 | + // Instantiate Firebase AI SDK - Default App |
| 45 | + let firebaseAI = FirebaseAI.firebaseAI() |
| 46 | + let _ = FirebaseAI.firebaseAI(backend: .googleAI()) |
| 47 | + let _ = FirebaseAI.firebaseAI(backend: .vertexAI()) |
| 48 | + let _ = FirebaseAI.firebaseAI(backend: .vertexAI(location: "my-location")) |
| 49 | + |
| 50 | + // Instantiate Firebase AI SDK - Custom App |
| 51 | + let _ = FirebaseAI.firebaseAI(app: app!) |
| 52 | + let _ = FirebaseAI.firebaseAI(app: app!, backend: .googleAI()) |
| 53 | + let _ = FirebaseAI.firebaseAI(app: app!, backend: .vertexAI()) |
| 54 | + let _ = FirebaseAI.firebaseAI(app: app!, backend: .vertexAI(location: "my-location")) |
| 55 | + |
| 56 | + // Permutations without optional arguments. |
| 57 | + |
| 58 | + let _ = firebaseAI.generativeModel(modelName: "gemini-2.0-flash") |
| 59 | + |
| 60 | + let _ = firebaseAI.generativeModel( |
| 61 | + modelName: "gemini-2.0-flash", |
| 62 | + safetySettings: filters |
| 63 | + ) |
| 64 | + |
| 65 | + let _ = firebaseAI.generativeModel( |
| 66 | + modelName: "gemini-2.0-flash", |
| 67 | + generationConfig: config |
| 68 | + ) |
| 69 | + |
| 70 | + let _ = firebaseAI.generativeModel( |
| 71 | + modelName: "gemini-2.0-flash", |
| 72 | + systemInstruction: systemInstruction |
| 73 | + ) |
| 74 | + |
| 75 | + // All arguments passed. |
| 76 | + let model = firebaseAI.generativeModel( |
| 77 | + modelName: "gemini-2.0-flash", |
| 78 | + generationConfig: config, // Optional |
| 79 | + safetySettings: filters, // Optional |
| 80 | + systemInstruction: systemInstruction, // Optional |
| 81 | + requestOptions: requestOptions // Optional |
| 82 | + ) |
| 83 | + |
| 84 | + // Full Typed Usage |
| 85 | + let pngData = Data() // .... |
| 86 | + let contents = [ModelContent( |
| 87 | + role: "user", |
| 88 | + parts: [ |
| 89 | + TextPart("Is it a cat?"), |
| 90 | + InlineDataPart(data: pngData, mimeType: "image/png"), |
| 91 | + ] |
| 92 | + )] |
| 93 | + |
| 94 | + do { |
| 95 | + let response = try await model.generateContent(contents) |
| 96 | + print(response.text ?? "Couldn't get text... check status") |
| 97 | + } catch { |
| 98 | + print("Error generating content: \(error)") |
| 99 | + } |
| 100 | + |
| 101 | + // Content input combinations. |
| 102 | + let _ = try await model.generateContent("Constant String") |
| 103 | + let str = "String Variable" |
| 104 | + let _ = try await model.generateContent(str) |
| 105 | + let _ = try await model.generateContent([str]) |
| 106 | + let _ = try await model.generateContent(str, "abc", "def") |
| 107 | + let _ = try await model.generateContent( |
| 108 | + str, |
| 109 | + FileDataPart(uri: "gs://test-bucket/image.jpg", mimeType: "image/jpeg") |
| 110 | + ) |
| 111 | + #if canImport(UIKit) |
| 112 | + _ = try await model.generateContent(UIImage()) |
| 113 | + _ = try await model.generateContent([UIImage()]) |
| 114 | + _ = try await model.generateContent([str, UIImage(), TextPart(str)]) |
| 115 | + _ = try await model.generateContent(str, UIImage(), "def", UIImage()) |
| 116 | + _ = try await model.generateContent([str, UIImage(), "def", UIImage()]) |
| 117 | + _ = try await model.generateContent([ModelContent(parts: "def", UIImage()), |
| 118 | + ModelContent(parts: "def", UIImage())]) |
| 119 | + #elseif canImport(AppKit) |
| 120 | + _ = try await model.generateContent(NSImage()) |
| 121 | + _ = try await model.generateContent([NSImage()]) |
| 122 | + _ = try await model.generateContent(str, NSImage(), "def", NSImage()) |
| 123 | + _ = try await model.generateContent([str, NSImage(), "def", NSImage()]) |
| 124 | + #endif |
| 125 | + |
| 126 | + // PartsRepresentable combinations. |
| 127 | + let _ = ModelContent(parts: [TextPart(str)]) |
| 128 | + let _ = ModelContent(role: "model", parts: [TextPart(str)]) |
| 129 | + let _ = ModelContent(parts: "Constant String") |
| 130 | + let _ = ModelContent(parts: str) |
| 131 | + let _ = ModelContent(parts: [str]) |
| 132 | + let _ = ModelContent(parts: [str, InlineDataPart(data: Data(), mimeType: "foo")]) |
| 133 | + #if canImport(UIKit) |
| 134 | + _ = ModelContent(role: "user", parts: UIImage()) |
| 135 | + _ = ModelContent(role: "user", parts: [UIImage()]) |
| 136 | + _ = ModelContent(parts: [str, UIImage()]) |
| 137 | + // Note: without explicitly specifying`: [any PartsRepresentable]` this will fail to compile |
| 138 | + // below with "Cannot convert value of type `[Any]` to expected type `[any Part]`. |
| 139 | + let representable2: [any PartsRepresentable] = [str, UIImage()] |
| 140 | + _ = ModelContent(parts: representable2) |
| 141 | + _ = ModelContent(parts: [str, UIImage(), TextPart(str)]) |
| 142 | + #elseif canImport(AppKit) |
| 143 | + _ = ModelContent(role: "user", parts: NSImage()) |
| 144 | + _ = ModelContent(role: "user", parts: [NSImage()]) |
| 145 | + _ = ModelContent(parts: [str, NSImage()]) |
| 146 | + // Note: without explicitly specifying`: [any PartsRepresentable]` this will fail to compile |
| 147 | + // below with "Cannot convert value of type `[Any]` to expected type `[any Part]`. |
| 148 | + let representable2: [any PartsRepresentable] = [str, NSImage()] |
| 149 | + _ = ModelContent(parts: representable2) |
| 150 | + _ = ModelContent(parts: [str, NSImage(), TextPart(str)]) |
| 151 | + #endif |
| 152 | + |
| 153 | + // countTokens API |
| 154 | + let _: CountTokensResponse = try await model.countTokens("What color is the Sky?") |
| 155 | + #if canImport(UIKit) |
| 156 | + let _: CountTokensResponse = try await model.countTokens("What color is the Sky?", |
| 157 | + UIImage()) |
| 158 | + let _: CountTokensResponse = try await model.countTokens([ |
| 159 | + ModelContent(parts: "What color is the Sky?", UIImage()), |
| 160 | + ModelContent(parts: UIImage(), "What color is the Sky?", UIImage()), |
| 161 | + ]) |
| 162 | + #endif |
| 163 | + |
| 164 | + // Chat |
| 165 | + _ = model.startChat() |
| 166 | + _ = model.startChat(history: [ModelContent(parts: "abc")]) |
| 167 | + } |
| 168 | + |
| 169 | + // Public API tests for GenerateContentResponse. |
| 170 | + func generateContentResponseAPI() { |
| 171 | + let response = GenerateContentResponse(candidates: []) |
| 172 | + |
| 173 | + let _: [Candidate] = response.candidates |
| 174 | + let _: PromptFeedback? = response.promptFeedback |
| 175 | + |
| 176 | + // Usage Metadata |
| 177 | + guard let usageMetadata = response.usageMetadata else { fatalError() } |
| 178 | + let _: Int = usageMetadata.promptTokenCount |
| 179 | + let _: Int = usageMetadata.candidatesTokenCount |
| 180 | + let _: Int = usageMetadata.totalTokenCount |
| 181 | + |
| 182 | + // Computed Properties |
| 183 | + let _: String? = response.text |
| 184 | + let _: [FunctionCallPart] = response.functionCalls |
| 185 | + } |
| 186 | +} |
0 commit comments