Skip to content

Commit ee33439

Browse files
committed
[vertexai] File for Schema integration tests
1 parent 99c1466 commit ee33439

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

FirebaseVertexAI/Tests/TestApp/Tests/Integration/GenerateContentIntegrationTests.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -165,31 +165,6 @@ struct GenerateContentIntegrationTests {
165165
#endif // canImport(UIKit)
166166
}
167167

168-
@Test(arguments: InstanceConfig.allConfigsExceptDeveloperV1)
169-
func generateContentSchemaItems(_ config: InstanceConfig) async throws {
170-
let model = VertexAI.componentInstance(config).generativeModel(
171-
modelName: ModelNames.gemini2FlashLite,
172-
generationConfig: GenerationConfig(
173-
responseMIMEType: "application/json",
174-
responseSchema:
175-
.array(
176-
items: .string(description: "The name of the city"),
177-
description: "A list of city names",
178-
minItems: 3,
179-
maxItems: 5
180-
)
181-
),
182-
safetySettings: safetySettings
183-
)
184-
let prompt = "What are the biggest cities in Canada?"
185-
let response = try await model.generateContent(prompt)
186-
let text = try #require(response.text).trimmingCharacters(in: .whitespacesAndNewlines)
187-
let jsonData = try #require(text.data(using: .utf8))
188-
let decodedJSON = try JSONDecoder().decode([String].self, from: jsonData)
189-
#expect(decodedJSON.count >= 3, "Expected at least 3 cities, but got \(decodedJSON.count)")
190-
#expect(decodedJSON.count <= 5, "Expected at most 5 cities, but got \(decodedJSON.count)")
191-
}
192-
193168
// MARK: Streaming Tests
194169

195170
@Test(arguments: InstanceConfig.allConfigs)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2025 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 FirebaseAuth
16+
import FirebaseCore
17+
import FirebaseStorage
18+
import FirebaseVertexAI
19+
import Testing
20+
import VertexAITestApp
21+
22+
#if canImport(UIKit)
23+
import UIKit
24+
#endif // canImport(UIKit)
25+
26+
@testable import struct FirebaseVertexAI.BackendError
27+
28+
@Suite(.serialized)
29+
/// Test the schema fields.
30+
struct SchemaTests {
31+
// Set temperature, topP and topK to lowest allowed values to make responses more deterministic.
32+
let generationConfig = GenerationConfig(temperature: 0.0, topP: 0.0, topK: 1)
33+
let safetySettings = [
34+
SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove),
35+
SafetySetting(harmCategory: .hateSpeech, threshold: .blockLowAndAbove),
36+
SafetySetting(harmCategory: .sexuallyExplicit, threshold: .blockLowAndAbove),
37+
SafetySetting(harmCategory: .dangerousContent, threshold: .blockLowAndAbove),
38+
SafetySetting(harmCategory: .civicIntegrity, threshold: .blockLowAndAbove),
39+
]
40+
// Candidates and total token counts may differ slightly between runs due to whitespace tokens.
41+
let tokenCountAccuracy = 1
42+
43+
let storage: Storage
44+
let userID1: String
45+
46+
init() async throws {
47+
userID1 = try await TestHelpers.getUserID()
48+
storage = Storage.storage()
49+
}
50+
51+
@Test(arguments: InstanceConfig.allConfigsExceptDeveloperV1)
52+
func generateContentSchemaItems(_ config: InstanceConfig) async throws {
53+
let model = VertexAI.componentInstance(config).generativeModel(
54+
modelName: ModelNames.gemini2FlashLite,
55+
generationConfig: GenerationConfig(
56+
responseMIMEType: "application/json",
57+
responseSchema:
58+
.array(
59+
items: .string(description: "The name of the city"),
60+
description: "A list of city names",
61+
minItems: 3,
62+
maxItems: 5
63+
)
64+
),
65+
safetySettings: safetySettings
66+
)
67+
let prompt = "What are the biggest cities in Canada?"
68+
let response = try await model.generateContent(prompt)
69+
let text = try #require(response.text).trimmingCharacters(in: .whitespacesAndNewlines)
70+
let jsonData = try #require(text.data(using: .utf8))
71+
let decodedJSON = try JSONDecoder().decode([String].self, from: jsonData)
72+
#expect(decodedJSON.count >= 3, "Expected at least 3 cities, but got \(decodedJSON.count)")
73+
#expect(decodedJSON.count <= 5, "Expected at most 5 cities, but got \(decodedJSON.count)")
74+
}
75+
}

FirebaseVertexAI/Tests/TestApp/VertexAITestApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
86D77E022D7B63AF003D155D /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86D77E012D7B63AC003D155D /* Constants.swift */; };
2929
86D77E042D7B6C9D003D155D /* InstanceConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86D77E032D7B6C95003D155D /* InstanceConfig.swift */; };
3030
DEF0BB4F2DA74F680093E9F4 /* TestHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF0BB4E2DA74F460093E9F4 /* TestHelpers.swift */; };
31+
DEF0BB512DA9B7450093E9F4 /* SchemaTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEF0BB502DA9B7400093E9F4 /* SchemaTests.swift */; };
3132
/* End PBXBuildFile section */
3233

3334
/* Begin PBXContainerItemProxy section */
@@ -61,6 +62,7 @@
6162
86D77E012D7B63AC003D155D /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
6263
86D77E032D7B6C95003D155D /* InstanceConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceConfig.swift; sourceTree = "<group>"; };
6364
DEF0BB4E2DA74F460093E9F4 /* TestHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHelpers.swift; sourceTree = "<group>"; };
65+
DEF0BB502DA9B7400093E9F4 /* SchemaTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SchemaTests.swift; sourceTree = "<group>"; };
6466
/* End PBXFileReference section */
6567

6668
/* Begin PBXFrameworksBuildPhase section */
@@ -139,6 +141,7 @@
139141
868A7C572CCC27AF00E449DD /* Integration */ = {
140142
isa = PBXGroup;
141143
children = (
144+
DEF0BB502DA9B7400093E9F4 /* SchemaTests.swift */,
142145
DEF0BB4E2DA74F460093E9F4 /* TestHelpers.swift */,
143146
8689CDCB2D7F8BCF00BF426B /* CountTokensIntegrationTests.swift */,
144147
868A7C4D2CCC1F4700E449DD /* Credentials.swift */,
@@ -292,6 +295,7 @@
292295
files = (
293296
8689CDCC2D7F8BD700BF426B /* CountTokensIntegrationTests.swift in Sources */,
294297
86D77E042D7B6C9D003D155D /* InstanceConfig.swift in Sources */,
298+
DEF0BB512DA9B7450093E9F4 /* SchemaTests.swift in Sources */,
295299
DEF0BB4F2DA74F680093E9F4 /* TestHelpers.swift in Sources */,
296300
868A7C4F2CCC229F00E449DD /* Credentials.swift in Sources */,
297301
864F8F712D4980DD0002EA7E /* ImagenIntegrationTests.swift in Sources */,

0 commit comments

Comments
 (0)