ChatTests.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. import FirebaseCore
  15. import Foundation
  16. import XCTest
  17. @testable import FirebaseAI
  18. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  19. final class ChatTests: XCTestCase {
  20. let modelName = "test-model-name"
  21. let modelResourceName = "projects/my-project/locations/us-central1/models/test-model-name"
  22. var urlSession: URLSession!
  23. override func setUp() {
  24. let configuration = URLSessionConfiguration.default
  25. configuration.protocolClasses = [MockURLProtocol.self]
  26. urlSession = URLSession(configuration: configuration)
  27. }
  28. override func tearDown() {
  29. MockURLProtocol.requestHandler = nil
  30. }
  31. func testMergingText() async throws {
  32. let bundle = BundleTestUtil.bundle()
  33. let fileURL = try XCTUnwrap(bundle.url(
  34. forResource: "streaming-success-basic-reply-parts",
  35. withExtension: "txt",
  36. subdirectory: "mock-responses/vertexai"
  37. ))
  38. // Skip tests using MockURLProtocol on watchOS; unsupported in watchOS 2 and later, see
  39. // https://developer.apple.com/documentation/foundation/urlprotocol for details.
  40. #if os(watchOS)
  41. throw XCTSkip("Custom URL protocols are unsupported in watchOS 2 and later.")
  42. #else // os(watchOS)
  43. MockURLProtocol.requestHandler = { request in
  44. let response = HTTPURLResponse(
  45. url: request.url!,
  46. statusCode: 200,
  47. httpVersion: nil,
  48. headerFields: nil
  49. )!
  50. return (response, fileURL.lines)
  51. }
  52. let app = FirebaseApp(instanceWithName: "testApp",
  53. options: FirebaseOptions(googleAppID: "ignore",
  54. gcmSenderID: "ignore"))
  55. let model = GenerativeModel(
  56. modelName: modelName,
  57. modelResourceName: modelResourceName,
  58. firebaseInfo: FirebaseInfo(
  59. projectID: "my-project-id",
  60. apiKey: "API_KEY",
  61. firebaseAppID: "My app ID",
  62. firebaseApp: app
  63. ),
  64. apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
  65. tools: nil,
  66. requestOptions: RequestOptions(),
  67. urlSession: urlSession
  68. )
  69. let chat = Chat(model: model, history: [])
  70. let input = "Test input"
  71. let stream = try chat.sendMessageStream(input)
  72. // Ensure the values are parsed correctly
  73. for try await value in stream {
  74. XCTAssertNotNil(value.text)
  75. }
  76. XCTAssertEqual(chat.history.count, 2)
  77. let part = try XCTUnwrap(chat.history[0].parts[0])
  78. let textPart = try XCTUnwrap(part as? TextPart)
  79. XCTAssertEqual(textPart.text, input)
  80. let finalText = "1 2 3 4 5 6 7 8"
  81. let assembledExpectation = ModelContent(role: "model", parts: finalText)
  82. XCTAssertEqual(chat.history[1], assembledExpectation)
  83. #endif // os(watchOS)
  84. }
  85. func testSendMessage_unary_appendsHistory() async throws {
  86. let expectedInput = "Test input"
  87. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  88. forResource: "unary-success-basic-reply-short",
  89. withExtension: "json",
  90. subdirectory: "mock-responses/googleai"
  91. )
  92. let model = GenerativeModel(
  93. modelName: modelName,
  94. modelResourceName: modelResourceName,
  95. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  96. apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
  97. tools: nil,
  98. requestOptions: RequestOptions(),
  99. urlSession: urlSession
  100. )
  101. let chat = model.startChat()
  102. // Pre-condition: History should be empty.
  103. XCTAssertTrue(chat.history.isEmpty)
  104. let response = try await chat.sendMessage(expectedInput)
  105. XCTAssertNotNil(response.text)
  106. let text = try XCTUnwrap(response.text)
  107. XCTAssertFalse(text.isEmpty)
  108. // Post-condition: History should have the user's message and the model's response.
  109. XCTAssertEqual(chat.history.count, 2)
  110. let userInput = try XCTUnwrap(chat.history.first)
  111. XCTAssertEqual(userInput.role, "user")
  112. XCTAssertEqual(userInput.parts.count, 1)
  113. let userInputText = try XCTUnwrap(userInput.parts.first as? TextPart)
  114. XCTAssertEqual(userInputText.text, expectedInput)
  115. let modelResponse = try XCTUnwrap(chat.history.last)
  116. XCTAssertEqual(modelResponse.role, "model")
  117. XCTAssertEqual(modelResponse.parts.count, 1)
  118. let modelResponseText = try XCTUnwrap(modelResponse.parts.first as? TextPart)
  119. XCTAssertFalse(modelResponseText.text.isEmpty)
  120. }
  121. func testSendMessageStream_error_doesNotAppendHistory() async throws {
  122. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  123. forResource: "streaming-failure-finish-reason-safety",
  124. withExtension: "txt",
  125. subdirectory: "mock-responses/vertexai"
  126. )
  127. let model = GenerativeModel(
  128. modelName: modelName,
  129. modelResourceName: modelResourceName,
  130. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  131. apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
  132. tools: nil,
  133. requestOptions: RequestOptions(),
  134. urlSession: urlSession
  135. )
  136. let chat = model.startChat()
  137. let input = "Test input"
  138. // Pre-condition: History should be empty.
  139. XCTAssertTrue(chat.history.isEmpty)
  140. do {
  141. let stream = try chat.sendMessageStream(input)
  142. for try await _ in stream {
  143. // Consume the stream.
  144. }
  145. XCTFail("Should have thrown a responseStoppedEarly error.")
  146. } catch let GenerateContentError.responseStoppedEarly(reason, _) {
  147. XCTAssertEqual(reason, .safety)
  148. } catch {
  149. XCTFail("Unexpected error thrown: \(error)")
  150. }
  151. // Post-condition: History should still be empty.
  152. XCTAssertEqual(chat.history.count, 0)
  153. }
  154. func testStartChat_withHistory_initializesCorrectly() async throws {
  155. let history = [
  156. ModelContent(role: "user", parts: "Question 1"),
  157. ModelContent(role: "model", parts: "Answer 1"),
  158. ]
  159. let model = GenerativeModel(
  160. modelName: modelName,
  161. modelResourceName: modelResourceName,
  162. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  163. apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
  164. tools: nil,
  165. requestOptions: RequestOptions(),
  166. urlSession: urlSession
  167. )
  168. let chat = model.startChat(history: history)
  169. XCTAssertEqual(chat.history.count, 2)
  170. XCTAssertEqual(chat.history, history)
  171. }
  172. }