@@ -20,13 +20,12 @@ import android.graphics.Bitmap
2020import android.graphics.BitmapFactory
2121import android.util.Base64
2222import com.google.firebase.vertexai.common.client.Schema
23- import com.google.firebase.vertexai.common.shared.Blob
2423import com.google.firebase.vertexai.common.shared.FileData
2524import com.google.firebase.vertexai.common.shared.FunctionCall
2625import com.google.firebase.vertexai.common.shared.FunctionCallPart
2726import com.google.firebase.vertexai.common.shared.FunctionResponse
2827import com.google.firebase.vertexai.common.shared.FunctionResponsePart
29- import com.google.firebase.vertexai.type.BlobPart
28+ import com.google.firebase.vertexai.common.shared.InlineData
3029import com.google.firebase.vertexai.type.BlockReason
3130import com.google.firebase.vertexai.type.Candidate
3231import com.google.firebase.vertexai.type.Citation
@@ -39,11 +38,13 @@ import com.google.firebase.vertexai.type.FunctionCallingConfig
3938import com.google.firebase.vertexai.type.FunctionDeclaration
4039import com.google.firebase.vertexai.type.GenerateContentResponse
4140import com.google.firebase.vertexai.type.GenerationConfig
41+ import com.google.firebase.vertexai.type.HarmBlockMethod
4242import com.google.firebase.vertexai.type.HarmBlockThreshold
4343import com.google.firebase.vertexai.type.HarmCategory
4444import com.google.firebase.vertexai.type.HarmProbability
4545import com.google.firebase.vertexai.type.HarmSeverity
4646import com.google.firebase.vertexai.type.ImagePart
47+ import com.google.firebase.vertexai.type.InlineDataPart
4748import com.google.firebase.vertexai.type.Part
4849import com.google.firebase.vertexai.type.PromptFeedback
4950import com.google.firebase.vertexai.type.SafetyRating
@@ -55,6 +56,7 @@ import com.google.firebase.vertexai.type.ToolConfig
5556import com.google.firebase.vertexai.type.UsageMetadata
5657import com.google.firebase.vertexai.type.content
5758import java.io.ByteArrayOutputStream
59+ import java.util.Calendar
5860import kotlinx.serialization.json.Json
5961import kotlinx.serialization.json.JsonObject
6062import org.json.JSONObject
@@ -71,12 +73,12 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part
7173 return when (this ) {
7274 is TextPart -> com.google.firebase.vertexai.common.shared.TextPart (text)
7375 is ImagePart ->
74- com.google.firebase.vertexai.common.shared.BlobPart (
75- Blob (" image/jpeg" , encodeBitmapToBase64Png(image))
76+ com.google.firebase.vertexai.common.shared.InlineDataPart (
77+ InlineData (" image/jpeg" , encodeBitmapToBase64Png(image))
7678 )
77- is BlobPart ->
78- com.google.firebase.vertexai.common.shared.BlobPart (
79- Blob (mimeType, Base64 .encodeToString(blob , BASE_64_FLAGS ))
79+ is InlineDataPart ->
80+ com.google.firebase.vertexai.common.shared.InlineDataPart (
81+ InlineData (mimeType, Base64 .encodeToString(inlineData , BASE_64_FLAGS ))
8082 )
8183 is com.google.firebase.vertexai.type.FunctionCallPart ->
8284 FunctionCallPart (FunctionCall (name, args.orEmpty()))
@@ -96,7 +98,8 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part
9698internal fun SafetySetting.toInternal () =
9799 com.google.firebase.vertexai.common.shared.SafetySetting (
98100 harmCategory.toInternal(),
99- threshold.toInternal()
101+ threshold.toInternal(),
102+ method.toInternal()
100103 )
101104
102105internal fun GenerationConfig.toInternal () =
@@ -107,11 +110,13 @@ internal fun GenerationConfig.toInternal() =
107110 candidateCount = candidateCount,
108111 maxOutputTokens = maxOutputTokens,
109112 stopSequences = stopSequences,
113+ frequencyPenalty = frequencyPenalty,
114+ presencePenalty = presencePenalty,
110115 responseMimeType = responseMimeType,
111116 responseSchema = responseSchema?.toInternal()
112117 )
113118
114- internal fun com.google.firebase.vertexai.type. HarmCategory.toInternal () =
119+ internal fun HarmCategory.toInternal () =
115120 when (this ) {
116121 HarmCategory .HARASSMENT -> com.google.firebase.vertexai.common.shared.HarmCategory .HARASSMENT
117122 HarmCategory .HATE_SPEECH -> com.google.firebase.vertexai.common.shared.HarmCategory .HATE_SPEECH
@@ -122,6 +127,13 @@ internal fun com.google.firebase.vertexai.type.HarmCategory.toInternal() =
122127 HarmCategory .UNKNOWN -> com.google.firebase.vertexai.common.shared.HarmCategory .UNKNOWN
123128 }
124129
130+ internal fun HarmBlockMethod.toInternal () =
131+ when (this ) {
132+ HarmBlockMethod .SEVERITY -> com.google.firebase.vertexai.common.shared.HarmBlockMethod .SEVERITY
133+ HarmBlockMethod .PROBABILITY ->
134+ com.google.firebase.vertexai.common.shared.HarmBlockMethod .PROBABILITY
135+ }
136+
125137internal fun ToolConfig.toInternal () =
126138 com.google.firebase.vertexai.common.client.ToolConfig (
127139 com.google.firebase.vertexai.common.client.FunctionCallingConfig (
@@ -150,7 +162,9 @@ internal fun HarmBlockThreshold.toInternal() =
150162 }
151163
152164internal fun Tool.toInternal () =
153- com.google.firebase.vertexai.common.client.Tool (functionDeclarations.map { it.toInternal() })
165+ com.google.firebase.vertexai.common.client.Tool (
166+ functionDeclarations?.map { it.toInternal() } ? : emptyList()
167+ )
154168
155169internal fun FunctionDeclaration.toInternal () =
156170 com.google.firebase.vertexai.common.client.FunctionDeclaration (name, " " , schema.toInternal())
@@ -191,12 +205,12 @@ internal fun com.google.firebase.vertexai.common.shared.Content.toPublic(): Cont
191205internal fun com.google.firebase.vertexai.common.shared.Part.toPublic (): Part {
192206 return when (this ) {
193207 is com.google.firebase.vertexai.common.shared.TextPart -> TextPart (text)
194- is com.google.firebase.vertexai.common.shared.BlobPart -> {
208+ is com.google.firebase.vertexai.common.shared.InlineDataPart -> {
195209 val data = Base64 .decode(inlineData.data, BASE_64_FLAGS )
196210 if (inlineData.mimeType.contains(" image" )) {
197211 ImagePart (decodeBitmapFromImage(data))
198212 } else {
199- BlobPart (inlineData.mimeType, data)
213+ InlineDataPart (inlineData.mimeType, data)
200214 }
201215 }
202216 is FunctionCallPart ->
@@ -218,8 +232,29 @@ internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part {
218232 }
219233}
220234
221- internal fun com.google.firebase.vertexai.common.server.CitationSources.toPublic () =
222- Citation (startIndex = startIndex, endIndex = endIndex, uri = uri, license = license)
235+ internal fun com.google.firebase.vertexai.common.server.CitationSources.toPublic (): Citation {
236+ val publicationDateAsCalendar =
237+ publicationDate?.let {
238+ val calendar = Calendar .getInstance()
239+ // Internal `Date.year` uses 0 to represent not specified. We use 1 as default.
240+ val year = if (it.year == null || it.year < 1 ) 1 else it.year
241+ // Internal `Date.month` uses 0 to represent not specified, or is 1-12 as months. The month as
242+ // expected by [Calendar] is 0-based, so we subtract 1 or use 0 as default.
243+ val month = if (it.month == null || it.month < 1 ) 0 else it.month - 1
244+ // Internal `Date.day` uses 0 to represent not specified. We use 1 as default.
245+ val day = if (it.day == null || it.day < 1 ) 1 else it.day
246+ calendar.set(year, month, day)
247+ calendar
248+ }
249+ return Citation (
250+ title = title,
251+ startIndex = startIndex,
252+ endIndex = endIndex,
253+ uri = uri,
254+ license = license,
255+ publicationDate = publicationDateAsCalendar
256+ )
257+ }
223258
224259internal fun com.google.firebase.vertexai.common.server.CitationMetadata.toPublic () =
225260 CitationMetadata (citationSources.map { it.toPublic() })
0 commit comments