GenerativeModelVertexAITests.swift 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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 FirebaseAppCheckInterop
  15. import FirebaseAuthInterop
  16. import FirebaseCore
  17. import XCTest
  18. @testable import FirebaseAI
  19. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  20. final class GenerativeModelVertexAITests: XCTestCase {
  21. let testPrompt = "What sorts of questions can I ask you?"
  22. let safetyRatingsNegligible: [SafetyRating] = [
  23. .init(
  24. category: .sexuallyExplicit,
  25. probability: .negligible,
  26. probabilityScore: 0.1431877,
  27. severity: .negligible,
  28. severityScore: 0.11027937,
  29. blocked: false
  30. ),
  31. .init(
  32. category: .hateSpeech,
  33. probability: .negligible,
  34. probabilityScore: 0.029035643,
  35. severity: .negligible,
  36. severityScore: 0.05613278,
  37. blocked: false
  38. ),
  39. .init(
  40. category: .harassment,
  41. probability: .negligible,
  42. probabilityScore: 0.087252244,
  43. severity: .negligible,
  44. severityScore: 0.04509957,
  45. blocked: false
  46. ),
  47. .init(
  48. category: .dangerousContent,
  49. probability: .negligible,
  50. probabilityScore: 0.2641685,
  51. severity: .negligible,
  52. severityScore: 0.082253955,
  53. blocked: false
  54. ),
  55. ].sorted()
  56. let safetyRatingsInvalidIgnored = [
  57. SafetyRating(
  58. category: .hateSpeech,
  59. probability: .negligible,
  60. probabilityScore: 0.00039444832,
  61. severity: .negligible,
  62. severityScore: 0.0,
  63. blocked: false
  64. ),
  65. SafetyRating(
  66. category: .dangerousContent,
  67. probability: .negligible,
  68. probabilityScore: 0.0010654529,
  69. severity: .negligible,
  70. severityScore: 0.0049325973,
  71. blocked: false
  72. ),
  73. SafetyRating(
  74. category: .harassment,
  75. probability: .negligible,
  76. probabilityScore: 0.00026658305,
  77. severity: .negligible,
  78. severityScore: 0.0,
  79. blocked: false
  80. ),
  81. SafetyRating(
  82. category: .sexuallyExplicit,
  83. probability: .negligible,
  84. probabilityScore: 0.0013701695,
  85. severity: .negligible,
  86. severityScore: 0.07626295,
  87. blocked: false
  88. ),
  89. // Ignored Invalid Safety Ratings: {},{},{},{}
  90. ].sorted()
  91. let testModelName = "test-model"
  92. let testModelResourceName =
  93. "projects/test-project-id/locations/test-location/publishers/google/models/test-model"
  94. let apiConfig = FirebaseAI.defaultVertexAIAPIConfig
  95. let vertexSubdirectory = "mock-responses/vertexai"
  96. var urlSession: URLSession!
  97. var model: GenerativeModel!
  98. override func setUp() async throws {
  99. let configuration = URLSessionConfiguration.default
  100. configuration.protocolClasses = [MockURLProtocol.self]
  101. urlSession = try XCTUnwrap(URLSession(configuration: configuration))
  102. model = GenerativeModel(
  103. modelName: testModelName,
  104. modelResourceName: testModelResourceName,
  105. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  106. apiConfig: apiConfig,
  107. tools: nil,
  108. requestOptions: RequestOptions(),
  109. urlSession: urlSession
  110. )
  111. }
  112. override func tearDown() {
  113. MockURLProtocol.requestHandler = nil
  114. }
  115. // MARK: - Generate Content
  116. func testGenerateContent_success_basicReplyLong() async throws {
  117. MockURLProtocol
  118. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  119. forResource: "unary-success-basic-reply-long",
  120. withExtension: "json",
  121. subdirectory: vertexSubdirectory
  122. )
  123. let response = try await model.generateContent(testPrompt)
  124. XCTAssertEqual(response.candidates.count, 1)
  125. let candidate = try XCTUnwrap(response.candidates.first)
  126. let finishReason = try XCTUnwrap(candidate.finishReason)
  127. XCTAssertEqual(finishReason, .stop)
  128. XCTAssertEqual(candidate.safetyRatings.count, 4)
  129. XCTAssertEqual(candidate.content.parts.count, 1)
  130. let part = try XCTUnwrap(candidate.content.parts.first)
  131. let partText = try XCTUnwrap(part as? TextPart).text
  132. XCTAssertTrue(partText.hasPrefix("1. **Use Freshly Ground Coffee**:"))
  133. XCTAssertEqual(response.text, partText)
  134. XCTAssertEqual(response.functionCalls, [])
  135. XCTAssertEqual(response.inlineDataParts, [])
  136. }
  137. func testGenerateContent_success_basicReplyShort() async throws {
  138. MockURLProtocol
  139. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  140. forResource: "unary-success-basic-reply-short",
  141. withExtension: "json",
  142. subdirectory: vertexSubdirectory
  143. )
  144. let response = try await model.generateContent(testPrompt)
  145. XCTAssertEqual(response.candidates.count, 1)
  146. let candidate = try XCTUnwrap(response.candidates.first)
  147. let finishReason = try XCTUnwrap(candidate.finishReason)
  148. XCTAssertEqual(finishReason, .stop)
  149. XCTAssertEqual(candidate.safetyRatings.sorted(), safetyRatingsNegligible)
  150. XCTAssertEqual(candidate.content.parts.count, 1)
  151. let part = try XCTUnwrap(candidate.content.parts.first)
  152. let textPart = try XCTUnwrap(part as? TextPart)
  153. XCTAssertEqual(textPart.text, "Mountain View, California")
  154. XCTAssertEqual(response.text, textPart.text)
  155. XCTAssertEqual(response.functionCalls, [])
  156. }
  157. func testGenerateContent_success_basicReplyFullUsageMetadata() async throws {
  158. MockURLProtocol
  159. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  160. forResource: "unary-success-basic-response-long-usage-metadata",
  161. withExtension: "json",
  162. subdirectory: vertexSubdirectory
  163. )
  164. let response = try await model.generateContent(testPrompt)
  165. XCTAssertEqual(response.candidates.count, 1)
  166. let candidate = try XCTUnwrap(response.candidates.first)
  167. let finishReason = try XCTUnwrap(candidate.finishReason)
  168. XCTAssertEqual(finishReason, .stop)
  169. let usageMetadata = try XCTUnwrap(response.usageMetadata)
  170. XCTAssertEqual(usageMetadata.promptTokensDetails.count, 2)
  171. XCTAssertEqual(usageMetadata.promptTokensDetails[0].modality, .image)
  172. XCTAssertEqual(usageMetadata.promptTokensDetails[0].tokenCount, 1806)
  173. XCTAssertEqual(usageMetadata.promptTokensDetails[1].modality, .text)
  174. XCTAssertEqual(usageMetadata.promptTokensDetails[1].tokenCount, 76)
  175. XCTAssertEqual(usageMetadata.candidatesTokensDetails.count, 1)
  176. XCTAssertEqual(usageMetadata.candidatesTokensDetails[0].modality, .text)
  177. XCTAssertEqual(usageMetadata.candidatesTokensDetails[0].tokenCount, 76)
  178. }
  179. func testGenerateContent_success_citations() async throws {
  180. MockURLProtocol
  181. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  182. forResource: "unary-success-citations",
  183. withExtension: "json",
  184. subdirectory: vertexSubdirectory
  185. )
  186. let expectedPublicationDate = DateComponents(
  187. calendar: Calendar(identifier: .gregorian),
  188. year: 2019,
  189. month: 5,
  190. day: 10
  191. )
  192. let response = try await model.generateContent(testPrompt)
  193. XCTAssertEqual(response.candidates.count, 1)
  194. let candidate = try XCTUnwrap(response.candidates.first)
  195. XCTAssertEqual(candidate.content.parts.count, 1)
  196. XCTAssertEqual(response.text, "Some information cited from an external source")
  197. let citationMetadata = try XCTUnwrap(candidate.citationMetadata)
  198. XCTAssertEqual(citationMetadata.citations.count, 3)
  199. let citationSource1 = try XCTUnwrap(citationMetadata.citations[0])
  200. XCTAssertEqual(citationSource1.uri, "https://www.example.com/some-citation-1")
  201. XCTAssertEqual(citationSource1.startIndex, 0)
  202. XCTAssertEqual(citationSource1.endIndex, 128)
  203. XCTAssertNil(citationSource1.title)
  204. XCTAssertNil(citationSource1.license)
  205. XCTAssertNil(citationSource1.publicationDate)
  206. let citationSource2 = try XCTUnwrap(citationMetadata.citations[1])
  207. XCTAssertEqual(citationSource2.title, "some-citation-2")
  208. XCTAssertEqual(citationSource2.publicationDate, expectedPublicationDate)
  209. XCTAssertEqual(citationSource2.startIndex, 130)
  210. XCTAssertEqual(citationSource2.endIndex, 265)
  211. XCTAssertNil(citationSource2.uri)
  212. XCTAssertNil(citationSource2.license)
  213. let citationSource3 = try XCTUnwrap(citationMetadata.citations[2])
  214. XCTAssertEqual(citationSource3.uri, "https://www.example.com/some-citation-3")
  215. XCTAssertEqual(citationSource3.startIndex, 272)
  216. XCTAssertEqual(citationSource3.endIndex, 431)
  217. XCTAssertEqual(citationSource3.license, "mit")
  218. XCTAssertNil(citationSource3.title)
  219. XCTAssertNil(citationSource3.publicationDate)
  220. }
  221. func testGenerateContent_success_quoteReply() async throws {
  222. MockURLProtocol
  223. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  224. forResource: "unary-success-quote-reply",
  225. withExtension: "json",
  226. subdirectory: vertexSubdirectory
  227. )
  228. let response = try await model.generateContent(testPrompt)
  229. XCTAssertEqual(response.candidates.count, 1)
  230. let candidate = try XCTUnwrap(response.candidates.first)
  231. let finishReason = try XCTUnwrap(candidate.finishReason)
  232. XCTAssertEqual(finishReason, .stop)
  233. XCTAssertEqual(candidate.safetyRatings.count, 4)
  234. XCTAssertEqual(candidate.content.parts.count, 1)
  235. let part = try XCTUnwrap(candidate.content.parts.first)
  236. let textPart = try XCTUnwrap(part as? TextPart)
  237. XCTAssertTrue(textPart.text.hasPrefix("Google"))
  238. XCTAssertEqual(response.text, textPart.text)
  239. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  240. XCTAssertNil(promptFeedback.blockReason)
  241. XCTAssertEqual(promptFeedback.safetyRatings.count, 4)
  242. }
  243. func testGenerateContent_success_unknownEnum_safetyRatings() async throws {
  244. let expectedSafetyRatings = [
  245. SafetyRating(
  246. category: .harassment,
  247. probability: .medium,
  248. probabilityScore: 0.0,
  249. severity: .init(rawValue: "HARM_SEVERITY_UNSPECIFIED"),
  250. severityScore: 0.0,
  251. blocked: false
  252. ),
  253. SafetyRating(
  254. category: .dangerousContent,
  255. probability: SafetyRating.HarmProbability(rawValue: "FAKE_NEW_HARM_PROBABILITY"),
  256. probabilityScore: 0.0,
  257. severity: .init(rawValue: "HARM_SEVERITY_UNSPECIFIED"),
  258. severityScore: 0.0,
  259. blocked: false
  260. ),
  261. SafetyRating(
  262. category: HarmCategory(rawValue: "FAKE_NEW_HARM_CATEGORY"),
  263. probability: .high,
  264. probabilityScore: 0.0,
  265. severity: .init(rawValue: "HARM_SEVERITY_UNSPECIFIED"),
  266. severityScore: 0.0,
  267. blocked: false
  268. ),
  269. ]
  270. MockURLProtocol
  271. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  272. forResource: "unary-success-unknown-enum-safety-ratings",
  273. withExtension: "json",
  274. subdirectory: vertexSubdirectory
  275. )
  276. let response = try await model.generateContent(testPrompt)
  277. XCTAssertEqual(response.text, "Some text")
  278. XCTAssertEqual(response.candidates.first?.safetyRatings, expectedSafetyRatings)
  279. XCTAssertEqual(response.promptFeedback?.safetyRatings, expectedSafetyRatings)
  280. }
  281. func testGenerateContent_success_prefixedModelName() async throws {
  282. MockURLProtocol
  283. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  284. forResource: "unary-success-basic-reply-short",
  285. withExtension: "json",
  286. subdirectory: vertexSubdirectory
  287. )
  288. let model = GenerativeModel(
  289. modelName: testModelName,
  290. modelResourceName: testModelResourceName,
  291. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  292. apiConfig: apiConfig,
  293. tools: nil,
  294. requestOptions: RequestOptions(),
  295. urlSession: urlSession
  296. )
  297. _ = try await model.generateContent(testPrompt)
  298. }
  299. func testGenerateContent_success_functionCall_emptyArguments() async throws {
  300. MockURLProtocol
  301. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  302. forResource: "unary-success-function-call-empty-arguments",
  303. withExtension: "json",
  304. subdirectory: vertexSubdirectory
  305. )
  306. let response = try await model.generateContent(testPrompt)
  307. XCTAssertEqual(response.candidates.count, 1)
  308. let candidate = try XCTUnwrap(response.candidates.first)
  309. XCTAssertEqual(candidate.content.parts.count, 1)
  310. let part = try XCTUnwrap(candidate.content.parts.first)
  311. guard let functionCall = part as? FunctionCallPart else {
  312. XCTFail("Part is not a FunctionCall.")
  313. return
  314. }
  315. XCTAssertEqual(functionCall.name, "current_time")
  316. XCTAssertTrue(functionCall.args.isEmpty)
  317. XCTAssertEqual(response.functionCalls, [functionCall])
  318. }
  319. func testGenerateContent_success_functionCall_noArguments() async throws {
  320. MockURLProtocol
  321. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  322. forResource: "unary-success-function-call-no-arguments",
  323. withExtension: "json",
  324. subdirectory: vertexSubdirectory
  325. )
  326. let response = try await model.generateContent(testPrompt)
  327. XCTAssertEqual(response.candidates.count, 1)
  328. let candidate = try XCTUnwrap(response.candidates.first)
  329. XCTAssertEqual(candidate.content.parts.count, 1)
  330. let part = try XCTUnwrap(candidate.content.parts.first)
  331. guard let functionCall = part as? FunctionCallPart else {
  332. XCTFail("Part is not a FunctionCall.")
  333. return
  334. }
  335. XCTAssertEqual(functionCall.name, "current_time")
  336. XCTAssertTrue(functionCall.args.isEmpty)
  337. XCTAssertEqual(response.functionCalls, [functionCall])
  338. }
  339. func testGenerateContent_success_functionCall_withArguments() async throws {
  340. MockURLProtocol
  341. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  342. forResource: "unary-success-function-call-with-arguments",
  343. withExtension: "json",
  344. subdirectory: vertexSubdirectory
  345. )
  346. let response = try await model.generateContent(testPrompt)
  347. XCTAssertEqual(response.candidates.count, 1)
  348. let candidate = try XCTUnwrap(response.candidates.first)
  349. XCTAssertEqual(candidate.content.parts.count, 1)
  350. let part = try XCTUnwrap(candidate.content.parts.first)
  351. guard let functionCall = part as? FunctionCallPart else {
  352. XCTFail("Part is not a FunctionCall.")
  353. return
  354. }
  355. XCTAssertEqual(functionCall.name, "sum")
  356. XCTAssertEqual(functionCall.args.count, 2)
  357. let argX = try XCTUnwrap(functionCall.args["x"])
  358. XCTAssertEqual(argX, .number(4))
  359. let argY = try XCTUnwrap(functionCall.args["y"])
  360. XCTAssertEqual(argY, .number(5))
  361. XCTAssertEqual(response.functionCalls, [functionCall])
  362. }
  363. func testGenerateContent_success_functionCall_parallelCalls() async throws {
  364. MockURLProtocol
  365. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  366. forResource: "unary-success-function-call-parallel-calls",
  367. withExtension: "json",
  368. subdirectory: vertexSubdirectory
  369. )
  370. let response = try await model.generateContent(testPrompt)
  371. XCTAssertEqual(response.candidates.count, 1)
  372. let candidate = try XCTUnwrap(response.candidates.first)
  373. XCTAssertEqual(candidate.content.parts.count, 3)
  374. let functionCalls = response.functionCalls
  375. XCTAssertEqual(functionCalls.count, 3)
  376. }
  377. func testGenerateContent_success_functionCall_mixedContent() async throws {
  378. MockURLProtocol
  379. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  380. forResource: "unary-success-function-call-mixed-content",
  381. withExtension: "json",
  382. subdirectory: vertexSubdirectory
  383. )
  384. let response = try await model.generateContent(testPrompt)
  385. XCTAssertEqual(response.candidates.count, 1)
  386. let candidate = try XCTUnwrap(response.candidates.first)
  387. XCTAssertEqual(candidate.content.parts.count, 4)
  388. let functionCalls = response.functionCalls
  389. XCTAssertEqual(functionCalls.count, 2)
  390. let text = try XCTUnwrap(response.text)
  391. XCTAssertEqual(text, "The sum of [1, 2, 3] is")
  392. }
  393. func testGenerateContent_success_thinking_thoughtSummary() async throws {
  394. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  395. forResource: "unary-success-thinking-reply-thought-summary",
  396. withExtension: "json",
  397. subdirectory: vertexSubdirectory
  398. )
  399. let response = try await model.generateContent(testPrompt)
  400. XCTAssertEqual(response.candidates.count, 1)
  401. let candidate = try XCTUnwrap(response.candidates.first)
  402. XCTAssertEqual(candidate.finishReason, .stop)
  403. XCTAssertEqual(candidate.content.parts.count, 2)
  404. let thoughtPart = try XCTUnwrap(candidate.content.parts.first as? TextPart)
  405. XCTAssertTrue(thoughtPart.isThought)
  406. XCTAssertTrue(thoughtPart.text.hasPrefix("Right, someone needs the city where Google"))
  407. XCTAssertEqual(response.thoughtSummary, thoughtPart.text)
  408. let textPart = try XCTUnwrap(candidate.content.parts.last as? TextPart)
  409. XCTAssertFalse(textPart.isThought)
  410. XCTAssertEqual(textPart.text, "Mountain View")
  411. XCTAssertEqual(response.text, textPart.text)
  412. }
  413. func testGenerateContent_success_codeExecution() async throws {
  414. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  415. forResource: "unary-success-code-execution",
  416. withExtension: "json",
  417. subdirectory: vertexSubdirectory
  418. )
  419. let response = try await model.generateContent(testPrompt)
  420. XCTAssertEqual(response.candidates.count, 1)
  421. let candidate = try XCTUnwrap(response.candidates.first)
  422. let parts = candidate.content.parts
  423. XCTAssertEqual(candidate.finishReason, .stop)
  424. XCTAssertEqual(parts.count, 4)
  425. let textPart1 = try XCTUnwrap(parts[0] as? TextPart)
  426. XCTAssertFalse(textPart1.isThought)
  427. XCTAssertTrue(textPart1.text.hasPrefix("To find the sum of the first 5 prime numbers"))
  428. let executableCodePart = try XCTUnwrap(parts[1] as? ExecutableCodePart)
  429. XCTAssertFalse(executableCodePart.isThought)
  430. XCTAssertEqual(executableCodePart.language, .python)
  431. XCTAssertTrue(executableCodePart.code.starts(with: "prime_numbers = [2, 3, 5, 7, 11]"))
  432. let codeExecutionResultPart = try XCTUnwrap(parts[2] as? CodeExecutionResultPart)
  433. XCTAssertFalse(codeExecutionResultPart.isThought)
  434. XCTAssertEqual(codeExecutionResultPart.outcome, .ok)
  435. XCTAssertEqual(codeExecutionResultPart.output, "The sum of the first 5 prime numbers is: 28\n")
  436. let textPart2 = try XCTUnwrap(parts[3] as? TextPart)
  437. XCTAssertFalse(textPart2.isThought)
  438. XCTAssertEqual(
  439. textPart2.text, "The sum of the first 5 prime numbers (2, 3, 5, 7, and 11) is 28."
  440. )
  441. }
  442. func testGenerateContent_success_image_invalidSafetyRatingsIgnored() async throws {
  443. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  444. forResource: "unary-success-image-invalid-safety-ratings",
  445. withExtension: "json",
  446. subdirectory: vertexSubdirectory
  447. )
  448. let response = try await model.generateContent(testPrompt)
  449. XCTAssertEqual(response.candidates.count, 1)
  450. let candidate = try XCTUnwrap(response.candidates.first)
  451. XCTAssertEqual(candidate.content.parts.count, 1)
  452. XCTAssertEqual(candidate.safetyRatings.sorted(), safetyRatingsInvalidIgnored)
  453. let inlineDataParts = response.inlineDataParts
  454. XCTAssertEqual(inlineDataParts.count, 1)
  455. let imagePart = try XCTUnwrap(inlineDataParts.first)
  456. XCTAssertEqual(imagePart.mimeType, "image/png")
  457. XCTAssertGreaterThan(imagePart.data.count, 0)
  458. }
  459. func testGenerateContent_success_image_emptyPartIgnored() async throws {
  460. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  461. forResource: "unary-success-empty-part",
  462. withExtension: "json",
  463. subdirectory: vertexSubdirectory
  464. )
  465. let response = try await model.generateContent(testPrompt)
  466. XCTAssertEqual(response.candidates.count, 1)
  467. let candidate = try XCTUnwrap(response.candidates.first)
  468. XCTAssertEqual(candidate.content.parts.count, 2)
  469. let inlineDataParts = response.inlineDataParts
  470. XCTAssertEqual(inlineDataParts.count, 1)
  471. let imagePart = try XCTUnwrap(inlineDataParts.first)
  472. XCTAssertEqual(imagePart.mimeType, "image/png")
  473. XCTAssertGreaterThan(imagePart.data.count, 0)
  474. let text = try XCTUnwrap(response.text)
  475. XCTAssertTrue(text.starts(with: "I can certainly help you with that"))
  476. }
  477. func testGenerateContent_appCheck_validToken() async throws {
  478. let appCheckToken = "test-valid-token"
  479. model = GenerativeModel(
  480. modelName: testModelName,
  481. modelResourceName: testModelResourceName,
  482. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  483. appCheck: AppCheckInteropFake(token: appCheckToken)
  484. ),
  485. apiConfig: apiConfig,
  486. tools: nil,
  487. requestOptions: RequestOptions(),
  488. urlSession: urlSession
  489. )
  490. MockURLProtocol
  491. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  492. forResource: "unary-success-basic-reply-short",
  493. withExtension: "json",
  494. subdirectory: vertexSubdirectory,
  495. appCheckToken: appCheckToken
  496. )
  497. _ = try await model.generateContent(testPrompt)
  498. }
  499. func testGenerateContent_appCheck_validToken_limitedUse() async throws {
  500. let appCheckToken = "test-valid-token"
  501. model = GenerativeModel(
  502. modelName: testModelName,
  503. modelResourceName: testModelResourceName,
  504. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  505. appCheck: AppCheckInteropFake(token: appCheckToken),
  506. useLimitedUseAppCheckTokens: true
  507. ),
  508. apiConfig: apiConfig,
  509. tools: nil,
  510. requestOptions: RequestOptions(),
  511. urlSession: urlSession
  512. )
  513. MockURLProtocol
  514. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  515. forResource: "unary-success-basic-reply-short",
  516. withExtension: "json",
  517. subdirectory: vertexSubdirectory,
  518. appCheckToken: "limited_use_\(appCheckToken)"
  519. )
  520. _ = try await model.generateContent(testPrompt)
  521. }
  522. func testGenerateContent_dataCollectionOff() async throws {
  523. let appCheckToken = "test-valid-token"
  524. model = GenerativeModel(
  525. modelName: testModelName,
  526. modelResourceName: testModelResourceName,
  527. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  528. appCheck: AppCheckInteropFake(token: appCheckToken), privateAppID: true
  529. ),
  530. apiConfig: apiConfig,
  531. tools: nil,
  532. requestOptions: RequestOptions(),
  533. urlSession: urlSession
  534. )
  535. MockURLProtocol
  536. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  537. forResource: "unary-success-basic-reply-short",
  538. withExtension: "json",
  539. subdirectory: vertexSubdirectory,
  540. appCheckToken: appCheckToken,
  541. dataCollection: false
  542. )
  543. _ = try await model.generateContent(testPrompt)
  544. }
  545. func testGenerateContent_appCheck_tokenRefreshError() async throws {
  546. model = GenerativeModel(
  547. modelName: testModelName,
  548. modelResourceName: testModelResourceName,
  549. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  550. appCheck: AppCheckInteropFake(error: AppCheckErrorFake())
  551. ),
  552. apiConfig: apiConfig,
  553. tools: nil,
  554. requestOptions: RequestOptions(),
  555. urlSession: urlSession
  556. )
  557. MockURLProtocol
  558. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  559. forResource: "unary-success-basic-reply-short",
  560. withExtension: "json",
  561. subdirectory: vertexSubdirectory,
  562. appCheckToken: AppCheckInteropFake.placeholderTokenValue
  563. )
  564. _ = try await model.generateContent(testPrompt)
  565. }
  566. func testGenerateContent_auth_validAuthToken() async throws {
  567. let authToken = "test-valid-token"
  568. model = GenerativeModel(
  569. modelName: testModelName,
  570. modelResourceName: testModelResourceName,
  571. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  572. auth: AuthInteropFake(token: authToken)
  573. ),
  574. apiConfig: apiConfig,
  575. tools: nil,
  576. requestOptions: RequestOptions(),
  577. urlSession: urlSession
  578. )
  579. MockURLProtocol
  580. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  581. forResource: "unary-success-basic-reply-short",
  582. withExtension: "json",
  583. subdirectory: vertexSubdirectory,
  584. authToken: authToken
  585. )
  586. _ = try await model.generateContent(testPrompt)
  587. }
  588. func testGenerateContent_auth_nilAuthToken() async throws {
  589. model = GenerativeModel(
  590. modelName: testModelName,
  591. modelResourceName: testModelResourceName,
  592. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(auth: AuthInteropFake(token: nil)),
  593. apiConfig: apiConfig,
  594. tools: nil,
  595. requestOptions: RequestOptions(),
  596. urlSession: urlSession
  597. )
  598. MockURLProtocol
  599. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  600. forResource: "unary-success-basic-reply-short",
  601. withExtension: "json",
  602. subdirectory: vertexSubdirectory,
  603. authToken: nil
  604. )
  605. _ = try await model.generateContent(testPrompt)
  606. }
  607. func testGenerateContent_auth_authTokenRefreshError() async throws {
  608. model = GenerativeModel(
  609. modelName: testModelName,
  610. modelResourceName: testModelResourceName,
  611. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  612. auth: AuthInteropFake(error: AuthErrorFake())
  613. ),
  614. apiConfig: apiConfig,
  615. tools: nil,
  616. requestOptions: RequestOptions(),
  617. urlSession: urlSession
  618. )
  619. MockURLProtocol
  620. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  621. forResource: "unary-success-basic-reply-short",
  622. withExtension: "json",
  623. subdirectory: vertexSubdirectory,
  624. authToken: nil
  625. )
  626. do {
  627. _ = try await model.generateContent(testPrompt)
  628. XCTFail("Should throw internalError(AuthErrorFake); no error.")
  629. } catch GenerateContentError.internalError(_ as AuthErrorFake) {
  630. //
  631. } catch {
  632. XCTFail("Should throw internalError(AuthErrorFake); error thrown: \(error)")
  633. }
  634. }
  635. func testGenerateContent_usageMetadata() async throws {
  636. MockURLProtocol
  637. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  638. forResource: "unary-success-basic-reply-short",
  639. withExtension: "json",
  640. subdirectory: vertexSubdirectory
  641. )
  642. let response = try await model.generateContent(testPrompt)
  643. let usageMetadata = try XCTUnwrap(response.usageMetadata)
  644. XCTAssertEqual(usageMetadata.promptTokenCount, 6)
  645. XCTAssertEqual(usageMetadata.candidatesTokenCount, 7)
  646. XCTAssertEqual(usageMetadata.totalTokenCount, 13)
  647. XCTAssertEqual(usageMetadata.promptTokensDetails.isEmpty, true)
  648. XCTAssertEqual(usageMetadata.candidatesTokensDetails.isEmpty, true)
  649. }
  650. func testGenerateContent_groundingMetadata() async throws {
  651. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  652. forResource: "unary-success-google-search-grounding",
  653. withExtension: "json",
  654. subdirectory: vertexSubdirectory
  655. )
  656. let response = try await model.generateContent(testPrompt)
  657. XCTAssertEqual(response.candidates.count, 1)
  658. let candidate = try XCTUnwrap(response.candidates.first)
  659. let groundingMetadata = try XCTUnwrap(candidate.groundingMetadata)
  660. XCTAssertEqual(groundingMetadata.webSearchQueries, ["current weather in London"])
  661. XCTAssertNotNil(groundingMetadata.searchEntryPoint)
  662. XCTAssertNotNil(groundingMetadata.searchEntryPoint?.renderedContent)
  663. XCTAssertEqual(groundingMetadata.groundingChunks.count, 2)
  664. let firstChunk = try XCTUnwrap(groundingMetadata.groundingChunks.first?.web)
  665. XCTAssertEqual(firstChunk.title, "accuweather.com")
  666. XCTAssertNotNil(firstChunk.uri)
  667. XCTAssertNil(firstChunk.domain) // Domain is not supported by Google AI backend
  668. XCTAssertEqual(groundingMetadata.groundingSupports.count, 3)
  669. let firstSupport = try XCTUnwrap(groundingMetadata.groundingSupports.first)
  670. let segment = try XCTUnwrap(firstSupport.segment)
  671. XCTAssertEqual(segment.text, "The current weather in London, United Kingdom is cloudy.")
  672. XCTAssertEqual(segment.startIndex, 0)
  673. XCTAssertEqual(segment.partIndex, 0)
  674. XCTAssertEqual(segment.endIndex, 56)
  675. XCTAssertEqual(firstSupport.groundingChunkIndices, [0])
  676. }
  677. func testGenerateContent_withGoogleSearchTool() async throws {
  678. let model = GenerativeModel(
  679. modelName: testModelName,
  680. modelResourceName: testModelResourceName,
  681. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  682. apiConfig: apiConfig,
  683. tools: [.googleSearch()],
  684. requestOptions: RequestOptions(),
  685. urlSession: urlSession
  686. )
  687. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  688. forResource: "unary-success-basic-reply-short",
  689. withExtension: "json",
  690. subdirectory: vertexSubdirectory
  691. )
  692. _ = try await model.generateContent(testPrompt)
  693. }
  694. func testGenerateContent_failure_invalidAPIKey() async throws {
  695. let expectedStatusCode = 400
  696. MockURLProtocol
  697. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  698. forResource: "unary-failure-api-key",
  699. withExtension: "json",
  700. subdirectory: vertexSubdirectory,
  701. statusCode: expectedStatusCode
  702. )
  703. do {
  704. _ = try await model.generateContent(testPrompt)
  705. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  706. } catch let GenerateContentError.internalError(error as BackendError) {
  707. XCTAssertEqual(error.httpResponseCode, 400)
  708. XCTAssertEqual(error.status, .invalidArgument)
  709. XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
  710. XCTAssertTrue(error.localizedDescription.contains(error.message))
  711. XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
  712. XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
  713. let nsError = error as NSError
  714. XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
  715. XCTAssertEqual(nsError.code, error.httpResponseCode)
  716. return
  717. } catch {
  718. XCTFail("Should throw GenerateContentError.internalError(RPCError); error thrown: \(error)")
  719. }
  720. }
  721. func testGenerateContent_failure_firebaseVertexAIAPINotEnabled() async throws {
  722. let expectedStatusCode = 403
  723. MockURLProtocol
  724. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  725. forResource: "unary-failure-firebasevertexai-api-not-enabled",
  726. withExtension: "json",
  727. subdirectory: vertexSubdirectory,
  728. statusCode: expectedStatusCode
  729. )
  730. do {
  731. _ = try await model.generateContent(testPrompt)
  732. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  733. } catch let GenerateContentError.internalError(error as BackendError) {
  734. XCTAssertEqual(error.httpResponseCode, expectedStatusCode)
  735. XCTAssertEqual(error.status, .permissionDenied)
  736. XCTAssertTrue(error.message
  737. .starts(with: "Vertex AI in Firebase API has not been used in project"))
  738. XCTAssertTrue(error.isVertexAIInFirebaseServiceDisabledError())
  739. return
  740. } catch {
  741. XCTFail("Should throw GenerateContentError.internalError(RPCError); error thrown: \(error)")
  742. }
  743. }
  744. func testGenerateContent_failure_emptyContent() async throws {
  745. MockURLProtocol
  746. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  747. forResource: "unary-failure-empty-content",
  748. withExtension: "json",
  749. subdirectory: vertexSubdirectory
  750. )
  751. do {
  752. _ = try await model.generateContent(testPrompt)
  753. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  754. } catch let GenerateContentError
  755. .internalError(underlying: invalidCandidateError as InvalidCandidateError) {
  756. guard case let .emptyContent(underlyingError) = invalidCandidateError else {
  757. XCTFail("Should be an InvalidCandidateError.emptyContent error: \(invalidCandidateError)")
  758. return
  759. }
  760. _ = try XCTUnwrap(underlyingError as? Candidate.EmptyContentError,
  761. "Should be an empty content error: \(underlyingError)")
  762. } catch {
  763. XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
  764. }
  765. }
  766. func testGenerateContent_failure_finishReasonSafety() async throws {
  767. MockURLProtocol
  768. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  769. forResource: "unary-failure-finish-reason-safety",
  770. withExtension: "json",
  771. subdirectory: vertexSubdirectory
  772. )
  773. do {
  774. _ = try await model.generateContent(testPrompt)
  775. XCTFail("Should throw")
  776. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  777. XCTAssertEqual(reason, .safety)
  778. XCTAssertEqual(response.text, "<redacted>")
  779. } catch {
  780. XCTFail("Should throw a responseStoppedEarly")
  781. }
  782. }
  783. func testGenerateContent_failure_finishReasonSafety_noContent() async throws {
  784. MockURLProtocol
  785. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  786. forResource: "unary-failure-finish-reason-safety-no-content",
  787. withExtension: "json",
  788. subdirectory: vertexSubdirectory
  789. )
  790. do {
  791. _ = try await model.generateContent(testPrompt)
  792. XCTFail("Should throw")
  793. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  794. XCTAssertEqual(reason, .safety)
  795. XCTAssertNil(response.text)
  796. } catch {
  797. XCTFail("Should throw a responseStoppedEarly")
  798. }
  799. }
  800. func testGenerateContent_failure_imageRejected() async throws {
  801. let expectedStatusCode = 400
  802. MockURLProtocol
  803. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  804. forResource: "unary-failure-image-rejected",
  805. withExtension: "json",
  806. subdirectory: vertexSubdirectory,
  807. statusCode: 400
  808. )
  809. do {
  810. _ = try await model.generateContent(testPrompt)
  811. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  812. } catch let GenerateContentError.internalError(underlying: rpcError as BackendError) {
  813. XCTAssertEqual(rpcError.status, .invalidArgument)
  814. XCTAssertEqual(rpcError.httpResponseCode, expectedStatusCode)
  815. XCTAssertEqual(rpcError.message, "Request contains an invalid argument.")
  816. } catch {
  817. XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
  818. }
  819. }
  820. func testGenerateContent_failure_promptBlockedSafety() async throws {
  821. MockURLProtocol
  822. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  823. forResource: "unary-failure-prompt-blocked-safety",
  824. withExtension: "json",
  825. subdirectory: vertexSubdirectory
  826. )
  827. do {
  828. _ = try await model.generateContent(testPrompt)
  829. XCTFail("Should throw")
  830. } catch let GenerateContentError.promptBlocked(response) {
  831. XCTAssertNil(response.text)
  832. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  833. XCTAssertEqual(promptFeedback.blockReason, PromptFeedback.BlockReason.safety)
  834. XCTAssertNil(promptFeedback.blockReasonMessage)
  835. } catch {
  836. XCTFail("Should throw a promptBlocked")
  837. }
  838. }
  839. func testGenerateContent_failure_promptBlockedSafetyWithMessage() async throws {
  840. MockURLProtocol
  841. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  842. forResource: "unary-failure-prompt-blocked-safety-with-message",
  843. withExtension: "json",
  844. subdirectory: vertexSubdirectory
  845. )
  846. do {
  847. _ = try await model.generateContent(testPrompt)
  848. XCTFail("Should throw")
  849. } catch let GenerateContentError.promptBlocked(response) {
  850. XCTAssertNil(response.text)
  851. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  852. XCTAssertEqual(promptFeedback.blockReason, PromptFeedback.BlockReason.safety)
  853. XCTAssertEqual(promptFeedback.blockReasonMessage, "Reasons")
  854. } catch {
  855. XCTFail("Should throw a promptBlocked")
  856. }
  857. }
  858. func testGenerateContent_failure_unknownEnum_finishReason() async throws {
  859. MockURLProtocol
  860. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  861. forResource: "unary-failure-unknown-enum-finish-reason",
  862. withExtension: "json",
  863. subdirectory: vertexSubdirectory
  864. )
  865. let unknownFinishReason = FinishReason(rawValue: "FAKE_NEW_FINISH_REASON")
  866. do {
  867. _ = try await model.generateContent(testPrompt)
  868. XCTFail("Should throw")
  869. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  870. XCTAssertEqual(reason, unknownFinishReason)
  871. XCTAssertEqual(response.text, "Some text")
  872. } catch {
  873. XCTFail("Should throw a responseStoppedEarly")
  874. }
  875. }
  876. func testGenerateContent_failure_unknownEnum_promptBlocked() async throws {
  877. MockURLProtocol
  878. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  879. forResource: "unary-failure-unknown-enum-prompt-blocked",
  880. withExtension: "json",
  881. subdirectory: vertexSubdirectory
  882. )
  883. let unknownBlockReason = PromptFeedback.BlockReason(rawValue: "FAKE_NEW_BLOCK_REASON")
  884. do {
  885. _ = try await model.generateContent(testPrompt)
  886. XCTFail("Should throw")
  887. } catch let GenerateContentError.promptBlocked(response) {
  888. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  889. XCTAssertEqual(promptFeedback.blockReason, unknownBlockReason)
  890. } catch {
  891. XCTFail("Should throw a promptBlocked")
  892. }
  893. }
  894. func testGenerateContent_failure_unknownModel() async throws {
  895. let expectedStatusCode = 404
  896. MockURLProtocol
  897. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  898. forResource: "unary-failure-unknown-model",
  899. withExtension: "json",
  900. subdirectory: vertexSubdirectory,
  901. statusCode: 404
  902. )
  903. do {
  904. _ = try await model.generateContent(testPrompt)
  905. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  906. } catch let GenerateContentError.internalError(underlying: rpcError as BackendError) {
  907. XCTAssertEqual(rpcError.status, .notFound)
  908. XCTAssertEqual(rpcError.httpResponseCode, expectedStatusCode)
  909. XCTAssertTrue(rpcError.message.hasPrefix("models/unknown is not found"))
  910. } catch {
  911. XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
  912. }
  913. }
  914. func testGenerateContent_failure_nonHTTPResponse() async throws {
  915. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.nonHTTPRequestHandler()
  916. var responseError: Error?
  917. var content: GenerateContentResponse?
  918. do {
  919. content = try await model.generateContent(testPrompt)
  920. } catch {
  921. responseError = error
  922. }
  923. XCTAssertNil(content)
  924. XCTAssertNotNil(responseError)
  925. let generateContentError = try XCTUnwrap(responseError as? GenerateContentError)
  926. guard case let .internalError(underlyingError) = generateContentError else {
  927. XCTFail("Should be an internal error: \(generateContentError)")
  928. return
  929. }
  930. XCTAssertEqual(underlyingError.localizedDescription, "Response was not an HTTP response.")
  931. let underlyingNSError = underlyingError as NSError
  932. XCTAssertEqual(underlyingNSError.domain, NSURLErrorDomain)
  933. XCTAssertEqual(underlyingNSError.code, URLError.Code.badServerResponse.rawValue)
  934. }
  935. func testGenerateContent_failure_invalidResponse() async throws {
  936. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  937. forResource: "unary-failure-invalid-response",
  938. withExtension: "json",
  939. subdirectory: vertexSubdirectory
  940. )
  941. var responseError: Error?
  942. var content: GenerateContentResponse?
  943. do {
  944. content = try await model.generateContent(testPrompt)
  945. } catch {
  946. responseError = error
  947. }
  948. XCTAssertNil(content)
  949. XCTAssertNotNil(responseError)
  950. let generateContentError = try XCTUnwrap(responseError as? GenerateContentError)
  951. guard case let .internalError(underlyingError) = generateContentError else {
  952. XCTFail("Should be an internal error: \(generateContentError)")
  953. return
  954. }
  955. let decodingError = try XCTUnwrap(underlyingError as? DecodingError)
  956. guard case let .dataCorrupted(context) = decodingError else {
  957. XCTFail("Should be a data corrupted error: \(decodingError)")
  958. return
  959. }
  960. XCTAssert(context.debugDescription.hasPrefix("Failed to decode GenerateContentResponse"))
  961. }
  962. func testGenerateContent_failure_malformedContent() async throws {
  963. MockURLProtocol
  964. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  965. // Note: Although this file does not contain `parts` in `content`, it is not actually
  966. // malformed. The `invalid-field` in the payload could be added, as a non-breaking change to
  967. // the proto API. Therefore, this test checks for the `emptyContent` error instead.
  968. forResource: "unary-failure-malformed-content",
  969. withExtension: "json",
  970. subdirectory: vertexSubdirectory
  971. )
  972. var responseError: Error?
  973. var content: GenerateContentResponse?
  974. do {
  975. content = try await model.generateContent(testPrompt)
  976. } catch {
  977. responseError = error
  978. }
  979. XCTAssertNil(content)
  980. XCTAssertNotNil(responseError)
  981. let generateContentError = try XCTUnwrap(responseError as? GenerateContentError)
  982. guard case let .internalError(underlyingError) = generateContentError else {
  983. XCTFail("Should be an internal error: \(generateContentError)")
  984. return
  985. }
  986. let invalidCandidateError = try XCTUnwrap(underlyingError as? InvalidCandidateError)
  987. guard case let .emptyContent(emptyContentUnderlyingError) = invalidCandidateError else {
  988. XCTFail("Should be an empty content error: \(invalidCandidateError)")
  989. return
  990. }
  991. _ = try XCTUnwrap(
  992. emptyContentUnderlyingError as? Candidate.EmptyContentError,
  993. "Should be an empty content error: \(emptyContentUnderlyingError)"
  994. )
  995. }
  996. func testGenerateContentMissingSafetyRatings() async throws {
  997. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  998. forResource: "unary-success-missing-safety-ratings",
  999. withExtension: "json",
  1000. subdirectory: vertexSubdirectory
  1001. )
  1002. let content = try await model.generateContent(testPrompt)
  1003. let promptFeedback = try XCTUnwrap(content.promptFeedback)
  1004. XCTAssertEqual(promptFeedback.safetyRatings.count, 0)
  1005. XCTAssertEqual(content.text, "This is the generated content.")
  1006. }
  1007. func testGenerateContent_requestOptions_customTimeout() async throws {
  1008. let expectedTimeout = 150.0
  1009. MockURLProtocol
  1010. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1011. forResource: "unary-success-basic-reply-short",
  1012. withExtension: "json",
  1013. subdirectory: vertexSubdirectory,
  1014. timeout: expectedTimeout
  1015. )
  1016. let requestOptions = RequestOptions(timeout: expectedTimeout)
  1017. model = GenerativeModel(
  1018. modelName: testModelName,
  1019. modelResourceName: testModelResourceName,
  1020. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1021. apiConfig: apiConfig,
  1022. tools: nil,
  1023. requestOptions: requestOptions,
  1024. urlSession: urlSession
  1025. )
  1026. let response = try await model.generateContent(testPrompt)
  1027. XCTAssertEqual(response.candidates.count, 1)
  1028. }
  1029. // MARK: - Generate Content (Streaming)
  1030. func testGenerateContentStream_failureInvalidAPIKey() async throws {
  1031. MockURLProtocol
  1032. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1033. forResource: "unary-failure-api-key",
  1034. withExtension: "json",
  1035. subdirectory: vertexSubdirectory
  1036. )
  1037. do {
  1038. let stream = try model.generateContentStream("Hi")
  1039. for try await _ in stream {
  1040. XCTFail("No content is there, this shouldn't happen.")
  1041. }
  1042. } catch let GenerateContentError.internalError(error as BackendError) {
  1043. XCTAssertEqual(error.httpResponseCode, 400)
  1044. XCTAssertEqual(error.status, .invalidArgument)
  1045. XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
  1046. XCTAssertTrue(error.localizedDescription.contains(error.message))
  1047. XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
  1048. XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
  1049. let nsError = error as NSError
  1050. XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
  1051. XCTAssertEqual(nsError.code, error.httpResponseCode)
  1052. return
  1053. }
  1054. XCTFail("Should have caught an error.")
  1055. }
  1056. func testGenerateContentStream_failure_vertexAIInFirebaseAPINotEnabled() async throws {
  1057. let expectedStatusCode = 403
  1058. MockURLProtocol
  1059. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1060. forResource: "unary-failure-firebasevertexai-api-not-enabled",
  1061. withExtension: "json",
  1062. subdirectory: vertexSubdirectory,
  1063. statusCode: expectedStatusCode
  1064. )
  1065. do {
  1066. let stream = try model.generateContentStream(testPrompt)
  1067. for try await _ in stream {
  1068. XCTFail("No content is there, this shouldn't happen.")
  1069. }
  1070. } catch let GenerateContentError.internalError(error as BackendError) {
  1071. XCTAssertEqual(error.httpResponseCode, expectedStatusCode)
  1072. XCTAssertEqual(error.status, .permissionDenied)
  1073. XCTAssertTrue(error.message
  1074. .starts(with: "Vertex AI in Firebase API has not been used in project"))
  1075. XCTAssertTrue(error.isVertexAIInFirebaseServiceDisabledError())
  1076. return
  1077. }
  1078. XCTFail("Should have caught an error.")
  1079. }
  1080. func testGenerateContentStream_failureEmptyContent() async throws {
  1081. MockURLProtocol
  1082. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1083. forResource: "streaming-failure-empty-content",
  1084. withExtension: "txt",
  1085. subdirectory: vertexSubdirectory
  1086. )
  1087. do {
  1088. let stream = try model.generateContentStream("Hi")
  1089. for try await _ in stream {
  1090. XCTFail("No content is there, this shouldn't happen.")
  1091. }
  1092. } catch GenerateContentError.internalError(_ as InvalidCandidateError) {
  1093. // Underlying error is as expected, nothing else to check.
  1094. return
  1095. }
  1096. XCTFail("Should have caught an error.")
  1097. }
  1098. func testGenerateContentStream_failureFinishReasonSafety() async throws {
  1099. MockURLProtocol
  1100. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1101. forResource: "streaming-failure-finish-reason-safety",
  1102. withExtension: "txt",
  1103. subdirectory: vertexSubdirectory
  1104. )
  1105. do {
  1106. let stream = try model.generateContentStream("Hi")
  1107. for try await _ in stream {
  1108. XCTFail("Content shouldn't be shown, this shouldn't happen.")
  1109. }
  1110. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  1111. XCTAssertEqual(reason, .safety)
  1112. let candidate = try XCTUnwrap(response.candidates.first)
  1113. XCTAssertEqual(candidate.finishReason, reason)
  1114. XCTAssertTrue(candidate.safetyRatings.contains { $0.blocked })
  1115. return
  1116. }
  1117. XCTFail("Should have caught an error.")
  1118. }
  1119. func testGenerateContentStream_failurePromptBlockedSafety() async throws {
  1120. MockURLProtocol
  1121. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1122. forResource: "streaming-failure-prompt-blocked-safety",
  1123. withExtension: "txt",
  1124. subdirectory: vertexSubdirectory
  1125. )
  1126. do {
  1127. let stream = try model.generateContentStream("Hi")
  1128. for try await _ in stream {
  1129. XCTFail("Content shouldn't be shown, this shouldn't happen.")
  1130. }
  1131. } catch let GenerateContentError.promptBlocked(response) {
  1132. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  1133. XCTAssertEqual(promptFeedback.blockReason, .safety)
  1134. XCTAssertNil(promptFeedback.blockReasonMessage)
  1135. return
  1136. }
  1137. XCTFail("Should have caught an error.")
  1138. }
  1139. func testGenerateContentStream_failurePromptBlockedSafetyWithMessage() async throws {
  1140. MockURLProtocol
  1141. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1142. forResource: "streaming-failure-prompt-blocked-safety-with-message",
  1143. withExtension: "txt",
  1144. subdirectory: vertexSubdirectory
  1145. )
  1146. do {
  1147. let stream = try model.generateContentStream("Hi")
  1148. for try await _ in stream {
  1149. XCTFail("Content shouldn't be shown, this shouldn't happen.")
  1150. }
  1151. } catch let GenerateContentError.promptBlocked(response) {
  1152. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  1153. XCTAssertEqual(promptFeedback.blockReason, .safety)
  1154. XCTAssertEqual(promptFeedback.blockReasonMessage, "Reasons")
  1155. return
  1156. }
  1157. XCTFail("Should have caught an error.")
  1158. }
  1159. func testGenerateContentStream_failureUnknownFinishEnum() async throws {
  1160. MockURLProtocol
  1161. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1162. forResource: "streaming-failure-unknown-finish-enum",
  1163. withExtension: "txt",
  1164. subdirectory: vertexSubdirectory
  1165. )
  1166. let unknownFinishReason = FinishReason(rawValue: "FAKE_ENUM")
  1167. let stream = try model.generateContentStream("Hi")
  1168. do {
  1169. for try await content in stream {
  1170. XCTAssertNotNil(content.text)
  1171. }
  1172. } catch let GenerateContentError.responseStoppedEarly(reason, _) {
  1173. XCTAssertEqual(reason, unknownFinishReason)
  1174. return
  1175. }
  1176. XCTFail("Should have caught an error.")
  1177. }
  1178. func testGenerateContentStream_successBasicReplyLong() async throws {
  1179. MockURLProtocol
  1180. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1181. forResource: "streaming-success-basic-reply-long",
  1182. withExtension: "txt",
  1183. subdirectory: vertexSubdirectory
  1184. )
  1185. var responses = 0
  1186. let stream = try model.generateContentStream("Hi")
  1187. for try await content in stream {
  1188. XCTAssertNotNil(content.text)
  1189. responses += 1
  1190. }
  1191. XCTAssertEqual(responses, 4)
  1192. }
  1193. func testGenerateContentStream_successBasicReplyShort() async throws {
  1194. MockURLProtocol
  1195. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1196. forResource: "streaming-success-basic-reply-short",
  1197. withExtension: "txt",
  1198. subdirectory: vertexSubdirectory
  1199. )
  1200. var responses = 0
  1201. let stream = try model.generateContentStream("Hi")
  1202. for try await content in stream {
  1203. XCTAssertNotNil(content.text)
  1204. responses += 1
  1205. }
  1206. XCTAssertEqual(responses, 1)
  1207. }
  1208. func testGenerateContentStream_successUnknownSafetyEnum() async throws {
  1209. MockURLProtocol
  1210. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1211. forResource: "streaming-success-unknown-safety-enum",
  1212. withExtension: "txt",
  1213. subdirectory: vertexSubdirectory
  1214. )
  1215. let unknownSafetyRating = SafetyRating(
  1216. category: HarmCategory(rawValue: "HARM_CATEGORY_DANGEROUS_CONTENT_NEW_ENUM"),
  1217. probability: SafetyRating.HarmProbability(rawValue: "NEGLIGIBLE_UNKNOWN_ENUM"),
  1218. probabilityScore: 0.0,
  1219. severity: SafetyRating.HarmSeverity(rawValue: "HARM_SEVERITY_UNSPECIFIED"),
  1220. severityScore: 0.0,
  1221. blocked: false
  1222. )
  1223. var foundUnknownSafetyRating = false
  1224. let stream = try model.generateContentStream("Hi")
  1225. for try await content in stream {
  1226. XCTAssertNotNil(content.text)
  1227. if let ratings = content.candidates.first?.safetyRatings,
  1228. ratings.contains(where: { $0 == unknownSafetyRating }) {
  1229. foundUnknownSafetyRating = true
  1230. }
  1231. }
  1232. XCTAssertTrue(foundUnknownSafetyRating)
  1233. }
  1234. func testGenerateContentStream_successWithCitations() async throws {
  1235. MockURLProtocol
  1236. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1237. forResource: "streaming-success-citations",
  1238. withExtension: "txt",
  1239. subdirectory: vertexSubdirectory
  1240. )
  1241. let expectedPublicationDate = DateComponents(
  1242. calendar: Calendar(identifier: .gregorian),
  1243. year: 2014,
  1244. month: 3,
  1245. day: 30
  1246. )
  1247. let stream = try model.generateContentStream("Hi")
  1248. var citations = [Citation]()
  1249. var responses = [GenerateContentResponse]()
  1250. for try await content in stream {
  1251. responses.append(content)
  1252. XCTAssertNotNil(content.text)
  1253. let candidate = try XCTUnwrap(content.candidates.first)
  1254. if let sources = candidate.citationMetadata?.citations {
  1255. citations.append(contentsOf: sources)
  1256. }
  1257. }
  1258. let lastCandidate = try XCTUnwrap(responses.last?.candidates.first)
  1259. XCTAssertEqual(lastCandidate.finishReason, .stop)
  1260. XCTAssertEqual(citations.count, 6)
  1261. XCTAssertTrue(citations
  1262. .contains {
  1263. $0.startIndex == 0 && $0.endIndex == 128
  1264. && $0.uri == "https://www.example.com/some-citation-1" && $0.title == nil
  1265. && $0.license == nil && $0.publicationDate == nil
  1266. })
  1267. XCTAssertTrue(citations
  1268. .contains {
  1269. $0.startIndex == 130 && $0.endIndex == 265 && $0.uri == nil
  1270. && $0.title == "some-citation-2" && $0.license == nil
  1271. && $0.publicationDate == expectedPublicationDate
  1272. })
  1273. XCTAssertTrue(citations
  1274. .contains {
  1275. $0.startIndex == 272 && $0.endIndex == 431
  1276. && $0.uri == "https://www.example.com/some-citation-3" && $0.title == nil
  1277. && $0.license == "mit" && $0.publicationDate == nil
  1278. })
  1279. XCTAssertFalse(citations.contains { $0.uri?.isEmpty ?? false })
  1280. XCTAssertFalse(citations.contains { $0.title?.isEmpty ?? false })
  1281. XCTAssertFalse(citations.contains { $0.license?.isEmpty ?? false })
  1282. }
  1283. func testGenerateContentStream_successWithThinking_thoughtSummary() async throws {
  1284. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1285. forResource: "streaming-success-thinking-reply-thought-summary",
  1286. withExtension: "txt",
  1287. subdirectory: vertexSubdirectory
  1288. )
  1289. var thoughtSummary = ""
  1290. var text = ""
  1291. let stream = try model.generateContentStream("Hi")
  1292. for try await response in stream {
  1293. let candidate = try XCTUnwrap(response.candidates.first)
  1294. XCTAssertEqual(candidate.content.parts.count, 1)
  1295. let part = try XCTUnwrap(candidate.content.parts.first)
  1296. let textPart = try XCTUnwrap(part as? TextPart)
  1297. if textPart.isThought {
  1298. let newThought = try XCTUnwrap(response.thoughtSummary)
  1299. thoughtSummary.append(newThought)
  1300. } else {
  1301. text.append(textPart.text)
  1302. }
  1303. }
  1304. XCTAssertTrue(thoughtSummary.hasPrefix("**Understanding the Core Question**"))
  1305. XCTAssertTrue(text.hasPrefix("The sky is blue due to a phenomenon"))
  1306. }
  1307. func testGenerateContentStream_success_codeExecution() async throws {
  1308. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1309. forResource: "streaming-success-code-execution",
  1310. withExtension: "txt",
  1311. subdirectory: vertexSubdirectory
  1312. )
  1313. var parts = [any Part]()
  1314. let stream = try model.generateContentStream(testPrompt)
  1315. for try await response in stream {
  1316. if let responseParts = response.candidates.first?.content.parts {
  1317. parts.append(contentsOf: responseParts)
  1318. }
  1319. }
  1320. let thoughtParts = parts.filter { $0.isThought }
  1321. XCTAssertEqual(thoughtParts.count, 0)
  1322. let textParts = parts.filter { $0 is TextPart }
  1323. XCTAssertGreaterThan(textParts.count, 0)
  1324. let executableCodeParts = parts.compactMap { $0 as? ExecutableCodePart }
  1325. XCTAssertEqual(executableCodeParts.count, 1)
  1326. let executableCodePart = try XCTUnwrap(executableCodeParts.first)
  1327. XCTAssertFalse(executableCodePart.isThought)
  1328. XCTAssertEqual(executableCodePart.language, .python)
  1329. XCTAssertTrue(executableCodePart.code.starts(with: "prime_numbers = [2, 3, 5, 7, 11]"))
  1330. let codeExecutionResultParts = parts.compactMap { $0 as? CodeExecutionResultPart }
  1331. XCTAssertEqual(codeExecutionResultParts.count, 1)
  1332. let codeExecutionResultPart = try XCTUnwrap(codeExecutionResultParts.first)
  1333. XCTAssertFalse(codeExecutionResultPart.isThought)
  1334. XCTAssertEqual(codeExecutionResultPart.outcome, .ok)
  1335. XCTAssertEqual(codeExecutionResultPart.output, "The sum of the first 5 prime numbers is: 28\n")
  1336. }
  1337. func testGenerateContentStream_successWithInvalidSafetyRatingsIgnored() async throws {
  1338. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1339. forResource: "streaming-success-image-invalid-safety-ratings",
  1340. withExtension: "txt",
  1341. subdirectory: vertexSubdirectory
  1342. )
  1343. let stream = try model.generateContentStream(testPrompt)
  1344. var responses = [GenerateContentResponse]()
  1345. for try await content in stream {
  1346. responses.append(content)
  1347. }
  1348. let response = try XCTUnwrap(responses.first)
  1349. XCTAssertEqual(response.candidates.count, 1)
  1350. let candidate = try XCTUnwrap(response.candidates.first)
  1351. XCTAssertEqual(candidate.safetyRatings.sorted(), safetyRatingsInvalidIgnored)
  1352. XCTAssertEqual(candidate.content.parts.count, 1)
  1353. let inlineDataParts = response.inlineDataParts
  1354. XCTAssertEqual(inlineDataParts.count, 1)
  1355. let imagePart = try XCTUnwrap(inlineDataParts.first)
  1356. XCTAssertEqual(imagePart.mimeType, "image/png")
  1357. XCTAssertGreaterThan(imagePart.data.count, 0)
  1358. }
  1359. func testGenerateContentStream_appCheck_validToken() async throws {
  1360. let appCheckToken = "test-valid-token"
  1361. model = GenerativeModel(
  1362. modelName: testModelName,
  1363. modelResourceName: testModelResourceName,
  1364. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  1365. appCheck: AppCheckInteropFake(token: appCheckToken)
  1366. ),
  1367. apiConfig: apiConfig,
  1368. tools: nil,
  1369. requestOptions: RequestOptions(),
  1370. urlSession: urlSession
  1371. )
  1372. MockURLProtocol
  1373. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1374. forResource: "streaming-success-basic-reply-short",
  1375. withExtension: "txt",
  1376. subdirectory: vertexSubdirectory,
  1377. appCheckToken: appCheckToken
  1378. )
  1379. let stream = try model.generateContentStream(testPrompt)
  1380. for try await _ in stream {}
  1381. }
  1382. func testGenerateContentStream_appCheck_tokenRefreshError() async throws {
  1383. model = GenerativeModel(
  1384. modelName: testModelName,
  1385. modelResourceName: testModelResourceName,
  1386. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  1387. appCheck: AppCheckInteropFake(error: AppCheckErrorFake())
  1388. ),
  1389. apiConfig: apiConfig,
  1390. tools: nil,
  1391. requestOptions: RequestOptions(),
  1392. urlSession: urlSession
  1393. )
  1394. MockURLProtocol
  1395. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1396. forResource: "streaming-success-basic-reply-short",
  1397. withExtension: "txt",
  1398. subdirectory: vertexSubdirectory,
  1399. appCheckToken: AppCheckInteropFake.placeholderTokenValue
  1400. )
  1401. let stream = try model.generateContentStream(testPrompt)
  1402. for try await _ in stream {}
  1403. }
  1404. func testGenerateContentStream_usageMetadata() async throws {
  1405. MockURLProtocol
  1406. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1407. forResource: "streaming-success-basic-reply-short",
  1408. withExtension: "txt",
  1409. subdirectory: vertexSubdirectory
  1410. )
  1411. var responses = [GenerateContentResponse]()
  1412. let stream = try model.generateContentStream(testPrompt)
  1413. for try await response in stream {
  1414. responses.append(response)
  1415. }
  1416. for (index, response) in responses.enumerated() {
  1417. if index == responses.endIndex - 1 {
  1418. let usageMetadata = try XCTUnwrap(response.usageMetadata)
  1419. XCTAssertEqual(usageMetadata.promptTokenCount, 6)
  1420. XCTAssertEqual(usageMetadata.candidatesTokenCount, 4)
  1421. XCTAssertEqual(usageMetadata.totalTokenCount, 10)
  1422. } else {
  1423. // Only the last streamed response contains usage metadata
  1424. XCTAssertNil(response.usageMetadata)
  1425. }
  1426. }
  1427. }
  1428. func testGenerateContentStream_errorMidStream() async throws {
  1429. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1430. forResource: "streaming-failure-error-mid-stream",
  1431. withExtension: "txt",
  1432. subdirectory: vertexSubdirectory
  1433. )
  1434. var responseCount = 0
  1435. do {
  1436. let stream = try model.generateContentStream("Hi")
  1437. for try await content in stream {
  1438. XCTAssertNotNil(content.text)
  1439. responseCount += 1
  1440. }
  1441. } catch let GenerateContentError.internalError(rpcError as BackendError) {
  1442. XCTAssertEqual(rpcError.httpResponseCode, 499)
  1443. XCTAssertEqual(rpcError.status, .cancelled)
  1444. // Check the content count is correct.
  1445. XCTAssertEqual(responseCount, 2)
  1446. return
  1447. }
  1448. XCTFail("Expected an internalError with an RPCError.")
  1449. }
  1450. func testGenerateContentStream_nonHTTPResponse() async throws {
  1451. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.nonHTTPRequestHandler()
  1452. let stream = try model.generateContentStream("Hi")
  1453. do {
  1454. for try await content in stream {
  1455. XCTFail("Unexpected content in stream: \(content)")
  1456. }
  1457. } catch let GenerateContentError.internalError(underlying) {
  1458. XCTAssertEqual(underlying.localizedDescription, "Response was not an HTTP response.")
  1459. return
  1460. }
  1461. XCTFail("Expected an internal error.")
  1462. }
  1463. func testGenerateContentStream_invalidResponse() async throws {
  1464. MockURLProtocol
  1465. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1466. forResource: "streaming-failure-invalid-json",
  1467. withExtension: "txt",
  1468. subdirectory: vertexSubdirectory
  1469. )
  1470. let stream = try model.generateContentStream(testPrompt)
  1471. do {
  1472. for try await content in stream {
  1473. XCTFail("Unexpected content in stream: \(content)")
  1474. }
  1475. } catch let GenerateContentError.internalError(underlying as DecodingError) {
  1476. guard case let .dataCorrupted(context) = underlying else {
  1477. XCTFail("Should be a data corrupted error: \(underlying)")
  1478. return
  1479. }
  1480. XCTAssert(context.debugDescription.hasPrefix("Failed to decode GenerateContentResponse"))
  1481. return
  1482. }
  1483. XCTFail("Expected an internal error.")
  1484. }
  1485. func testGenerateContentStream_malformedContent() async throws {
  1486. MockURLProtocol
  1487. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1488. // Note: Although this file does not contain `parts` in `content`, it is not actually
  1489. // malformed. The `invalid-field` in the payload could be added, as a non-breaking change to
  1490. // the proto API. Therefore, this test checks for the `emptyContent` error instead.
  1491. forResource: "streaming-failure-malformed-content",
  1492. withExtension: "txt",
  1493. subdirectory: vertexSubdirectory
  1494. )
  1495. let stream = try model.generateContentStream(testPrompt)
  1496. do {
  1497. for try await content in stream {
  1498. XCTFail("Unexpected content in stream: \(content)")
  1499. }
  1500. } catch let GenerateContentError.internalError(underlyingError as InvalidCandidateError) {
  1501. guard case let .emptyContent(contentError) = underlyingError else {
  1502. XCTFail("Should be an empty content error: \(underlyingError)")
  1503. return
  1504. }
  1505. XCTAssert(contentError is Candidate.EmptyContentError)
  1506. return
  1507. }
  1508. XCTFail("Expected an internal decoding error.")
  1509. }
  1510. func testGenerateContentStream_requestOptions_customTimeout() async throws {
  1511. let expectedTimeout = 150.0
  1512. MockURLProtocol
  1513. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1514. forResource: "streaming-success-basic-reply-short",
  1515. withExtension: "txt",
  1516. subdirectory: vertexSubdirectory,
  1517. timeout: expectedTimeout
  1518. )
  1519. let requestOptions = RequestOptions(timeout: expectedTimeout)
  1520. model = GenerativeModel(
  1521. modelName: testModelName,
  1522. modelResourceName: testModelResourceName,
  1523. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1524. apiConfig: apiConfig,
  1525. tools: nil,
  1526. requestOptions: requestOptions,
  1527. urlSession: urlSession
  1528. )
  1529. var responses = 0
  1530. let stream = try model.generateContentStream(testPrompt)
  1531. for try await content in stream {
  1532. XCTAssertNotNil(content.text)
  1533. responses += 1
  1534. }
  1535. XCTAssertEqual(responses, 1)
  1536. }
  1537. // MARK: - Count Tokens
  1538. func testCountTokens_succeeds() async throws {
  1539. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1540. forResource: "unary-success-total-tokens",
  1541. withExtension: "json",
  1542. subdirectory: vertexSubdirectory
  1543. )
  1544. let response = try await model.countTokens("Why is the sky blue?")
  1545. XCTAssertEqual(response.totalTokens, 6)
  1546. }
  1547. func testCountTokens_succeeds_detailed() async throws {
  1548. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1549. forResource: "unary-success-detailed-token-response",
  1550. withExtension: "json",
  1551. subdirectory: vertexSubdirectory
  1552. )
  1553. let response = try await model.countTokens("Why is the sky blue?")
  1554. XCTAssertEqual(response.totalTokens, 1837)
  1555. XCTAssertEqual(response.promptTokensDetails.count, 2)
  1556. XCTAssertEqual(response.promptTokensDetails[0].modality, .image)
  1557. XCTAssertEqual(response.promptTokensDetails[0].tokenCount, 1806)
  1558. XCTAssertEqual(response.promptTokensDetails[1].modality, .text)
  1559. XCTAssertEqual(response.promptTokensDetails[1].tokenCount, 31)
  1560. }
  1561. func testCountTokens_succeeds_allOptions() async throws {
  1562. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1563. forResource: "unary-success-total-tokens",
  1564. withExtension: "json",
  1565. subdirectory: vertexSubdirectory
  1566. )
  1567. let generationConfig = GenerationConfig(
  1568. temperature: 0.5,
  1569. topP: 0.9,
  1570. topK: 3,
  1571. candidateCount: 1,
  1572. maxOutputTokens: 1024,
  1573. stopSequences: ["test-stop"],
  1574. responseMIMEType: "text/plain"
  1575. )
  1576. let sumFunction = FunctionDeclaration(
  1577. name: "sum",
  1578. description: "Add two integers.",
  1579. parameters: ["x": .integer(), "y": .integer()]
  1580. )
  1581. let systemInstruction = ModelContent(
  1582. role: "system",
  1583. parts: "You are a calculator. Use the provided tools."
  1584. )
  1585. model = GenerativeModel(
  1586. modelName: testModelName,
  1587. modelResourceName: testModelResourceName,
  1588. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1589. apiConfig: apiConfig,
  1590. generationConfig: generationConfig,
  1591. tools: [Tool(functionDeclarations: [sumFunction])],
  1592. systemInstruction: systemInstruction,
  1593. requestOptions: RequestOptions(),
  1594. urlSession: urlSession
  1595. )
  1596. let response = try await model.countTokens("Why is the sky blue?")
  1597. XCTAssertEqual(response.totalTokens, 6)
  1598. }
  1599. func testCountTokens_succeeds_noBillableCharacters() async throws {
  1600. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1601. forResource: "unary-success-no-billable-characters",
  1602. withExtension: "json",
  1603. subdirectory: vertexSubdirectory
  1604. )
  1605. let response = try await model.countTokens(InlineDataPart(data: Data(), mimeType: "image/jpeg"))
  1606. XCTAssertEqual(response.totalTokens, 258)
  1607. }
  1608. func testCountTokens_modelNotFound() async throws {
  1609. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1610. forResource: "unary-failure-model-not-found", withExtension: "json",
  1611. subdirectory: vertexSubdirectory,
  1612. statusCode: 404
  1613. )
  1614. do {
  1615. _ = try await model.countTokens("Why is the sky blue?")
  1616. XCTFail("Request should not have succeeded.")
  1617. } catch let rpcError as BackendError {
  1618. XCTAssertEqual(rpcError.httpResponseCode, 404)
  1619. XCTAssertEqual(rpcError.status, .notFound)
  1620. XCTAssert(rpcError.message.hasPrefix("models/test-model-name is not found"))
  1621. return
  1622. }
  1623. XCTFail("Expected internal RPCError.")
  1624. }
  1625. func testCountTokens_requestOptions_customTimeout() async throws {
  1626. let expectedTimeout = 150.0
  1627. MockURLProtocol
  1628. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1629. forResource: "unary-success-total-tokens",
  1630. withExtension: "json",
  1631. subdirectory: vertexSubdirectory,
  1632. timeout: expectedTimeout
  1633. )
  1634. let requestOptions = RequestOptions(timeout: expectedTimeout)
  1635. model = GenerativeModel(
  1636. modelName: testModelName,
  1637. modelResourceName: testModelResourceName,
  1638. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1639. apiConfig: apiConfig,
  1640. tools: nil,
  1641. requestOptions: requestOptions,
  1642. urlSession: urlSession
  1643. )
  1644. let response = try await model.countTokens(testPrompt)
  1645. XCTAssertEqual(response.totalTokens, 6)
  1646. }
  1647. }
  1648. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  1649. extension SafetyRating: Swift.Comparable {
  1650. public static func < (lhs: SafetyRating, rhs: SafetyRating) -> Bool {
  1651. return lhs.category.rawValue < rhs.category.rawValue
  1652. }
  1653. }