@@ -20,13 +20,12 @@ import android.graphics.Bitmap
20
20
import android.graphics.BitmapFactory
21
21
import android.util.Base64
22
22
import com.google.firebase.vertexai.common.client.Schema
23
- import com.google.firebase.vertexai.common.shared.Blob
24
23
import com.google.firebase.vertexai.common.shared.FileData
25
24
import com.google.firebase.vertexai.common.shared.FunctionCall
26
25
import com.google.firebase.vertexai.common.shared.FunctionCallPart
27
26
import com.google.firebase.vertexai.common.shared.FunctionResponse
28
27
import com.google.firebase.vertexai.common.shared.FunctionResponsePart
29
- import com.google.firebase.vertexai.type.BlobPart
28
+ import com.google.firebase.vertexai.common.shared.InlineData
30
29
import com.google.firebase.vertexai.type.BlockReason
31
30
import com.google.firebase.vertexai.type.Candidate
32
31
import com.google.firebase.vertexai.type.Citation
@@ -39,11 +38,13 @@ import com.google.firebase.vertexai.type.FunctionCallingConfig
39
38
import com.google.firebase.vertexai.type.FunctionDeclaration
40
39
import com.google.firebase.vertexai.type.GenerateContentResponse
41
40
import com.google.firebase.vertexai.type.GenerationConfig
41
+ import com.google.firebase.vertexai.type.HarmBlockMethod
42
42
import com.google.firebase.vertexai.type.HarmBlockThreshold
43
43
import com.google.firebase.vertexai.type.HarmCategory
44
44
import com.google.firebase.vertexai.type.HarmProbability
45
45
import com.google.firebase.vertexai.type.HarmSeverity
46
46
import com.google.firebase.vertexai.type.ImagePart
47
+ import com.google.firebase.vertexai.type.InlineDataPart
47
48
import com.google.firebase.vertexai.type.Part
48
49
import com.google.firebase.vertexai.type.PromptFeedback
49
50
import com.google.firebase.vertexai.type.SafetyRating
@@ -55,6 +56,7 @@ import com.google.firebase.vertexai.type.ToolConfig
55
56
import com.google.firebase.vertexai.type.UsageMetadata
56
57
import com.google.firebase.vertexai.type.content
57
58
import java.io.ByteArrayOutputStream
59
+ import java.util.Calendar
58
60
import kotlinx.serialization.json.Json
59
61
import kotlinx.serialization.json.JsonObject
60
62
import org.json.JSONObject
@@ -71,12 +73,12 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part
71
73
return when (this ) {
72
74
is TextPart -> com.google.firebase.vertexai.common.shared.TextPart (text)
73
75
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))
76
78
)
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 ))
80
82
)
81
83
is com.google.firebase.vertexai.type.FunctionCallPart ->
82
84
FunctionCallPart (FunctionCall (name, args.orEmpty()))
@@ -96,7 +98,8 @@ internal fun Part.toInternal(): com.google.firebase.vertexai.common.shared.Part
96
98
internal fun SafetySetting.toInternal () =
97
99
com.google.firebase.vertexai.common.shared.SafetySetting (
98
100
harmCategory.toInternal(),
99
- threshold.toInternal()
101
+ threshold.toInternal(),
102
+ method.toInternal()
100
103
)
101
104
102
105
internal fun GenerationConfig.toInternal () =
@@ -107,11 +110,13 @@ internal fun GenerationConfig.toInternal() =
107
110
candidateCount = candidateCount,
108
111
maxOutputTokens = maxOutputTokens,
109
112
stopSequences = stopSequences,
113
+ frequencyPenalty = frequencyPenalty,
114
+ presencePenalty = presencePenalty,
110
115
responseMimeType = responseMimeType,
111
116
responseSchema = responseSchema?.toInternal()
112
117
)
113
118
114
- internal fun com.google.firebase.vertexai.type. HarmCategory.toInternal () =
119
+ internal fun HarmCategory.toInternal () =
115
120
when (this ) {
116
121
HarmCategory .HARASSMENT -> com.google.firebase.vertexai.common.shared.HarmCategory .HARASSMENT
117
122
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() =
122
127
HarmCategory .UNKNOWN -> com.google.firebase.vertexai.common.shared.HarmCategory .UNKNOWN
123
128
}
124
129
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
+
125
137
internal fun ToolConfig.toInternal () =
126
138
com.google.firebase.vertexai.common.client.ToolConfig (
127
139
com.google.firebase.vertexai.common.client.FunctionCallingConfig (
@@ -150,7 +162,9 @@ internal fun HarmBlockThreshold.toInternal() =
150
162
}
151
163
152
164
internal 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
+ )
154
168
155
169
internal fun FunctionDeclaration.toInternal () =
156
170
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
191
205
internal fun com.google.firebase.vertexai.common.shared.Part.toPublic (): Part {
192
206
return when (this ) {
193
207
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 -> {
195
209
val data = Base64 .decode(inlineData.data, BASE_64_FLAGS )
196
210
if (inlineData.mimeType.contains(" image" )) {
197
211
ImagePart (decodeBitmapFromImage(data))
198
212
} else {
199
- BlobPart (inlineData.mimeType, data)
213
+ InlineDataPart (inlineData.mimeType, data)
200
214
}
201
215
}
202
216
is FunctionCallPart ->
@@ -218,8 +232,29 @@ internal fun com.google.firebase.vertexai.common.shared.Part.toPublic(): Part {
218
232
}
219
233
}
220
234
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
+ }
223
258
224
259
internal fun com.google.firebase.vertexai.common.server.CitationMetadata.toPublic () =
225
260
CitationMetadata (citationSources.map { it.toPublic() })
0 commit comments