IntegrationTests.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2024 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. import FirebaseCore
  15. import FirebaseVertexAI
  16. import XCTest
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. final class IntegrationTests: XCTestCase {
  19. // Set temperature, topP and topK to lowest allowed values to make responses more deterministic.
  20. let generationConfig = GenerationConfig(temperature: 0.0, topP: 0.0, topK: 1)
  21. var vertex: VertexAI!
  22. var model: GenerativeModel!
  23. override func setUp() async throws {
  24. try XCTSkipIf(ProcessInfo.processInfo.environment["VertexAIRunIntegrationTests"] == nil, """
  25. Vertex AI integration tests skipped; to enable them, set the VertexAIRunIntegrationTests \
  26. environment variable in Xcode or CI jobs.
  27. """)
  28. let plistPath = try XCTUnwrap(Bundle.module.path(
  29. forResource: "GoogleService-Info",
  30. ofType: "plist"
  31. ))
  32. let options = try XCTUnwrap(FirebaseOptions(contentsOfFile: plistPath))
  33. FirebaseApp.configure(options: options)
  34. vertex = VertexAI.vertexAI()
  35. model = vertex.generativeModel(
  36. modelName: "gemini-1.5-flash",
  37. generationConfig: generationConfig
  38. )
  39. }
  40. override func tearDown() async throws {
  41. if let app = FirebaseApp.app() {
  42. await app.delete()
  43. }
  44. }
  45. // MARK: - Generate Content
  46. func testGenerateContent() async throws {
  47. let prompt = "Where is Google headquarters located? Answer with the city name only."
  48. let response = try await model.generateContent(prompt)
  49. let text = try XCTUnwrap(response.text).trimmingCharacters(in: .whitespacesAndNewlines)
  50. XCTAssertEqual(text, "Mountain View")
  51. }
  52. // MARK: - Count Tokens
  53. func testCountTokens() async throws {
  54. let prompt = "Why is the sky blue?"
  55. let response = try await model.countTokens(prompt)
  56. XCTAssertEqual(response.totalTokens, 6)
  57. XCTAssertEqual(response.totalBillableCharacters, 16)
  58. }
  59. }