Skip to content

Commit 96ddf0c

Browse files
authored
Merge branch 'main' into daymon-bump-protobuf
2 parents 155159b + 448adae commit 96ddf0c

File tree

13 files changed

+179
-77
lines changed

13 files changed

+179
-77
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version=19.2.0
2-
latestReleasedVersion=19.1.0
1+
version=19.2.1
2+
latestReleasedVersion=19.2.0

firebase-vertexai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
* [changed] Breaking Change: refactored enum classes to be normal classes (#6340).
23

34

45
# 16.0.0-beta05

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ internal fun SafetySetting.toInternal() =
109109
method.toInternal()
110110
)
111111

112+
internal fun makeMissingCaseException(source: String, ordinal: Int): SerializationException {
113+
return SerializationException(
114+
"""
115+
|Missing case for a $source: $ordinal
116+
|This error indicates that one of the `toInternal` conversions needs updating.
117+
|If you're a developer seeing this exception, please file an issue on our GitHub repo:
118+
|https://github.com/firebase/firebase-android-sdk
119+
"""
120+
.trimMargin()
121+
)
122+
}
123+
112124
internal fun GenerationConfig.toInternal() =
113125
com.google.firebase.vertexai.common.client.GenerationConfig(
114126
temperature = temperature,
@@ -132,13 +144,15 @@ internal fun HarmCategory.toInternal() =
132144
HarmCategory.DANGEROUS_CONTENT ->
133145
com.google.firebase.vertexai.common.shared.HarmCategory.DANGEROUS_CONTENT
134146
HarmCategory.UNKNOWN -> com.google.firebase.vertexai.common.shared.HarmCategory.UNKNOWN
147+
else -> throw makeMissingCaseException("HarmCategory", ordinal)
135148
}
136149

137150
internal fun HarmBlockMethod.toInternal() =
138151
when (this) {
139152
HarmBlockMethod.SEVERITY -> com.google.firebase.vertexai.common.shared.HarmBlockMethod.SEVERITY
140153
HarmBlockMethod.PROBABILITY ->
141154
com.google.firebase.vertexai.common.shared.HarmBlockMethod.PROBABILITY
155+
else -> throw makeMissingCaseException("HarmBlockMethod", ordinal)
142156
}
143157

144158
internal fun ToolConfig.toInternal() =
@@ -166,6 +180,7 @@ internal fun HarmBlockThreshold.toInternal() =
166180
com.google.firebase.vertexai.common.shared.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
167181
HarmBlockThreshold.LOW_AND_ABOVE ->
168182
com.google.firebase.vertexai.common.shared.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE
183+
else -> throw makeMissingCaseException("HarmBlockThreshold", ordinal)
169184
}
170185

171186
internal fun Tool.toInternal() =
@@ -217,7 +232,7 @@ internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part {
217232
if (inlineData.mimeType.contains("image")) {
218233
ImagePart(decodeBitmapFromImage(data))
219234
} else {
220-
InlineDataPart(inlineData.mimeType, data)
235+
InlineDataPart(data, inlineData.mimeType)
221236
}
222237
}
223238
is com.google.firebase.vertexai.common.shared.FunctionCallPart ->

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,24 @@ internal constructor(
7070
)
7171

