Skip to content

Commit 3837840

Browse files
authored
Merge branch 'main' into rl.vertex.warn
2 parents e751bfe + 8a72ed5 commit 3837840

File tree

14 files changed

+168
-13
lines changed

14 files changed

+168
-13
lines changed

ci/fireci/fireciplugins/api_information.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def api_information(auth_token, repo_name, issue_number):
3737
with open(os.path.join(dir_suffix, filename), 'r') as f:
3838
outputlines = f.readlines()
3939
for line in outputlines:
40-
if 'error' in line:
40+
if 'error:' in line:
4141
formatted_output_lines.append(line[line.find('error:'):])
42-
elif 'warning' in line:
42+
elif 'warning:' in line:
4343
formatted_output_lines.append(line[line.find('warning:'):])
4444

4545
if formatted_output_lines:

firebase-functions/src/androidTest/java/com/google/firebase/functions/StreamTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ class StreamTests {
128128
fun genStreamError_receivesError() = runBlocking {
129129
val input = mapOf("data" to "test error")
130130
val function =
131-
functions.getHttpsCallable("genStreamError").withTimeout(2000, TimeUnit.MILLISECONDS)
131+
functions.getHttpsCallable("genStreamError").withTimeout(10_000, TimeUnit.MILLISECONDS)
132132
val subscriber = StreamSubscriber()
133133

134134
function.stream(input).subscribe(subscriber)
135135

136-
withTimeout(2000) {
136+
withTimeout(10_000) {
137137
while (subscriber.throwable == null) {
138-
delay(100)
138+
delay(1_000)
139139
}
140140
}
141141

firebase-vertexai/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
`GenerativeModel` or `ImagenModel`.
44
* [changed] Added new exception type for quota exceeded scenarios.
55
* [feature] `CountTokenRequest` now includes `GenerationConfig` from the model.
6-
6+
* [changed] **Breaking Change**: `ImagenInlineImage.data` now returns the raw
7+
image bytes (in JPEG or PNG format, as specified in
8+
`ImagenInlineImage.mimeType`) instead of Base64-encoded data. (#6800)
9+
* **Action Required:** Remove any Base64 decoding from your
10+
`ImagenInlineImage.data` usage.
11+
* The `asBitmap()` helper method is unaffected and requires no code changes.
712

813
# 16.2.0
914
* [fixed] Added support for new values sent by the server for `FinishReason` and `BlockReason`.

firebase-vertexai/firebase-vertexai.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ dependencies {
115115
testImplementation(libs.kotlin.coroutines.test)
116116
testImplementation(libs.robolectric)
117117
testImplementation(libs.truth)
118+
testImplementation(libs.mockito.core)
118119

119120
androidTestImplementation(libs.androidx.espresso.core)
120121
androidTestImplementation(libs.androidx.test.junit)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/FirebaseVertexAI.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ internal constructor(
8181
return GenerativeModel(
8282
"projects/${firebaseApp.options.projectId}/locations/${location}/publishers/google/models/${modelName}",
8383
firebaseApp.options.apiKey,
84+
firebaseApp,
8485
generationConfig,
8586
safetySettings,
8687
tools,
@@ -124,6 +125,7 @@ internal constructor(
124125
return ImagenModel(
125126
"projects/${firebaseApp.options.projectId}/locations/${location}/publishers/google/models/${modelName}",
126127
firebaseApp.options.apiKey,
128+
firebaseApp,
127129
generationConfig,
128130
safetySettings,
129131
requestOptions,

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/GenerativeModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.firebase.vertexai
1818

1919
import android.graphics.Bitmap
20+
import com.google.firebase.FirebaseApp
2021
import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
2122
import com.google.firebase.auth.internal.InternalAuthProvider
2223
import com.google.firebase.vertexai.common.APIController
@@ -59,6 +60,7 @@ internal constructor(
5960
internal constructor(
6061
modelName: String,
6162
apiKey: String,
63+
firebaseApp: FirebaseApp,
6264
generationConfig: GenerationConfig? = null,
6365
safetySettings: List<SafetySetting>? = null,
6466
tools: List<Tool>? = null,
@@ -79,6 +81,7 @@ internal constructor(
7981
modelName,
8082
requestOptions,
8183
"gl-kotlin/${KotlinVersion.CURRENT} fire/${BuildConfig.VERSION_NAME}",
84+
firebaseApp,
8285
AppCheckHeaderProvider(TAG, appCheckTokenProvider, internalAuthProvider),
8386
),
8487
)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/ImagenModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.firebase.vertexai
1818

19+
import com.google.firebase.FirebaseApp
1920
import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
2021
import com.google.firebase.auth.internal.InternalAuthProvider
2122
import com.google.firebase.vertexai.common.APIController
@@ -46,6 +47,7 @@ internal constructor(
4647
internal constructor(
4748
modelName: String,
4849
apiKey: String,
50+
firebaseApp: FirebaseApp,
4951
generationConfig: ImagenGenerationConfig? = null,
5052
safetySettings: ImagenSafetySettings? = null,
5153
requestOptions: RequestOptions = RequestOptions(),
@@ -60,6 +62,7 @@ internal constructor(
6062
modelName,
6163
requestOptions,
6264
"gl-kotlin/${KotlinVersion.CURRENT} fire/${BuildConfig.VERSION_NAME}",
65+
firebaseApp,
6366
AppCheckHeaderProvider(TAG, appCheckTokenProvider, internalAuthProvider),
6467
),
6568
)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/common/APIController.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.google.firebase.vertexai.common
1818

1919
import android.util.Log
2020
import com.google.firebase.Firebase
21+
import com.google.firebase.FirebaseApp
2122
import com.google.firebase.options
2223
import com.google.firebase.vertexai.common.util.decodeToFlow
2324
import com.google.firebase.vertexai.common.util.fullModelName
@@ -91,6 +92,9 @@ internal constructor(
9192
private val requestOptions: RequestOptions,
9293
httpEngine: HttpClientEngine,
9394
private val apiClient: String,
95+
private val firebaseApp: FirebaseApp,
96+
private val appVersion: Int = 0,
97+
private val googleAppId: String,
9498
private val headerProvider: HeaderProvider?,
9599
) {
96100

@@ -99,8 +103,19 @@ internal constructor(
99103
model: String,
100104
requestOptions: RequestOptions,
101105
apiClient: String,
106+
firebaseApp: FirebaseApp,
102107
headerProvider: HeaderProvider? = null,
103-
) : this(key, model, requestOptions, OkHttp.create(), apiClient, headerProvider)
108+
) : this(
109+
key,
110+
model,
111+
requestOptions,
112+
OkHttp.create(),
113+
apiClient,
114+
firebaseApp,
115+
getVersionNumber(firebaseApp),
116+
firebaseApp.options.applicationId,
117+
headerProvider
118+
)
104119

105120
private val model = fullModelName(model)
106121

@@ -175,6 +190,10 @@ internal constructor(
175190
contentType(ContentType.Application.Json)
176191
header("x-goog-api-key", key)
177192
header("x-goog-api-client", apiClient)
193+
if (firebaseApp.isDataCollectionDefaultEnabled) {
194+
header("X-Firebase-AppId", googleAppId)
195+
header("X-Firebase-AppVersion", appVersion)
196+
}
178197
}
179198

180199
private suspend fun HttpRequestBuilder.applyHeaderProvider() {
@@ -240,6 +259,16 @@ internal constructor(
240259

241260
companion object {
242261
private val TAG = APIController::class.java.simpleName
262+
263+
private fun getVersionNumber(app: FirebaseApp): Int {
264+
try {
265+
val context = app.applicationContext
266+
return context.packageManager.getPackageInfo(context.packageName, 0).versionCode
267+
} catch (e: Exception) {
268+
Log.d(TAG, "Error while getting app version: ${e.message}")
269+
return 0
270+
}
271+
}
243272
}
244273
}
245274

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenGenerationResponse.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.firebase.vertexai.type
1818

19+
import android.util.Base64
1920
import com.google.firebase.vertexai.ImagenModel
2021
import kotlinx.serialization.Serializable
2122

@@ -53,7 +54,7 @@ internal constructor(public val images: List<T>, public val filteredReason: Stri
5354
val raiFilteredReason: String? = null,
5455
) {
5556
internal fun toPublicInline() =
56-
ImagenInlineImage(bytesBase64Encoded!!.toByteArray(), mimeType!!)
57+
ImagenInlineImage(Base64.decode(bytesBase64Encoded!!, Base64.NO_WRAP), mimeType!!)
5758

5859
internal fun toPublicGCS() = ImagenGCSImage(gcsUri!!, mimeType!!)
5960
}

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/ImagenInlineImage.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ package com.google.firebase.vertexai.type
1818

1919
import android.graphics.Bitmap
2020
import android.graphics.BitmapFactory
21-
import android.util.Base64
2221

2322
/**
24-
* Represents an Imagen-generated image that is contained inline
23+
* Represents an Imagen-generated image that is returned as inline data.
2524
*
26-
* @param data Contains the raw bytes of the image
27-
* @param mimeType Contains the MIME type of the image (for example, `"image/png"`)
25+
* @property data The raw image bytes in JPEG or PNG format, as specified by [mimeType].
26+
* @property mimeType The IANA standard MIME type of the image data; either `"image/png"` or
27+
* `"image/jpeg"`; to request a different format, see [ImagenGenerationConfig.imageFormat].
2828
*/
2929
@PublicPreviewAPI
3030
public class ImagenInlineImage
@@ -34,7 +34,6 @@ internal constructor(public val data: ByteArray, public val mimeType: String) {
3434
* Returns the image as an Android OS native [Bitmap] so that it can be saved or sent to the UI.
3535
*/
3636
public fun asBitmap(): Bitmap {
37-
val data = Base64.decode(data, Base64.NO_WRAP)
3837
return BitmapFactory.decodeByteArray(data, 0, data.size)
3938
}
4039
}

0 commit comments

Comments
 (0)