Skip to content

Commit

Permalink
Add support for FileDataPart in the request (#121)
Browse files Browse the repository at this point in the history
We are enabling devs to reference a file in the request, but not ot
upload from the Android SDK

---------

Co-authored-by: Daymon <[email protected]>
  • Loading branch information
rlazo and daymxn authored Apr 19, 2024
1 parent 9476b4f commit c8a0a37
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import com.google.ai.client.generativeai.common.server.SafetyRating
import com.google.ai.client.generativeai.common.shared.Blob
import com.google.ai.client.generativeai.common.shared.BlobPart
import com.google.ai.client.generativeai.common.shared.Content
import com.google.ai.client.generativeai.common.shared.FileData
import com.google.ai.client.generativeai.common.shared.FileDataPart
import com.google.ai.client.generativeai.common.shared.FunctionCall
import com.google.ai.client.generativeai.common.shared.FunctionCallPart
import com.google.ai.client.generativeai.common.shared.FunctionResponse
Expand Down Expand Up @@ -76,6 +78,8 @@ internal fun com.google.ai.client.generativeai.type.Part.toInternal(): Part {
FunctionCallPart(FunctionCall(name, args.orEmpty()))
is com.google.ai.client.generativeai.type.FunctionResponsePart ->
FunctionResponsePart(FunctionResponse(name, response.toInternal()))
is com.google.ai.client.generativeai.type.FileDataPart ->
FileDataPart(FileData(fileUri = uri, mimeType = mimeType))
else ->
throw SerializationException(
"The given subclass of Part (${javaClass.simpleName}) is not supported in the serialization yet."
Expand Down Expand Up @@ -197,6 +201,11 @@ internal fun Part.toPublic(): com.google.ai.client.generativeai.type.Part {
functionResponse.name,
functionResponse.response.toPublic(),
)
is FileDataPart ->
com.google.ai.client.generativeai.type.FileDataPart(
fileData.fileUri,
fileData.mimeType,
)
else ->
throw SerializationException(
"Unsupported part type \"${javaClass.simpleName}\" provided. This model may not be supported by this SDK."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Content @JvmOverloads constructor(val role: String? = "user", val parts: L

@JvmName("addImage") fun image(image: Bitmap) = part(ImagePart(image))

@JvmName("addFileData")
fun fileData(uri: String, mimeType: String) = part(FileDataPart(uri, mimeType))

fun build(): Content = Content(role, parts)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.json.JSONObject
* * [TextPart] representing text or string based data.
* * [ImagePart] representing image data.
* * [BlobPart] representing MIME typed binary data.
* * [FileDataPart] representing MIME typed binary data.
*/
interface Part

Expand All @@ -41,6 +42,12 @@ class ImagePart(val image: Bitmap) : Part
/** Represents binary data with an associated MIME type sent to and received from requests. */
class BlobPart(val mimeType: String, val blob: ByteArray) : Part

/** Represents an URI-based data with a specified media type. */
class FileDataPart(val uri: String, val mimeType: String) : Part

/** @return The part as a [BlobPart] if it represents a blob, and null otherwise */
fun Part.asFileDataPartOrNull(): FileDataPart? = this as? FileDataPart

/** Represents function call name and params received from requests. */
class FunctionCallPart(val name: String, val args: Map<String, String>) : Part

Expand Down

0 comments on commit c8a0a37

Please sign in to comment.