Skip to content

Commit

Permalink
Add support for FileData (#92)
Browse files Browse the repository at this point in the history
Per [b/330773378](https://b.corp.google.com/issues/330773378),

This adds support for the new `FileData` part type for referencing
storage files. This also adds "support" from the genai side, as a means
of catching future api discrepancies between common and genai.
  • Loading branch information
daymxn committed Mar 22, 2024
1 parent b2afc55 commit 672f0a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changes/crowd-birthday-drink-circle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"PATCH","changes":["Implement error catching for unsupported Part types."]}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ data class Content(@EncodeDefault val role: String? = "user", val parts: List<Pa

@Serializable data class BlobPart(@SerialName("inline_data") val inlineData: Blob) : Part

@Serializable data class FileDataPart(@SerialName("file_data") val fileData: FileData) : Part

@Serializable
data class FileData(
@SerialName("mime_type") val mimeType: String,
@SerialName("file_uri") val fileUri: String
)

@Serializable
data class Blob(
@SerialName("mime_type") val mimeType: String,
Expand All @@ -75,7 +83,8 @@ object PartSerializer : JsonContentPolymorphicSerializer<Part>(Part::class) {
val jsonObject = element.jsonObject
return when {
"text" in jsonObject -> TextPart.serializer()
"inlineData" in jsonObject -> BlobPart.serializer()
"inline_data" in jsonObject -> BlobPart.serializer()
"file_data" in jsonObject -> FileDataPart.serializer()
else -> throw SerializationException("Unknown Part type")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ internal fun Part.toPublic(): com.google.ai.client.generativeai.type.Part {
com.google.ai.client.generativeai.type.BlobPart(inlineData.mimeType, data)
}
}
else ->
throw SerializationException(
"Unsupported part type \"${javaClass.simpleName}\" provided. This model may not be supported by this SDK."
)
}
}

Expand Down

0 comments on commit 672f0a4

Please sign in to comment.