GenerativeModelVertexAITests.swift 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  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 FirebaseAILogic
  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. let usageMetadata = try XCTUnwrap(response.usageMetadata)
  442. XCTAssertEqual(usageMetadata.toolUsePromptTokenCount, 371)
  443. }
  444. func testGenerateContent_success_urlContext() async throws {
  445. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  446. forResource: "unary-success-url-context",
  447. withExtension: "json",
  448. subdirectory: vertexSubdirectory
  449. )
  450. let response = try await model.generateContent(testPrompt)
  451. XCTAssertEqual(response.candidates.count, 1)
  452. let candidate = try XCTUnwrap(response.candidates.first)
  453. let urlContextMetadata = try XCTUnwrap(candidate.urlContextMetadata)
  454. XCTAssertEqual(urlContextMetadata.urlMetadata.count, 1)
  455. let urlMetadata = try XCTUnwrap(urlContextMetadata.urlMetadata.first)
  456. let retrievedURL = try XCTUnwrap(urlMetadata.retrievedURL)
  457. XCTAssertEqual(
  458. retrievedURL,
  459. URL(string: "https://berkshirehathaway.com")
  460. )
  461. XCTAssertEqual(urlMetadata.retrievalStatus, .success)
  462. let usageMetadata = try XCTUnwrap(response.usageMetadata)
  463. XCTAssertEqual(usageMetadata.toolUsePromptTokenCount, 34)
  464. XCTAssertEqual(usageMetadata.thoughtsTokenCount, 36)
  465. }
  466. func testGenerateContent_success_urlContext_mixedValidity() async throws {
  467. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  468. forResource: "unary-success-url-context-mixed-validity",
  469. withExtension: "json",
  470. subdirectory: vertexSubdirectory
  471. )
  472. let response = try await model.generateContent(testPrompt)
  473. let candidate = try XCTUnwrap(response.candidates.first)
  474. let urlContextMetadata = try XCTUnwrap(candidate.urlContextMetadata)
  475. XCTAssertEqual(urlContextMetadata.urlMetadata.count, 3)
  476. let paywallURLMetadata = urlContextMetadata.urlMetadata[0]
  477. XCTAssertEqual(paywallURLMetadata.retrievalStatus, .error)
  478. let successURLMetadata = urlContextMetadata.urlMetadata[1]
  479. XCTAssertEqual(successURLMetadata.retrievalStatus, .success)
  480. let errorURLMetadata = urlContextMetadata.urlMetadata[2]
  481. XCTAssertEqual(errorURLMetadata.retrievalStatus, .error)
  482. }
  483. func testGenerateContent_success_urlContext_retrievedURLPresentOnErrorStatus() async throws {
  484. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  485. forResource: "unary-success-url-context-missing-retrievedurl",
  486. withExtension: "json",
  487. subdirectory: vertexSubdirectory
  488. )
  489. let response = try await model.generateContent(testPrompt)
  490. let candidate = try XCTUnwrap(response.candidates.first)
  491. let urlContextMetadata = try XCTUnwrap(candidate.urlContextMetadata)
  492. let urlMetadata = try XCTUnwrap(urlContextMetadata.urlMetadata.first)
  493. let retrievedURL = try XCTUnwrap(urlMetadata.retrievedURL)
  494. XCTAssertEqual(retrievedURL.absoluteString, "https://example.com/8")
  495. XCTAssertEqual(urlMetadata.retrievalStatus, .error)
  496. }
  497. func testGenerateContent_success_image_invalidSafetyRatingsIgnored() async throws {
  498. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  499. forResource: "unary-success-image-invalid-safety-ratings",
  500. withExtension: "json",
  501. subdirectory: vertexSubdirectory
  502. )
  503. let response = try await model.generateContent(testPrompt)
  504. XCTAssertEqual(response.candidates.count, 1)
  505. let candidate = try XCTUnwrap(response.candidates.first)
  506. XCTAssertEqual(candidate.content.parts.count, 1)
  507. XCTAssertEqual(candidate.safetyRatings.sorted(), safetyRatingsInvalidIgnored)
  508. let inlineDataParts = response.inlineDataParts
  509. XCTAssertEqual(inlineDataParts.count, 1)
  510. let imagePart = try XCTUnwrap(inlineDataParts.first)
  511. XCTAssertEqual(imagePart.mimeType, "image/png")
  512. XCTAssertGreaterThan(imagePart.data.count, 0)
  513. }
  514. func testGenerateContent_success_image_emptyPartIgnored() async throws {
  515. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  516. forResource: "unary-success-empty-part",
  517. withExtension: "json",
  518. subdirectory: vertexSubdirectory
  519. )
  520. let response = try await model.generateContent(testPrompt)
  521. XCTAssertEqual(response.candidates.count, 1)
  522. let candidate = try XCTUnwrap(response.candidates.first)
  523. XCTAssertEqual(candidate.content.parts.count, 2)
  524. let inlineDataParts = response.inlineDataParts
  525. XCTAssertEqual(inlineDataParts.count, 1)
  526. let imagePart = try XCTUnwrap(inlineDataParts.first)
  527. XCTAssertEqual(imagePart.mimeType, "image/png")
  528. XCTAssertGreaterThan(imagePart.data.count, 0)
  529. let text = try XCTUnwrap(response.text)
  530. XCTAssertTrue(text.starts(with: "I can certainly help you with that"))
  531. }
  532. func testGenerateContent_appCheck_validToken() async throws {
  533. let appCheckToken = "test-valid-token"
  534. model = GenerativeModel(
  535. modelName: testModelName,
  536. modelResourceName: testModelResourceName,
  537. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  538. appCheck: AppCheckInteropFake(token: appCheckToken)
  539. ),
  540. apiConfig: apiConfig,
  541. tools: nil,
  542. requestOptions: RequestOptions(),
  543. urlSession: urlSession
  544. )
  545. MockURLProtocol
  546. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  547. forResource: "unary-success-basic-reply-short",
  548. withExtension: "json",
  549. subdirectory: vertexSubdirectory,
  550. appCheckToken: appCheckToken
  551. )
  552. _ = try await model.generateContent(testPrompt)
  553. }
  554. func testGenerateContent_appCheck_validToken_limitedUse() async throws {
  555. let appCheckToken = "test-valid-token"
  556. model = GenerativeModel(
  557. modelName: testModelName,
  558. modelResourceName: testModelResourceName,
  559. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  560. appCheck: AppCheckInteropFake(token: appCheckToken),
  561. useLimitedUseAppCheckTokens: true
  562. ),
  563. apiConfig: apiConfig,
  564. tools: nil,
  565. requestOptions: RequestOptions(),
  566. urlSession: urlSession
  567. )
  568. MockURLProtocol
  569. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  570. forResource: "unary-success-basic-reply-short",
  571. withExtension: "json",
  572. subdirectory: vertexSubdirectory,
  573. appCheckToken: "limited_use_\(appCheckToken)"
  574. )
  575. _ = try await model.generateContent(testPrompt)
  576. }
  577. func testGenerateContent_dataCollectionOff() async throws {
  578. let appCheckToken = "test-valid-token"
  579. model = GenerativeModel(
  580. modelName: testModelName,
  581. modelResourceName: testModelResourceName,
  582. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  583. appCheck: AppCheckInteropFake(token: appCheckToken), privateAppID: true
  584. ),
  585. apiConfig: apiConfig,
  586. tools: nil,
  587. requestOptions: RequestOptions(),
  588. urlSession: urlSession
  589. )
  590. MockURLProtocol
  591. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  592. forResource: "unary-success-basic-reply-short",
  593. withExtension: "json",
  594. subdirectory: vertexSubdirectory,
  595. appCheckToken: appCheckToken,
  596. dataCollection: false
  597. )
  598. _ = try await model.generateContent(testPrompt)
  599. }
  600. func testGenerateContent_appCheck_tokenRefreshError() async throws {
  601. model = GenerativeModel(
  602. modelName: testModelName,
  603. modelResourceName: testModelResourceName,
  604. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  605. appCheck: AppCheckInteropFake(error: AppCheckErrorFake())
  606. ),
  607. apiConfig: apiConfig,
  608. tools: nil,
  609. requestOptions: RequestOptions(),
  610. urlSession: urlSession
  611. )
  612. MockURLProtocol
  613. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  614. forResource: "unary-success-basic-reply-short",
  615. withExtension: "json",
  616. subdirectory: vertexSubdirectory,
  617. appCheckToken: AppCheckInteropFake.placeholderTokenValue
  618. )
  619. _ = try await model.generateContent(testPrompt)
  620. }
  621. func testGenerateContent_auth_validAuthToken() async throws {
  622. let authToken = "test-valid-token"
  623. model = GenerativeModel(
  624. modelName: testModelName,
  625. modelResourceName: testModelResourceName,
  626. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  627. auth: AuthInteropFake(token: authToken)
  628. ),
  629. apiConfig: apiConfig,
  630. tools: nil,
  631. requestOptions: RequestOptions(),
  632. urlSession: urlSession
  633. )
  634. MockURLProtocol
  635. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  636. forResource: "unary-success-basic-reply-short",
  637. withExtension: "json",
  638. subdirectory: vertexSubdirectory,
  639. authToken: authToken
  640. )
  641. _ = try await model.generateContent(testPrompt)
  642. }
  643. func testGenerateContent_auth_nilAuthToken() async throws {
  644. model = GenerativeModel(
  645. modelName: testModelName,
  646. modelResourceName: testModelResourceName,
  647. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(auth: AuthInteropFake(token: nil)),
  648. apiConfig: apiConfig,
  649. tools: nil,
  650. requestOptions: RequestOptions(),
  651. urlSession: urlSession
  652. )
  653. MockURLProtocol
  654. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  655. forResource: "unary-success-basic-reply-short",
  656. withExtension: "json",
  657. subdirectory: vertexSubdirectory,
  658. authToken: nil
  659. )
  660. _ = try await model.generateContent(testPrompt)
  661. }
  662. func testGenerateContent_auth_authTokenRefreshError() async throws {
  663. model = GenerativeModel(
  664. modelName: testModelName,
  665. modelResourceName: testModelResourceName,
  666. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  667. auth: AuthInteropFake(error: AuthErrorFake())
  668. ),
  669. apiConfig: apiConfig,
  670. tools: nil,
  671. requestOptions: RequestOptions(),
  672. urlSession: urlSession
  673. )
  674. MockURLProtocol
  675. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  676. forResource: "unary-success-basic-reply-short",
  677. withExtension: "json",
  678. subdirectory: vertexSubdirectory,
  679. authToken: nil
  680. )
  681. do {
  682. _ = try await model.generateContent(testPrompt)
  683. XCTFail("Should throw internalError(AuthErrorFake); no error.")
  684. } catch GenerateContentError.internalError(_ as AuthErrorFake) {
  685. //
  686. } catch {
  687. XCTFail("Should throw internalError(AuthErrorFake); error thrown: \(error)")
  688. }
  689. }
  690. func testGenerateContent_usageMetadata() async throws {
  691. MockURLProtocol
  692. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  693. forResource: "unary-success-basic-reply-short",
  694. withExtension: "json",
  695. subdirectory: vertexSubdirectory
  696. )
  697. let response = try await model.generateContent(testPrompt)
  698. let usageMetadata = try XCTUnwrap(response.usageMetadata)
  699. XCTAssertEqual(usageMetadata.promptTokenCount, 6)
  700. XCTAssertEqual(usageMetadata.candidatesTokenCount, 7)
  701. XCTAssertEqual(usageMetadata.totalTokenCount, 13)
  702. XCTAssertEqual(usageMetadata.promptTokensDetails.isEmpty, true)
  703. XCTAssertEqual(usageMetadata.candidatesTokensDetails.isEmpty, true)
  704. }
  705. func testGenerateContent_groundingMetadata() async throws {
  706. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  707. forResource: "unary-success-google-search-grounding",
  708. withExtension: "json",
  709. subdirectory: vertexSubdirectory
  710. )
  711. let response = try await model.generateContent(testPrompt)
  712. XCTAssertEqual(response.candidates.count, 1)
  713. let candidate = try XCTUnwrap(response.candidates.first)
  714. let groundingMetadata = try XCTUnwrap(candidate.groundingMetadata)
  715. XCTAssertEqual(groundingMetadata.webSearchQueries, ["current weather in London"])
  716. XCTAssertNotNil(groundingMetadata.searchEntryPoint)
  717. XCTAssertNotNil(groundingMetadata.searchEntryPoint?.renderedContent)
  718. XCTAssertEqual(groundingMetadata.groundingChunks.count, 2)
  719. let firstChunk = try XCTUnwrap(groundingMetadata.groundingChunks.first?.web)
  720. XCTAssertEqual(firstChunk.title, "accuweather.com")
  721. XCTAssertNotNil(firstChunk.uri)
  722. XCTAssertNil(firstChunk.domain) // Domain is not supported by Google AI backend
  723. XCTAssertEqual(groundingMetadata.groundingSupports.count, 3)
  724. let firstSupport = try XCTUnwrap(groundingMetadata.groundingSupports.first)
  725. let segment = try XCTUnwrap(firstSupport.segment)
  726. XCTAssertEqual(segment.text, "The current weather in London, United Kingdom is cloudy.")
  727. XCTAssertEqual(segment.startIndex, 0)
  728. XCTAssertEqual(segment.partIndex, 0)
  729. XCTAssertEqual(segment.endIndex, 56)
  730. XCTAssertEqual(firstSupport.groundingChunkIndices, [0])
  731. }
  732. func testGenerateContent_withGoogleSearchTool() async throws {
  733. let model = GenerativeModel(
  734. modelName: testModelName,
  735. modelResourceName: testModelResourceName,
  736. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  737. apiConfig: apiConfig,
  738. tools: [.googleSearch()],
  739. requestOptions: RequestOptions(),
  740. urlSession: urlSession
  741. )
  742. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  743. forResource: "unary-success-basic-reply-short",
  744. withExtension: "json",
  745. subdirectory: vertexSubdirectory
  746. )
  747. _ = try await model.generateContent(testPrompt)
  748. }
  749. func testGenerateContent_failure_invalidAPIKey() async throws {
  750. let expectedStatusCode = 400
  751. MockURLProtocol
  752. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  753. forResource: "unary-failure-api-key",
  754. withExtension: "json",
  755. subdirectory: vertexSubdirectory,
  756. statusCode: expectedStatusCode
  757. )
  758. do {
  759. _ = try await model.generateContent(testPrompt)
  760. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  761. } catch let GenerateContentError.internalError(error as BackendError) {
  762. XCTAssertEqual(error.httpResponseCode, 400)
  763. XCTAssertEqual(error.status, .invalidArgument)
  764. XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
  765. XCTAssertTrue(error.localizedDescription.contains(error.message))
  766. XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
  767. XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
  768. let nsError = error as NSError
  769. XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
  770. XCTAssertEqual(nsError.code, error.httpResponseCode)
  771. return
  772. } catch {
  773. XCTFail("Should throw GenerateContentError.internalError(RPCError); error thrown: \(error)")
  774. }
  775. }
  776. func testGenerateContent_failure_firebaseVertexAIAPINotEnabled() async throws {
  777. let expectedStatusCode = 403
  778. MockURLProtocol
  779. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  780. forResource: "unary-failure-firebasevertexai-api-not-enabled",
  781. withExtension: "json",
  782. subdirectory: vertexSubdirectory,
  783. statusCode: expectedStatusCode
  784. )
  785. do {
  786. _ = try await model.generateContent(testPrompt)
  787. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  788. } catch let GenerateContentError.internalError(error as BackendError) {
  789. XCTAssertEqual(error.httpResponseCode, expectedStatusCode)
  790. XCTAssertEqual(error.status, .permissionDenied)
  791. XCTAssertTrue(error.message
  792. .starts(with: "Vertex AI in Firebase API has not been used in project"))
  793. XCTAssertTrue(error.isVertexAIInFirebaseServiceDisabledError())
  794. return
  795. } catch {
  796. XCTFail("Should throw GenerateContentError.internalError(RPCError); error thrown: \(error)")
  797. }
  798. }
  799. func testGenerateContent_failure_emptyContent() async throws {
  800. MockURLProtocol
  801. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  802. forResource: "unary-failure-empty-content",
  803. withExtension: "json",
  804. subdirectory: vertexSubdirectory
  805. )
  806. do {
  807. _ = try await model.generateContent(testPrompt)
  808. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  809. } catch let GenerateContentError
  810. .internalError(underlying: invalidCandidateError as InvalidCandidateError) {
  811. guard case let .emptyContent(underlyingError) = invalidCandidateError else {
  812. XCTFail("Should be an InvalidCandidateError.emptyContent error: \(invalidCandidateError)")
  813. return
  814. }
  815. _ = try XCTUnwrap(underlyingError as? Candidate.EmptyContentError,
  816. "Should be an empty content error: \(underlyingError)")
  817. } catch {
  818. XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
  819. }
  820. }
  821. func testGenerateContent_failure_finishReasonSafety() async throws {
  822. MockURLProtocol
  823. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  824. forResource: "unary-failure-finish-reason-safety",
  825. withExtension: "json",
  826. subdirectory: vertexSubdirectory
  827. )
  828. do {
  829. _ = try await model.generateContent(testPrompt)
  830. XCTFail("Should throw")
  831. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  832. XCTAssertEqual(reason, .safety)
  833. XCTAssertEqual(response.text, "<redacted>")
  834. } catch {
  835. XCTFail("Should throw a responseStoppedEarly")
  836. }
  837. }
  838. func testGenerateContent_failure_finishReasonSafety_noContent() async throws {
  839. MockURLProtocol
  840. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  841. forResource: "unary-failure-finish-reason-safety-no-content",
  842. withExtension: "json",
  843. subdirectory: vertexSubdirectory
  844. )
  845. do {
  846. _ = try await model.generateContent(testPrompt)
  847. XCTFail("Should throw")
  848. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  849. XCTAssertEqual(reason, .safety)
  850. XCTAssertNil(response.text)
  851. } catch {
  852. XCTFail("Should throw a responseStoppedEarly")
  853. }
  854. }
  855. func testGenerateContent_failure_imageRejected() async throws {
  856. let expectedStatusCode = 400
  857. MockURLProtocol
  858. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  859. forResource: "unary-failure-image-rejected",
  860. withExtension: "json",
  861. subdirectory: vertexSubdirectory,
  862. statusCode: 400
  863. )
  864. do {
  865. _ = try await model.generateContent(testPrompt)
  866. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  867. } catch let GenerateContentError.internalError(underlying: rpcError as BackendError) {
  868. XCTAssertEqual(rpcError.status, .invalidArgument)
  869. XCTAssertEqual(rpcError.httpResponseCode, expectedStatusCode)
  870. XCTAssertEqual(rpcError.message, "Request contains an invalid argument.")
  871. } catch {
  872. XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
  873. }
  874. }
  875. func testGenerateContent_failure_promptBlockedSafety() async throws {
  876. MockURLProtocol
  877. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  878. forResource: "unary-failure-prompt-blocked-safety",
  879. withExtension: "json",
  880. subdirectory: vertexSubdirectory
  881. )
  882. do {
  883. _ = try await model.generateContent(testPrompt)
  884. XCTFail("Should throw")
  885. } catch let GenerateContentError.promptBlocked(response) {
  886. XCTAssertNil(response.text)
  887. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  888. XCTAssertEqual(promptFeedback.blockReason, PromptFeedback.BlockReason.safety)
  889. XCTAssertNil(promptFeedback.blockReasonMessage)
  890. } catch {
  891. XCTFail("Should throw a promptBlocked")
  892. }
  893. }
  894. func testGenerateContent_failure_promptBlockedSafetyWithMessage() async throws {
  895. MockURLProtocol
  896. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  897. forResource: "unary-failure-prompt-blocked-safety-with-message",
  898. withExtension: "json",
  899. subdirectory: vertexSubdirectory
  900. )
  901. do {
  902. _ = try await model.generateContent(testPrompt)
  903. XCTFail("Should throw")
  904. } catch let GenerateContentError.promptBlocked(response) {
  905. XCTAssertNil(response.text)
  906. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  907. XCTAssertEqual(promptFeedback.blockReason, PromptFeedback.BlockReason.safety)
  908. XCTAssertEqual(promptFeedback.blockReasonMessage, "Reasons")
  909. } catch {
  910. XCTFail("Should throw a promptBlocked")
  911. }
  912. }
  913. func testGenerateContent_failure_unknownEnum_finishReason() async throws {
  914. MockURLProtocol
  915. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  916. forResource: "unary-failure-unknown-enum-finish-reason",
  917. withExtension: "json",
  918. subdirectory: vertexSubdirectory
  919. )
  920. let unknownFinishReason = FinishReason(rawValue: "FAKE_NEW_FINISH_REASON")
  921. do {
  922. _ = try await model.generateContent(testPrompt)
  923. XCTFail("Should throw")
  924. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  925. XCTAssertEqual(reason, unknownFinishReason)
  926. XCTAssertEqual(response.text, "Some text")
  927. } catch {
  928. XCTFail("Should throw a responseStoppedEarly")
  929. }
  930. }
  931. func testGenerateContent_failure_unknownEnum_promptBlocked() async throws {
  932. MockURLProtocol
  933. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  934. forResource: "unary-failure-unknown-enum-prompt-blocked",
  935. withExtension: "json",
  936. subdirectory: vertexSubdirectory
  937. )
  938. let unknownBlockReason = PromptFeedback.BlockReason(rawValue: "FAKE_NEW_BLOCK_REASON")
  939. do {
  940. _ = try await model.generateContent(testPrompt)
  941. XCTFail("Should throw")
  942. } catch let GenerateContentError.promptBlocked(response) {
  943. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  944. XCTAssertEqual(promptFeedback.blockReason, unknownBlockReason)
  945. } catch {
  946. XCTFail("Should throw a promptBlocked")
  947. }
  948. }
  949. func testGenerateContent_failure_unknownModel() async throws {
  950. let expectedStatusCode = 404
  951. MockURLProtocol
  952. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  953. forResource: "unary-failure-unknown-model",
  954. withExtension: "json",
  955. subdirectory: vertexSubdirectory,
  956. statusCode: 404
  957. )
  958. do {
  959. _ = try await model.generateContent(testPrompt)
  960. XCTFail("Should throw GenerateContentError.internalError; no error thrown.")
  961. } catch let GenerateContentError.internalError(underlying: rpcError as BackendError) {
  962. XCTAssertEqual(rpcError.status, .notFound)
  963. XCTAssertEqual(rpcError.httpResponseCode, expectedStatusCode)
  964. XCTAssertTrue(rpcError.message.hasPrefix("models/unknown is not found"))
  965. } catch {
  966. XCTFail("Should throw GenerateContentError.internalError; error thrown: \(error)")
  967. }
  968. }
  969. func testGenerateContent_failure_nonHTTPResponse() async throws {
  970. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.nonHTTPRequestHandler()
  971. var responseError: Error?
  972. var content: GenerateContentResponse?
  973. do {
  974. content = try await model.generateContent(testPrompt)
  975. } catch {
  976. responseError = error
  977. }
  978. XCTAssertNil(content)
  979. XCTAssertNotNil(responseError)
  980. let generateContentError = try XCTUnwrap(responseError as? GenerateContentError)
  981. guard case let .internalError(underlyingError) = generateContentError else {
  982. XCTFail("Should be an internal error: \(generateContentError)")
  983. return
  984. }
  985. XCTAssertEqual(underlyingError.localizedDescription, "Response was not an HTTP response.")
  986. let underlyingNSError = underlyingError as NSError
  987. XCTAssertEqual(underlyingNSError.domain, NSURLErrorDomain)
  988. XCTAssertEqual(underlyingNSError.code, URLError.Code.badServerResponse.rawValue)
  989. }
  990. func testGenerateContent_failure_invalidResponse() async throws {
  991. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  992. forResource: "unary-failure-invalid-response",
  993. withExtension: "json",
  994. subdirectory: vertexSubdirectory
  995. )
  996. var responseError: Error?
  997. var content: GenerateContentResponse?
  998. do {
  999. content = try await model.generateContent(testPrompt)
  1000. } catch {
  1001. responseError = error
  1002. }
  1003. XCTAssertNil(content)
  1004. XCTAssertNotNil(responseError)
  1005. let generateContentError = try XCTUnwrap(responseError as? GenerateContentError)
  1006. guard case let .internalError(underlyingError) = generateContentError else {
  1007. XCTFail("Should be an internal error: \(generateContentError)")
  1008. return
  1009. }
  1010. let decodingError = try XCTUnwrap(underlyingError as? DecodingError)
  1011. guard case let .dataCorrupted(context) = decodingError else {
  1012. XCTFail("Should be a data corrupted error: \(decodingError)")
  1013. return
  1014. }
  1015. XCTAssert(context.debugDescription.hasPrefix("Failed to decode GenerateContentResponse"))
  1016. }
  1017. func testGenerateContent_failure_malformedContent() async throws {
  1018. MockURLProtocol
  1019. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1020. // Note: Although this file does not contain `parts` in `content`, it is not actually
  1021. // malformed. The `invalid-field` in the payload could be added, as a non-breaking change to
  1022. // the proto API. Therefore, this test checks for the `emptyContent` error instead.
  1023. forResource: "unary-failure-malformed-content",
  1024. withExtension: "json",
  1025. subdirectory: vertexSubdirectory
  1026. )
  1027. var responseError: Error?
  1028. var content: GenerateContentResponse?
  1029. do {
  1030. content = try await model.generateContent(testPrompt)
  1031. } catch {
  1032. responseError = error
  1033. }
  1034. XCTAssertNil(content)
  1035. XCTAssertNotNil(responseError)
  1036. let generateContentError = try XCTUnwrap(responseError as? GenerateContentError)
  1037. guard case let .internalError(underlyingError) = generateContentError else {
  1038. XCTFail("Should be an internal error: \(generateContentError)")
  1039. return
  1040. }
  1041. let invalidCandidateError = try XCTUnwrap(underlyingError as? InvalidCandidateError)
  1042. guard case let .emptyContent(emptyContentUnderlyingError) = invalidCandidateError else {
  1043. XCTFail("Should be an empty content error: \(invalidCandidateError)")
  1044. return
  1045. }
  1046. _ = try XCTUnwrap(
  1047. emptyContentUnderlyingError as? Candidate.EmptyContentError,
  1048. "Should be an empty content error: \(emptyContentUnderlyingError)"
  1049. )
  1050. }
  1051. func testGenerateContentMissingSafetyRatings() async throws {
  1052. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1053. forResource: "unary-success-missing-safety-ratings",
  1054. withExtension: "json",
  1055. subdirectory: vertexSubdirectory
  1056. )
  1057. let content = try await model.generateContent(testPrompt)
  1058. let promptFeedback = try XCTUnwrap(content.promptFeedback)
  1059. XCTAssertEqual(promptFeedback.safetyRatings.count, 0)
  1060. XCTAssertEqual(content.text, "This is the generated content.")
  1061. }
  1062. func testGenerateContent_requestOptions_customTimeout() async throws {
  1063. let expectedTimeout = 150.0
  1064. MockURLProtocol
  1065. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1066. forResource: "unary-success-basic-reply-short",
  1067. withExtension: "json",
  1068. subdirectory: vertexSubdirectory,
  1069. timeout: expectedTimeout
  1070. )
  1071. let requestOptions = RequestOptions(timeout: expectedTimeout)
  1072. model = GenerativeModel(
  1073. modelName: testModelName,
  1074. modelResourceName: testModelResourceName,
  1075. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1076. apiConfig: apiConfig,
  1077. tools: nil,
  1078. requestOptions: requestOptions,
  1079. urlSession: urlSession
  1080. )
  1081. let response = try await model.generateContent(testPrompt)
  1082. XCTAssertEqual(response.candidates.count, 1)
  1083. }
  1084. // MARK: - Generate Content (Streaming)
  1085. func testGenerateContentStream_failureInvalidAPIKey() async throws {
  1086. MockURLProtocol
  1087. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1088. forResource: "unary-failure-api-key",
  1089. withExtension: "json",
  1090. subdirectory: vertexSubdirectory
  1091. )
  1092. do {
  1093. let stream = try model.generateContentStream("Hi")
  1094. for try await _ in stream {
  1095. XCTFail("No content is there, this shouldn't happen.")
  1096. }
  1097. } catch let GenerateContentError.internalError(error as BackendError) {
  1098. XCTAssertEqual(error.httpResponseCode, 400)
  1099. XCTAssertEqual(error.status, .invalidArgument)
  1100. XCTAssertEqual(error.message, "API key not valid. Please pass a valid API key.")
  1101. XCTAssertTrue(error.localizedDescription.contains(error.message))
  1102. XCTAssertTrue(error.localizedDescription.contains(error.status.rawValue))
  1103. XCTAssertTrue(error.localizedDescription.contains("\(error.httpResponseCode)"))
  1104. let nsError = error as NSError
  1105. XCTAssertEqual(nsError.domain, "\(Constants.baseErrorDomain).\(BackendError.self)")
  1106. XCTAssertEqual(nsError.code, error.httpResponseCode)
  1107. return
  1108. }
  1109. XCTFail("Should have caught an error.")
  1110. }
  1111. func testGenerateContentStream_failure_vertexAIInFirebaseAPINotEnabled() async throws {
  1112. let expectedStatusCode = 403
  1113. MockURLProtocol
  1114. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1115. forResource: "unary-failure-firebasevertexai-api-not-enabled",
  1116. withExtension: "json",
  1117. subdirectory: vertexSubdirectory,
  1118. statusCode: expectedStatusCode
  1119. )
  1120. do {
  1121. let stream = try model.generateContentStream(testPrompt)
  1122. for try await _ in stream {
  1123. XCTFail("No content is there, this shouldn't happen.")
  1124. }
  1125. } catch let GenerateContentError.internalError(error as BackendError) {
  1126. XCTAssertEqual(error.httpResponseCode, expectedStatusCode)
  1127. XCTAssertEqual(error.status, .permissionDenied)
  1128. XCTAssertTrue(error.message
  1129. .starts(with: "Vertex AI in Firebase API has not been used in project"))
  1130. XCTAssertTrue(error.isVertexAIInFirebaseServiceDisabledError())
  1131. return
  1132. }
  1133. XCTFail("Should have caught an error.")
  1134. }
  1135. func testGenerateContentStream_failureEmptyContent() async throws {
  1136. MockURLProtocol
  1137. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1138. forResource: "streaming-failure-empty-content",
  1139. withExtension: "txt",
  1140. subdirectory: vertexSubdirectory
  1141. )
  1142. do {
  1143. let stream = try model.generateContentStream("Hi")
  1144. for try await _ in stream {
  1145. XCTFail("No content is there, this shouldn't happen.")
  1146. }
  1147. } catch GenerateContentError.internalError(_ as InvalidCandidateError) {
  1148. // Underlying error is as expected, nothing else to check.
  1149. return
  1150. }
  1151. XCTFail("Should have caught an error.")
  1152. }
  1153. func testGenerateContentStream_failureFinishReasonSafety() async throws {
  1154. MockURLProtocol
  1155. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1156. forResource: "streaming-failure-finish-reason-safety",
  1157. withExtension: "txt",
  1158. subdirectory: vertexSubdirectory
  1159. )
  1160. do {
  1161. let stream = try model.generateContentStream("Hi")
  1162. for try await _ in stream {
  1163. XCTFail("Content shouldn't be shown, this shouldn't happen.")
  1164. }
  1165. } catch let GenerateContentError.responseStoppedEarly(reason, response) {
  1166. XCTAssertEqual(reason, .safety)
  1167. let candidate = try XCTUnwrap(response.candidates.first)
  1168. XCTAssertEqual(candidate.finishReason, reason)
  1169. XCTAssertTrue(candidate.safetyRatings.contains { $0.blocked })
  1170. return
  1171. }
  1172. XCTFail("Should have caught an error.")
  1173. }
  1174. func testGenerateContentStream_failurePromptBlockedSafety() async throws {
  1175. MockURLProtocol
  1176. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1177. forResource: "streaming-failure-prompt-blocked-safety",
  1178. withExtension: "txt",
  1179. subdirectory: vertexSubdirectory
  1180. )
  1181. do {
  1182. let stream = try model.generateContentStream("Hi")
  1183. for try await _ in stream {
  1184. XCTFail("Content shouldn't be shown, this shouldn't happen.")
  1185. }
  1186. } catch let GenerateContentError.promptBlocked(response) {
  1187. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  1188. XCTAssertEqual(promptFeedback.blockReason, .safety)
  1189. XCTAssertNil(promptFeedback.blockReasonMessage)
  1190. return
  1191. }
  1192. XCTFail("Should have caught an error.")
  1193. }
  1194. func testGenerateContentStream_failurePromptBlockedSafetyWithMessage() async throws {
  1195. MockURLProtocol
  1196. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1197. forResource: "streaming-failure-prompt-blocked-safety-with-message",
  1198. withExtension: "txt",
  1199. subdirectory: vertexSubdirectory
  1200. )
  1201. do {
  1202. let stream = try model.generateContentStream("Hi")
  1203. for try await _ in stream {
  1204. XCTFail("Content shouldn't be shown, this shouldn't happen.")
  1205. }
  1206. } catch let GenerateContentError.promptBlocked(response) {
  1207. let promptFeedback = try XCTUnwrap(response.promptFeedback)
  1208. XCTAssertEqual(promptFeedback.blockReason, .safety)
  1209. XCTAssertEqual(promptFeedback.blockReasonMessage, "Reasons")
  1210. return
  1211. }
  1212. XCTFail("Should have caught an error.")
  1213. }
  1214. func testGenerateContentStream_failureUnknownFinishEnum() async throws {
  1215. MockURLProtocol
  1216. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1217. forResource: "streaming-failure-unknown-finish-enum",
  1218. withExtension: "txt",
  1219. subdirectory: vertexSubdirectory
  1220. )
  1221. let unknownFinishReason = FinishReason(rawValue: "FAKE_ENUM")
  1222. let stream = try model.generateContentStream("Hi")
  1223. do {
  1224. for try await content in stream {
  1225. XCTAssertNotNil(content.text)
  1226. }
  1227. } catch let GenerateContentError.responseStoppedEarly(reason, _) {
  1228. XCTAssertEqual(reason, unknownFinishReason)
  1229. return
  1230. }
  1231. XCTFail("Should have caught an error.")
  1232. }
  1233. func testGenerateContentStream_successBasicReplyLong() async throws {
  1234. MockURLProtocol
  1235. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1236. forResource: "streaming-success-basic-reply-long",
  1237. withExtension: "txt",
  1238. subdirectory: vertexSubdirectory
  1239. )
  1240. var responses = 0
  1241. let stream = try model.generateContentStream("Hi")
  1242. for try await content in stream {
  1243. XCTAssertNotNil(content.text)
  1244. responses += 1
  1245. }
  1246. XCTAssertEqual(responses, 4)
  1247. }
  1248. func testGenerateContentStream_successBasicReplyShort() async throws {
  1249. MockURLProtocol
  1250. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1251. forResource: "streaming-success-basic-reply-short",
  1252. withExtension: "txt",
  1253. subdirectory: vertexSubdirectory
  1254. )
  1255. var responses = 0
  1256. let stream = try model.generateContentStream("Hi")
  1257. for try await content in stream {
  1258. XCTAssertNotNil(content.text)
  1259. responses += 1
  1260. }
  1261. XCTAssertEqual(responses, 1)
  1262. }
  1263. func testGenerateContentStream_successUnknownSafetyEnum() async throws {
  1264. MockURLProtocol
  1265. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1266. forResource: "streaming-success-unknown-safety-enum",
  1267. withExtension: "txt",
  1268. subdirectory: vertexSubdirectory
  1269. )
  1270. let unknownSafetyRating = SafetyRating(
  1271. category: HarmCategory(rawValue: "HARM_CATEGORY_DANGEROUS_CONTENT_NEW_ENUM"),
  1272. probability: SafetyRating.HarmProbability(rawValue: "NEGLIGIBLE_UNKNOWN_ENUM"),
  1273. probabilityScore: 0.0,
  1274. severity: SafetyRating.HarmSeverity(rawValue: "HARM_SEVERITY_UNSPECIFIED"),
  1275. severityScore: 0.0,
  1276. blocked: false
  1277. )
  1278. var foundUnknownSafetyRating = false
  1279. let stream = try model.generateContentStream("Hi")
  1280. for try await content in stream {
  1281. XCTAssertNotNil(content.text)
  1282. if let ratings = content.candidates.first?.safetyRatings,
  1283. ratings.contains(where: { $0 == unknownSafetyRating }) {
  1284. foundUnknownSafetyRating = true
  1285. }
  1286. }
  1287. XCTAssertTrue(foundUnknownSafetyRating)
  1288. }
  1289. func testGenerateContentStream_successWithCitations() async throws {
  1290. MockURLProtocol
  1291. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1292. forResource: "streaming-success-citations",
  1293. withExtension: "txt",
  1294. subdirectory: vertexSubdirectory
  1295. )
  1296. let expectedPublicationDate = DateComponents(
  1297. calendar: Calendar(identifier: .gregorian),
  1298. year: 2014,
  1299. month: 3,
  1300. day: 30
  1301. )
  1302. let stream = try model.generateContentStream("Hi")
  1303. var citations = [Citation]()
  1304. var responses = [GenerateContentResponse]()
  1305. for try await content in stream {
  1306. responses.append(content)
  1307. XCTAssertNotNil(content.text)
  1308. let candidate = try XCTUnwrap(content.candidates.first)
  1309. if let sources = candidate.citationMetadata?.citations {
  1310. citations.append(contentsOf: sources)
  1311. }
  1312. }
  1313. let lastCandidate = try XCTUnwrap(responses.last?.candidates.first)
  1314. XCTAssertEqual(lastCandidate.finishReason, .stop)
  1315. XCTAssertEqual(citations.count, 6)
  1316. XCTAssertTrue(citations
  1317. .contains {
  1318. $0.startIndex == 0 && $0.endIndex == 128
  1319. && $0.uri == "https://www.example.com/some-citation-1" && $0.title == nil
  1320. && $0.license == nil && $0.publicationDate == nil
  1321. })
  1322. XCTAssertTrue(citations
  1323. .contains {
  1324. $0.startIndex == 130 && $0.endIndex == 265 && $0.uri == nil
  1325. && $0.title == "some-citation-2" && $0.license == nil
  1326. && $0.publicationDate == expectedPublicationDate
  1327. })
  1328. XCTAssertTrue(citations
  1329. .contains {
  1330. $0.startIndex == 272 && $0.endIndex == 431
  1331. && $0.uri == "https://www.example.com/some-citation-3" && $0.title == nil
  1332. && $0.license == "mit" && $0.publicationDate == nil
  1333. })
  1334. XCTAssertFalse(citations.contains { $0.uri?.isEmpty ?? false })
  1335. XCTAssertFalse(citations.contains { $0.title?.isEmpty ?? false })
  1336. XCTAssertFalse(citations.contains { $0.license?.isEmpty ?? false })
  1337. }
  1338. func testGenerateContentStream_successWithThinking_thoughtSummary() async throws {
  1339. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1340. forResource: "streaming-success-thinking-reply-thought-summary",
  1341. withExtension: "txt",
  1342. subdirectory: vertexSubdirectory
  1343. )
  1344. var thoughtSummary = ""
  1345. var text = ""
  1346. let stream = try model.generateContentStream("Hi")
  1347. for try await response in stream {
  1348. let candidate = try XCTUnwrap(response.candidates.first)
  1349. XCTAssertEqual(candidate.content.parts.count, 1)
  1350. let part = try XCTUnwrap(candidate.content.parts.first)
  1351. let textPart = try XCTUnwrap(part as? TextPart)
  1352. if textPart.isThought {
  1353. let newThought = try XCTUnwrap(response.thoughtSummary)
  1354. thoughtSummary.append(newThought)
  1355. } else {
  1356. text.append(textPart.text)
  1357. }
  1358. }
  1359. XCTAssertTrue(thoughtSummary.hasPrefix("**Understanding the Core Question**"))
  1360. XCTAssertTrue(text.hasPrefix("The sky is blue due to a phenomenon"))
  1361. }
  1362. func testGenerateContentStream_success_codeExecution() async throws {
  1363. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1364. forResource: "streaming-success-code-execution",
  1365. withExtension: "txt",
  1366. subdirectory: vertexSubdirectory
  1367. )
  1368. var parts = [any Part]()
  1369. let stream = try model.generateContentStream(testPrompt)
  1370. for try await response in stream {
  1371. if let responseParts = response.candidates.first?.content.parts {
  1372. parts.append(contentsOf: responseParts)
  1373. }
  1374. }
  1375. let thoughtParts = parts.filter { $0.isThought }
  1376. XCTAssertEqual(thoughtParts.count, 0)
  1377. let textParts = parts.filter { $0 is TextPart }
  1378. XCTAssertGreaterThan(textParts.count, 0)
  1379. let executableCodeParts = parts.compactMap { $0 as? ExecutableCodePart }
  1380. XCTAssertEqual(executableCodeParts.count, 1)
  1381. let executableCodePart = try XCTUnwrap(executableCodeParts.first)
  1382. XCTAssertFalse(executableCodePart.isThought)
  1383. XCTAssertEqual(executableCodePart.language, .python)
  1384. XCTAssertTrue(executableCodePart.code.starts(with: "prime_numbers = [2, 3, 5, 7, 11]"))
  1385. let codeExecutionResultParts = parts.compactMap { $0 as? CodeExecutionResultPart }
  1386. XCTAssertEqual(codeExecutionResultParts.count, 1)
  1387. let codeExecutionResultPart = try XCTUnwrap(codeExecutionResultParts.first)
  1388. XCTAssertFalse(codeExecutionResultPart.isThought)
  1389. XCTAssertEqual(codeExecutionResultPart.outcome, .ok)
  1390. XCTAssertEqual(codeExecutionResultPart.output, "The sum of the first 5 prime numbers is: 28\n")
  1391. }
  1392. func testGenerateContentStream_successWithInvalidSafetyRatingsIgnored() async throws {
  1393. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1394. forResource: "streaming-success-image-invalid-safety-ratings",
  1395. withExtension: "txt",
  1396. subdirectory: vertexSubdirectory
  1397. )
  1398. let stream = try model.generateContentStream(testPrompt)
  1399. var responses = [GenerateContentResponse]()
  1400. for try await content in stream {
  1401. responses.append(content)
  1402. }
  1403. let response = try XCTUnwrap(responses.first)
  1404. XCTAssertEqual(response.candidates.count, 1)
  1405. let candidate = try XCTUnwrap(response.candidates.first)
  1406. XCTAssertEqual(candidate.safetyRatings.sorted(), safetyRatingsInvalidIgnored)
  1407. XCTAssertEqual(candidate.content.parts.count, 1)
  1408. let inlineDataParts = response.inlineDataParts
  1409. XCTAssertEqual(inlineDataParts.count, 1)
  1410. let imagePart = try XCTUnwrap(inlineDataParts.first)
  1411. XCTAssertEqual(imagePart.mimeType, "image/png")
  1412. XCTAssertGreaterThan(imagePart.data.count, 0)
  1413. }
  1414. func testGenerateContentStream_appCheck_validToken() async throws {
  1415. let appCheckToken = "test-valid-token"
  1416. model = GenerativeModel(
  1417. modelName: testModelName,
  1418. modelResourceName: testModelResourceName,
  1419. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  1420. appCheck: AppCheckInteropFake(token: appCheckToken)
  1421. ),
  1422. apiConfig: apiConfig,
  1423. tools: nil,
  1424. requestOptions: RequestOptions(),
  1425. urlSession: urlSession
  1426. )
  1427. MockURLProtocol
  1428. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1429. forResource: "streaming-success-basic-reply-short",
  1430. withExtension: "txt",
  1431. subdirectory: vertexSubdirectory,
  1432. appCheckToken: appCheckToken
  1433. )
  1434. let stream = try model.generateContentStream(testPrompt)
  1435. for try await _ in stream {}
  1436. }
  1437. func testGenerateContentStream_appCheck_tokenRefreshError() async throws {
  1438. model = GenerativeModel(
  1439. modelName: testModelName,
  1440. modelResourceName: testModelResourceName,
  1441. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
  1442. appCheck: AppCheckInteropFake(error: AppCheckErrorFake())
  1443. ),
  1444. apiConfig: apiConfig,
  1445. tools: nil,
  1446. requestOptions: RequestOptions(),
  1447. urlSession: urlSession
  1448. )
  1449. MockURLProtocol
  1450. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1451. forResource: "streaming-success-basic-reply-short",
  1452. withExtension: "txt",
  1453. subdirectory: vertexSubdirectory,
  1454. appCheckToken: AppCheckInteropFake.placeholderTokenValue
  1455. )
  1456. let stream = try model.generateContentStream(testPrompt)
  1457. for try await _ in stream {}
  1458. }
  1459. func testGenerateContentStream_usageMetadata() async throws {
  1460. MockURLProtocol
  1461. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1462. forResource: "streaming-success-basic-reply-short",
  1463. withExtension: "txt",
  1464. subdirectory: vertexSubdirectory
  1465. )
  1466. var responses = [GenerateContentResponse]()
  1467. let stream = try model.generateContentStream(testPrompt)
  1468. for try await response in stream {
  1469. responses.append(response)
  1470. }
  1471. for (index, response) in responses.enumerated() {
  1472. if index == responses.endIndex - 1 {
  1473. let usageMetadata = try XCTUnwrap(response.usageMetadata)
  1474. XCTAssertEqual(usageMetadata.promptTokenCount, 6)
  1475. XCTAssertEqual(usageMetadata.candidatesTokenCount, 4)
  1476. XCTAssertEqual(usageMetadata.totalTokenCount, 10)
  1477. } else {
  1478. // Only the last streamed response contains usage metadata
  1479. XCTAssertNil(response.usageMetadata)
  1480. }
  1481. }
  1482. }
  1483. func testGenerateContentStream_errorMidStream() async throws {
  1484. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1485. forResource: "streaming-failure-error-mid-stream",
  1486. withExtension: "txt",
  1487. subdirectory: vertexSubdirectory
  1488. )
  1489. var responseCount = 0
  1490. do {
  1491. let stream = try model.generateContentStream("Hi")
  1492. for try await content in stream {
  1493. XCTAssertNotNil(content.text)
  1494. responseCount += 1
  1495. }
  1496. } catch let GenerateContentError.internalError(rpcError as BackendError) {
  1497. XCTAssertEqual(rpcError.httpResponseCode, 499)
  1498. XCTAssertEqual(rpcError.status, .cancelled)
  1499. // Check the content count is correct.
  1500. XCTAssertEqual(responseCount, 2)
  1501. return
  1502. }
  1503. XCTFail("Expected an internalError with an RPCError.")
  1504. }
  1505. func testGenerateContentStream_nonHTTPResponse() async throws {
  1506. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.nonHTTPRequestHandler()
  1507. let stream = try model.generateContentStream("Hi")
  1508. do {
  1509. for try await content in stream {
  1510. XCTFail("Unexpected content in stream: \(content)")
  1511. }
  1512. } catch let GenerateContentError.internalError(underlying) {
  1513. XCTAssertEqual(underlying.localizedDescription, "Response was not an HTTP response.")
  1514. return
  1515. }
  1516. XCTFail("Expected an internal error.")
  1517. }
  1518. func testGenerateContentStream_invalidResponse() async throws {
  1519. MockURLProtocol
  1520. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1521. forResource: "streaming-failure-invalid-json",
  1522. withExtension: "txt",
  1523. subdirectory: vertexSubdirectory
  1524. )
  1525. let stream = try model.generateContentStream(testPrompt)
  1526. do {
  1527. for try await content in stream {
  1528. XCTFail("Unexpected content in stream: \(content)")
  1529. }
  1530. } catch let GenerateContentError.internalError(underlying as DecodingError) {
  1531. guard case let .dataCorrupted(context) = underlying else {
  1532. XCTFail("Should be a data corrupted error: \(underlying)")
  1533. return
  1534. }
  1535. XCTAssert(context.debugDescription.hasPrefix("Failed to decode GenerateContentResponse"))
  1536. return
  1537. }
  1538. XCTFail("Expected an internal error.")
  1539. }
  1540. func testGenerateContentStream_malformedContent() async throws {
  1541. MockURLProtocol
  1542. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1543. // Note: Although this file does not contain `parts` in `content`, it is not actually
  1544. // malformed. The `invalid-field` in the payload could be added, as a non-breaking change to
  1545. // the proto API. Therefore, this test checks for the `emptyContent` error instead.
  1546. forResource: "streaming-failure-malformed-content",
  1547. withExtension: "txt",
  1548. subdirectory: vertexSubdirectory
  1549. )
  1550. let stream = try model.generateContentStream(testPrompt)
  1551. do {
  1552. for try await content in stream {
  1553. XCTFail("Unexpected content in stream: \(content)")
  1554. }
  1555. } catch let GenerateContentError.internalError(underlyingError as InvalidCandidateError) {
  1556. guard case let .emptyContent(contentError) = underlyingError else {
  1557. XCTFail("Should be an empty content error: \(underlyingError)")
  1558. return
  1559. }
  1560. XCTAssert(contentError is Candidate.EmptyContentError)
  1561. return
  1562. }
  1563. XCTFail("Expected an internal decoding error.")
  1564. }
  1565. func testGenerateContentStream_requestOptions_customTimeout() async throws {
  1566. let expectedTimeout = 150.0
  1567. MockURLProtocol
  1568. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1569. forResource: "streaming-success-basic-reply-short",
  1570. withExtension: "txt",
  1571. subdirectory: vertexSubdirectory,
  1572. timeout: expectedTimeout
  1573. )
  1574. let requestOptions = RequestOptions(timeout: expectedTimeout)
  1575. model = GenerativeModel(
  1576. modelName: testModelName,
  1577. modelResourceName: testModelResourceName,
  1578. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1579. apiConfig: apiConfig,
  1580. tools: nil,
  1581. requestOptions: requestOptions,
  1582. urlSession: urlSession
  1583. )
  1584. var responses = 0
  1585. let stream = try model.generateContentStream(testPrompt)
  1586. for try await content in stream {
  1587. XCTAssertNotNil(content.text)
  1588. responses += 1
  1589. }
  1590. XCTAssertEqual(responses, 1)
  1591. }
  1592. func testGenerateContentStream_success_urlContext() async throws {
  1593. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1594. forResource: "streaming-success-url-context",
  1595. withExtension: "txt",
  1596. subdirectory: vertexSubdirectory
  1597. )
  1598. var responses = [GenerateContentResponse]()
  1599. let stream = try model.generateContentStream(testPrompt)
  1600. for try await response in stream {
  1601. responses.append(response)
  1602. }
  1603. let firstResponse = try XCTUnwrap(responses.first)
  1604. let candidate = try XCTUnwrap(firstResponse.candidates.first)
  1605. let urlContextMetadata = try XCTUnwrap(candidate.urlContextMetadata)
  1606. XCTAssertEqual(urlContextMetadata.urlMetadata.count, 1)
  1607. let urlMetadata = try XCTUnwrap(urlContextMetadata.urlMetadata.first)
  1608. let retrievedURL = try XCTUnwrap(urlMetadata.retrievedURL)
  1609. XCTAssertEqual(retrievedURL, URL(string: "https://google.com"))
  1610. XCTAssertEqual(urlMetadata.retrievalStatus, .success)
  1611. }
  1612. // MARK: - Count Tokens
  1613. func testCountTokens_succeeds() async throws {
  1614. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1615. forResource: "unary-success-total-tokens",
  1616. withExtension: "json",
  1617. subdirectory: vertexSubdirectory
  1618. )
  1619. let response = try await model.countTokens("Why is the sky blue?")
  1620. XCTAssertEqual(response.totalTokens, 6)
  1621. }
  1622. func testCountTokens_succeeds_detailed() async throws {
  1623. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1624. forResource: "unary-success-detailed-token-response",
  1625. withExtension: "json",
  1626. subdirectory: vertexSubdirectory
  1627. )
  1628. let response = try await model.countTokens("Why is the sky blue?")
  1629. XCTAssertEqual(response.totalTokens, 1837)
  1630. XCTAssertEqual(response.promptTokensDetails.count, 2)
  1631. XCTAssertEqual(response.promptTokensDetails[0].modality, .image)
  1632. XCTAssertEqual(response.promptTokensDetails[0].tokenCount, 1806)
  1633. XCTAssertEqual(response.promptTokensDetails[1].modality, .text)
  1634. XCTAssertEqual(response.promptTokensDetails[1].tokenCount, 31)
  1635. }
  1636. func testCountTokens_succeeds_allOptions() async throws {
  1637. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1638. forResource: "unary-success-total-tokens",
  1639. withExtension: "json",
  1640. subdirectory: vertexSubdirectory
  1641. )
  1642. let generationConfig = GenerationConfig(
  1643. temperature: 0.5,
  1644. topP: 0.9,
  1645. topK: 3,
  1646. candidateCount: 1,
  1647. maxOutputTokens: 1024,
  1648. stopSequences: ["test-stop"],
  1649. responseMIMEType: "text/plain"
  1650. )
  1651. let sumFunction = FunctionDeclaration(
  1652. name: "sum",
  1653. description: "Add two integers.",
  1654. parameters: ["x": .integer(), "y": .integer()]
  1655. )
  1656. let systemInstruction = ModelContent(
  1657. role: "system",
  1658. parts: "You are a calculator. Use the provided tools."
  1659. )
  1660. model = GenerativeModel(
  1661. modelName: testModelName,
  1662. modelResourceName: testModelResourceName,
  1663. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1664. apiConfig: apiConfig,
  1665. generationConfig: generationConfig,
  1666. tools: [Tool(functionDeclarations: [sumFunction])],
  1667. systemInstruction: systemInstruction,
  1668. requestOptions: RequestOptions(),
  1669. urlSession: urlSession
  1670. )
  1671. let response = try await model.countTokens("Why is the sky blue?")
  1672. XCTAssertEqual(response.totalTokens, 6)
  1673. }
  1674. func testCountTokens_succeeds_noBillableCharacters() async throws {
  1675. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1676. forResource: "unary-success-no-billable-characters",
  1677. withExtension: "json",
  1678. subdirectory: vertexSubdirectory
  1679. )
  1680. let response = try await model.countTokens(InlineDataPart(data: Data(), mimeType: "image/jpeg"))
  1681. XCTAssertEqual(response.totalTokens, 258)
  1682. }
  1683. func testCountTokens_modelNotFound() async throws {
  1684. MockURLProtocol.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1685. forResource: "unary-failure-model-not-found", withExtension: "json",
  1686. subdirectory: vertexSubdirectory,
  1687. statusCode: 404
  1688. )
  1689. do {
  1690. _ = try await model.countTokens("Why is the sky blue?")
  1691. XCTFail("Request should not have succeeded.")
  1692. } catch let rpcError as BackendError {
  1693. XCTAssertEqual(rpcError.httpResponseCode, 404)
  1694. XCTAssertEqual(rpcError.status, .notFound)
  1695. XCTAssert(rpcError.message.hasPrefix("models/test-model-name is not found"))
  1696. return
  1697. }
  1698. XCTFail("Expected internal RPCError.")
  1699. }
  1700. func testCountTokens_requestOptions_customTimeout() async throws {
  1701. let expectedTimeout = 150.0
  1702. MockURLProtocol
  1703. .requestHandler = try GenerativeModelTestUtil.httpRequestHandler(
  1704. forResource: "unary-success-total-tokens",
  1705. withExtension: "json",
  1706. subdirectory: vertexSubdirectory,
  1707. timeout: expectedTimeout
  1708. )
  1709. let requestOptions = RequestOptions(timeout: expectedTimeout)
  1710. model = GenerativeModel(
  1711. modelName: testModelName,
  1712. modelResourceName: testModelResourceName,
  1713. firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(),
  1714. apiConfig: apiConfig,
  1715. tools: nil,
  1716. requestOptions: requestOptions,
  1717. urlSession: urlSession
  1718. )
  1719. let response = try await model.countTokens(testPrompt)
  1720. XCTAssertEqual(response.totalTokens, 6)
  1721. }
  1722. }
  1723. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  1724. extension SafetyRating: Swift.Comparable {
  1725. public static func < (lhs: SafetyRating, rhs: SafetyRating) -> Bool {
  1726. return lhs.category.rawValue < rhs.category.rawValue
  1727. }
  1728. }