MultimodalSnippets.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseCore
  15. import FirebaseVertexAI
  16. import XCTest
  17. #if canImport(UIKit)
  18. import UIKit
  19. #endif // canImport(UIKit)
  20. // These snippet tests are intentionally skipped in CI jobs; see the README file in this directory
  21. // for instructions on running them manually.
  22. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  23. final class MultimodalSnippets: XCTestCase {
  24. let bundle = BundleTestUtil.bundle()
  25. lazy var model = VertexAI.vertexAI().generativeModel(modelName: "gemini-1.5-flash")
  26. lazy var videoURL = {
  27. guard let url = bundle.url(forResource: "animals", withExtension: "mp4") else {
  28. fatalError("Video file animals.mp4 not found in Resources.")
  29. }
  30. return url
  31. }()
  32. override func setUpWithError() throws {
  33. try FirebaseApp.configureDefaultAppForSnippets()
  34. }
  35. override func tearDown() async throws {
  36. await FirebaseApp.deleteDefaultAppForSnippets()
  37. }
  38. #if canImport(UIKit)
  39. func testMultimodalOneImageNonStreaming() async throws {
  40. guard let image = UIImage(systemName: "bicycle") else { fatalError() }
  41. // Provide a text prompt to include with the image
  42. let prompt = "What's in this picture?"
  43. // To generate text output, call generateContent and pass in the prompt
  44. let response = try await model.generateContent(image, prompt)
  45. print(response.text ?? "No text in response.")
  46. }
  47. func testMultimodalOneImageStreaming() async throws {
  48. guard let image = UIImage(systemName: "bicycle") else { fatalError() }
  49. // Provide a text prompt to include with the image
  50. let prompt = "What's in this picture?"
  51. // To stream generated text output, call generateContentStream and pass in the prompt
  52. let contentStream = try model.generateContentStream(image, prompt)
  53. for try await chunk in contentStream {
  54. if let text = chunk.text {
  55. print(text)
  56. }
  57. }
  58. }
  59. func testMultimodalMultiImagesNonStreaming() async throws {
  60. guard let image1 = UIImage(systemName: "car") else { fatalError() }
  61. guard let image2 = UIImage(systemName: "car.2") else { fatalError() }
  62. // Provide a text prompt to include with the images
  63. let prompt = "What's different between these pictures?"
  64. // To generate text output, call generateContent and pass in the prompt
  65. let response = try await model.generateContent(image1, image2, prompt)
  66. print(response.text ?? "No text in response.")
  67. }
  68. func testMultimodalMultiImagesStreaming() async throws {
  69. guard let image1 = UIImage(systemName: "car") else { fatalError() }
  70. guard let image2 = UIImage(systemName: "car.2") else { fatalError() }
  71. // Provide a text prompt to include with the images
  72. let prompt = "What's different between these pictures?"
  73. // To stream generated text output, call generateContentStream and pass in the prompt
  74. let contentStream = try model.generateContentStream(image1, image2, prompt)
  75. for try await chunk in contentStream {
  76. if let text = chunk.text {
  77. print(text)
  78. }
  79. }
  80. }
  81. #endif // canImport(UIKit)
  82. func testMultimodalVideoNonStreaming() async throws {
  83. // Provide the video as `Data` with the appropriate MIME type
  84. let video = try InlineDataPart(data: Data(contentsOf: videoURL), mimeType: "video/mp4")
  85. // Provide a text prompt to include with the video
  86. let prompt = "What is in the video?"
  87. // To generate text output, call generateContent with the text and video
  88. let response = try await model.generateContent(video, prompt)
  89. print(response.text ?? "No text in response.")
  90. }
  91. func testMultimodalVideoStreaming() async throws {
  92. // Provide the video as `Data` with the appropriate MIME type
  93. let video = try InlineDataPart(data: Data(contentsOf: videoURL), mimeType: "video/mp4")
  94. // Provide a text prompt to include with the video
  95. let prompt = "What is in the video?"
  96. // To stream generated text output, call generateContentStream with the text and video
  97. let contentStream = try model.generateContentStream(video, prompt)
  98. for try await chunk in contentStream {
  99. if let text = chunk.text {
  100. print(text)
  101. }
  102. }
  103. }
  104. }