7272
/** The reason for content finishing. */
73-
public enum class FinishReason {
74-
/** A new and not yet supported value. */
75-
UNKNOWN,
73+
public class FinishReason private constructor(public val name: String, public val ordinal: Int) {
74+
public companion object {
75+
/** A new and not yet supported value. */
76+
@JvmField public val UNKNOWN: FinishReason = FinishReason("UNKNOWN", 0)
7677

77-
/** Model finished successfully and stopped. */
78-
STOP,
78+
/** Model finished successfully and stopped. */
79+
@JvmField public val STOP: FinishReason = FinishReason("STOP", 1)
7980

80-
/** Model hit the token limit. */
81-
MAX_TOKENS,
81+
/** Model hit the token limit. */
82+
@JvmField public val MAX_TOKENS: FinishReason = FinishReason("MAX_TOKENS", 2)
8283

83-
/** [SafetySetting] prevented the model from outputting content. */
84-
SAFETY,
84+
/** [SafetySetting] prevented the model from outputting content. */
85+
@JvmField public val SAFETY: FinishReason = FinishReason("SAFETY", 3)
8586

86-
/** Model began looping. */
87-
RECITATION,
87+
/** Model began looping. */
88+
@JvmField public val RECITATION: FinishReason = FinishReason("RECITATION", 4)
8889

89-
/** Model stopped for another reason. */
90-
OTHER
90+
/** Model stopped for another reason. */
91+
@JvmField public val OTHER: FinishReason = FinishReason("OTHER", 5)
92+
}
9193
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ constructor(public val role: String? = "user", public val parts: List<Part>) {
5656
*/
5757
@JvmName("addInlineData")
5858
public fun inlineData(mimeType: String, bytes: ByteArray): Content.Builder =
59-
part(InlineDataPart(mimeType, bytes))
59+
part(InlineDataPart(bytes, mimeType))
6060

6161
/** Wraps the provided [image] inside an [ImagePart] and adds it to the [parts] list. */
6262
@JvmName("addImage") public fun image(image: Bitmap): Content.Builder = part(ImagePart(image))

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ package com.google.firebase.vertexai.type
2020
* Specifies how the block method computes the score that will be compared against the
2121
* [HarmBlockThreshold] in [SafetySetting].
2222
*/
23-
public enum class HarmBlockMethod {
24-
/**
25-
* The harm block method uses both probability and severity scores. See [HarmSeverity] and
26-
* [HarmProbability].
27-
*/
28-
SEVERITY,
29-
/** The harm block method uses the probability score. See [HarmProbability]. */
30-
PROBABILITY,
23+
public class HarmBlockMethod private constructor(public val ordinal: Int) {
24+
public companion object {
25+
/**
26+
* The harm block method uses both probability and severity scores. See [HarmSeverity] and
27+
* [HarmProbability].
28+
*/
29+
@JvmField public val SEVERITY: HarmBlockMethod = HarmBlockMethod(0)
30+
31+
/** The harm block method uses the probability score. See [HarmProbability]. */
32+
@JvmField public val PROBABILITY: HarmBlockMethod = HarmBlockMethod(1)
33+
}
3134
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
package com.google.firebase.vertexai.type
1818

1919
/** Represents the threshold for a [HarmCategory] to be allowed by [SafetySetting]. */
20-
public enum class HarmBlockThreshold {
21-
/** Content with negligible harm is allowed. */
22-
LOW_AND_ABOVE,
20+
public class HarmBlockThreshold private constructor(public val ordinal: Int) {
21+
public companion object {
22+
/** Content with negligible harm is allowed. */
23+
@JvmField public val LOW_AND_ABOVE: HarmBlockThreshold = HarmBlockThreshold(0)
2324

24-
/** Content with negligible to low harm is allowed. */
25-
MEDIUM_AND_ABOVE,
25+
/** Content with negligible to low harm is allowed. */
26+
@JvmField public val MEDIUM_AND_ABOVE: HarmBlockThreshold = HarmBlockThreshold(1)
2627

27-
/** Content with negligible to medium harm is allowed. */
28-
ONLY_HIGH,
28+
/** Content with negligible to medium harm is allowed. */
29+
@JvmField public val ONLY_HIGH: HarmBlockThreshold = HarmBlockThreshold(2)
2930

30-
/** All content is allowed regardless of harm. */
31-
NONE
31+
/** All content is allowed regardless of harm. */
32+
@JvmField public val NONE: HarmBlockThreshold = HarmBlockThreshold(3)
33+
}
3234
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
package com.google.firebase.vertexai.type
1818

1919
/** Category for a given harm rating. */
20-
public enum class HarmCategory {
21-
/** A new and not yet supported value. */
22-
UNKNOWN,
20+
public class HarmCategory private constructor(public val ordinal: Int) {
21+
public companion object {
22+
/** A new and not yet supported value. */
23+
@JvmField public val UNKNOWN: HarmCategory = HarmCategory(0)
2324

24-
/** Harassment content. */
25-
HARASSMENT,
25+
/** Harassment content. */
26+
@JvmField public val HARASSMENT: HarmCategory = HarmCategory(1)
2627

27-
/** Hate speech and content. */
28-
HATE_SPEECH,
28+
/** Hate speech and content. */
29+
@JvmField public val HATE_SPEECH: HarmCategory = HarmCategory(2)
2930

30-
/** Sexually explicit content. */
31-
SEXUALLY_EXPLICIT,
31+
/** Sexually explicit content. */
32+
@JvmField public val SEXUALLY_EXPLICIT: HarmCategory = HarmCategory(3)
3233

33-
/** Dangerous content. */
34-
DANGEROUS_CONTENT
34+
/** Dangerous content. */
35+
@JvmField public val DANGEROUS_CONTENT: HarmCategory = HarmCategory(4)
36+
}
3537
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
package com.google.firebase.vertexai.type
1818

1919
/** Represents the probability that some [HarmCategory] is applicable in a [SafetyRating]. */
20-
public enum class HarmProbability {
21-
/** A new and not yet supported value. */
22-
UNKNOWN,
20+
public class HarmProbability private constructor(public val ordinal: Int) {
21+
public companion object {
22+
/** A new and not yet supported value. */
23+
@JvmField public val UNKNOWN: HarmProbability = HarmProbability(0)
2324

24-
/** Probability for harm is negligible. */
25-
NEGLIGIBLE,
25+
/** Probability for harm is negligible. */
26+
@JvmField public val NEGLIGIBLE: HarmProbability = HarmProbability(1)
2627

27-
/** Probability for harm is low. */
28-
LOW,
28+
/** Probability for harm is low. */
29+
@JvmField public val LOW: HarmProbability = HarmProbability(2)
2930

30-
/** Probability for harm is medium. */
31-
MEDIUM,
31+
/** Probability for harm is medium. */
32+
@JvmField public val MEDIUM: HarmProbability = HarmProbability(3)
3233

33-
/** Probability for harm is high. */
34-
HIGH,
34+
/** Probability for harm is high. */
35+
@JvmField public val HIGH: HarmProbability = HarmProbability(4)
36+
}
3537
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
package com.google.firebase.vertexai.type
1818

1919
/** Represents the severity of a [HarmCategory] being applicable in a [SafetyRating]. */
20-
public enum class HarmSeverity {
21-
/** A new and not yet supported value. */
22-
UNKNOWN,
20+
public class HarmSeverity private constructor(public val ordinal: Int) {
21+
public companion object {
22+
/** A new and not yet supported value. */
23+
@JvmField public val UNKNOWN: HarmSeverity = HarmSeverity(0)
2324

24-
/** Severity for harm is negligible. */
25-
NEGLIGIBLE,
25+
/** Severity for harm is negligible. */
26+
@JvmField public val NEGLIGIBLE: HarmSeverity = HarmSeverity(1)
2627

27-
/** Low level of harm severity. */
28-
LOW,
28+
/** Low level of harm severity. */
29+
@JvmField public val LOW: HarmSeverity = HarmSeverity(2)
2930

30-
/** Medium level of harm severity. */
31-
MEDIUM,
31+
/** Medium level of harm severity. */
32+
@JvmField public val MEDIUM: HarmSeverity = HarmSeverity(3)
3233

33-
/** High level of harm severity. */
34-
HIGH,
34+
/** High level of harm severity. */
35+
@JvmField public val HIGH: HarmSeverity = HarmSeverity(4)
36+
}
3537
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ public class ImagePart(public val image: Bitmap) : Part
3737
/**
3838
* Represents binary data with an associated MIME type sent to and received from requests.
3939
*
40+
* @param inlineData the binary data as a [ByteArray]
4041
* @param mimeType an IANA standard MIME type. For supported values, see the
4142
* [Vertex AI documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/send-multimodal-prompts#media_requirements)
42-
* .
43-
* @param inlineData the binary data as a [ByteArray]
4443
*/
45-
public class InlineDataPart(public val mimeType: String, public val inlineData: ByteArray) : Part
44+
public class InlineDataPart(public val inlineData: ByteArray, public val mimeType: String) : Part
4645

4746
/**
4847
* Represents a function call request from the model

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ public class PromptFeedback(
3030
)
3131

3232
/** Describes why content was blocked. */
33-
public enum class BlockReason {
34-
/** A new and not yet supported value. */
35-
UNKNOWN,
33+
public class BlockReason private constructor(public val name: String, public val ordinal: Int) {
34+
public companion object {
35+
/** A new and not yet supported value. */
36+
@JvmField public val UNKNOWN: BlockReason = BlockReason("UNKNOWN", 0)
3637

37-
/** Content was blocked for violating provided [SafetySetting]. */
38-
SAFETY,
38+
/** Content was blocked for violating provided [SafetySetting]. */
39+
@JvmField public val SAFETY: BlockReason = BlockReason("SAFETY", 1)
3940

40-
/** Content was blocked for another reason. */
41-
OTHER
41+
/** Content was blocked for another reason. */
42+
@JvmField public val OTHER: BlockReason = BlockReason("OTHER", 2)
43+
}
4244
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.vertexai.common
18+
19+
import com.google.firebase.vertexai.internal.util.toInternal
20+
import com.google.firebase.vertexai.type.HarmBlockMethod
21+
import com.google.firebase.vertexai.type.HarmBlockThreshold
22+
import com.google.firebase.vertexai.type.HarmCategory
23+
import org.junit.Test
24+
25+
/**
26+
* Fetches all the `@JvmStatic` properties of a class that are instances of the class itself.
27+
*
28+
* For example, given the following class:
29+
* ```kt
30+
* public class HarmCategory private constructor(public val ordinal: Int) {
31+
* public companion object {
32+
* @JvmField public val UNKNOWN: HarmCategory = HarmCategory(0)
33+
* @JvmField public val HARASSMENT: HarmCategory = HarmCategory(1)
34+
* }
35+
* }
36+
* ```
37+
* This function will yield:
38+
* ```kt
39+
* [UNKNOWN, HARASSMENT]
40+
* ```
41+
*/
42+
internal inline fun <reified T : Any> getEnumValues(): List<T> {
43+
return T::class
44+
.java
45+
.declaredFields
46+
.filter { it.type == T::class.java }
47+
.mapNotNull { it.get(null) as? T }
48+
}
49+
50+
/**
51+
* Ensures that whenever any of our "pseudo-enums" are updated, that the conversion layer is also
52+
* updated.
53+
*/
54+
internal class EnumUpdateTests {
55+
@Test
56+
fun `HarmCategory#toInternal() covers all values`() {
57+
val values = getEnumValues<HarmCategory>()
58+
values.forEach { it.toInternal() }
59+
}
60+
61+
@Test
62+
fun `HarmBlockMethod#toInternal() covers all values`() {
63+
val values = getEnumValues<HarmBlockMethod>()
64+
values.forEach { it.toInternal() }
65+
}
66+
67+
@Test
68+
fun `HarmBlockThreshold#toInternal() covers all values`() {
69+
val values = getEnumValues<HarmBlockThreshold>()
70+
values.forEach { it.toInternal() }
71+
}
72+
}

0 commit comments

Comments
 (0)