@@ -63,73 +63,80 @@ struct ServerPromptTemplateIntegrationTests {
6363 #expect( resultText. contains ( " Paul " ) )
6464 }
6565
66- @Test ( arguments: imageGenerationTestConfigs)
66+ @Test ( arguments: [
67+ // templatePredict is only currently supported on Developer API.
68+ InstanceConfig . googleAI_v1beta,
69+ ] )
6770 func generateImages( _ config: InstanceConfig ) async throws {
6871 let imagenModel = FirebaseAI . componentInstance ( config) . templateImagenModel ( )
6972 let imagenPrompt = " A cat picture "
7073 let response = try await imagenModel. generateImages (
71- template: " generate_images.prompt " ,
74+ template: " generate-images2 " ,
7275 variables: [
7376 " prompt " : imagenPrompt,
7477 ]
7578 )
7679 #expect( response. images. count == 3 )
7780 }
7881
79- #if canImport(UIKit )
80- @ Test ( arguments : testConfigs )
81- func generateContentWithMedia ( _ config : InstanceConfig ) async throws {
82- let model = FirebaseAI . componentInstance ( config ) . templateGenerativeModel ( )
82+ @ Test ( arguments : testConfigs )
83+ func generateContentWithMedia ( _ config : InstanceConfig ) async throws {
84+ let model = FirebaseAI . componentInstance ( config ) . templateGenerativeModel ( )
85+ #if canImport(UIKit )
8386 let image = UIImage ( systemName: " photo " ) !
84- let imageBytes = try #require(
85- image. jpegData ( compressionQuality: 0.8 ) , " Could not get image data. "
86- )
87- let base64Image = imageBytes. base64EncodedString ( )
88-
89- let response = try await model. generateContent (
90- template: " media.prompt " ,
91- variables: [
92- " imageData " : [
93- " isInline " : true ,
94- " mimeType " : " image/jpeg " ,
95- " contents " : base64Image,
96- ] ,
97- ]
98- )
99- let text = try #require( response. text)
100- #expect( !text. isEmpty)
101- }
102- #endif // canImport(UIKit)
87+ #elseif canImport(AppKit)
88+ let image = NSImage ( systemSymbolName: " photo " , accessibilityDescription: nil ) !
89+ #endif
90+ let imageBytes = try #require(
91+ image. jpegData ( compressionQuality: 0.8 ) , " Could not get image data. "
92+ )
93+ let base64Image = imageBytes. base64EncodedString ( )
94+
95+ let response = try await model. generateContent (
96+ template: " media.prompt " ,
97+ variables: [
98+ " imageData " : [
99+ " isInline " : true ,
100+ " mimeType " : " image/jpeg " ,
101+ " contents " : base64Image,
102+ ] ,
103+ ]
104+ )
105+ let text = try #require( response. text)
106+ #expect( !text. isEmpty)
107+ }
103108
104- #if canImport(UIKit )
105- @ Test ( arguments : testConfigs )
106- func generateContentStreamWithMedia ( _ config : InstanceConfig ) async throws {
107- let model = FirebaseAI . componentInstance ( config ) . templateGenerativeModel ( )
109+ @ Test ( arguments : testConfigs )
110+ func generateContentStreamWithMedia ( _ config : InstanceConfig ) async throws {
111+ let model = FirebaseAI . componentInstance ( config ) . templateGenerativeModel ( )
112+ #if canImport(UIKit )
108113 let image = UIImage ( systemName: " photo " ) !
109- let imageBytes = try #require(
110- image. jpegData ( compressionQuality: 0.8 ) , " Could not get image data. "
111- )
112- let base64Image = imageBytes. base64EncodedString ( )
113-
114- let stream = try model. generateContentStream (
115- template: " media.prompt " ,
116- variables: [
117- " imageData " : [
118- " isInline " : true ,
119- " mimeType " : " image/jpeg " ,
120- " contents " : base64Image,
121- ] ,
122- ]
123- )
124- var resultText = " "
125- for try await response in stream {
126- if let text = response. text {
127- resultText += text
128- }
114+ #elseif canImport(AppKit)
115+ let image = NSImage ( systemSymbolName: " photo " , accessibilityDescription: nil ) !
116+ #endif
117+ let imageBytes = try #require(
118+ image. jpegData ( compressionQuality: 0.8 ) , " Could not get image data. "
119+ )
120+ let base64Image = imageBytes. base64EncodedString ( )
121+
122+ let stream = try model. generateContentStream (
123+ template: " media.prompt " ,
124+ variables: [
125+ " imageData " : [
126+ " isInline " : true ,
127+ " mimeType " : " image/jpeg " ,
128+ " contents " : base64Image,
129+ ] ,
130+ ]
131+ )
132+ var resultText = " "
133+ for try await response in stream {
134+ if let text = response. text {
135+ resultText += text
129136 }
130- #expect( !resultText. isEmpty)
131137 }
132- #endif // canImport(UIKit)
138+ #expect( !resultText. isEmpty)
139+ }
133140
134141 @Test ( arguments: testConfigs)
135142 func chat( _ config: InstanceConfig ) async throws {
@@ -180,3 +187,20 @@ struct ServerPromptTemplateIntegrationTests {
180187 #expect( textPart. text == userMessage)
181188 }
182189}
190+
191+ #if canImport(AppKit)
192+ import AppKit
193+
194+ extension NSImage {
195+ func jpegData( compressionQuality: CGFloat ) -> Data ? {
196+ guard let tiffRepresentation = tiffRepresentation,
197+ let bitmapImage = NSBitmapImageRep ( data: tiffRepresentation) else {
198+ return nil
199+ }
200+ return bitmapImage. representation (
201+ using: . jpeg,
202+ properties: [ . compressionFactor: compressionQuality]
203+ )
204+ }
205+ }
206+ #endif
0 commit comments