Skip to content

Add URI-based file data support #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class FunctionCallingViewModel: ObservableObject {
case let .functionCall(functionCall):
messages.insert(functionCall.chatMessage(), at: messages.count - 1)
functionCalls.append(functionCall)
case .data, .functionResponse:
case .data, .fileData, .functionResponse:
fatalError("Unsupported response content.")
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/GoogleAI/Chat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public class Chat {
case let .text(str):
combinedText += str

case .data, .functionCall, .functionResponse:
case .data, .fileData, .functionCall, .functionResponse:
// Don't combine it, just add to the content. If there's any text pending, add that as
// a part.
if !combinedText.isEmpty {
Expand Down
28 changes: 27 additions & 1 deletion Sources/GoogleAI/ModelContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct ModelContent: Codable, Equatable {
enum CodingKeys: String, CodingKey {
case text
case inlineData
case fileData
case functionCall
case functionResponse
}
Expand All @@ -34,12 +35,30 @@ public struct ModelContent: Codable, Equatable {
case bytes = "data"
}

enum FileDataKeys: String, CodingKey {
case mimeType = "mime_type"
case url = "file_uri"
}

/// Text value.
case text(String)

/// Data with a specified media type. Not all media types may be supported by the AI model.
/// Data with a specified media type.
///
/// > Note: Supported media types depends on the model; see
/// > [supported file
/// > formats](https://ai.google.dev/tutorials/prompting_with_media#supported_file_formats)
/// > for details.
case data(mimetype: String, Data)

/// URI-based data with a specified media type.
///
/// > Note: Supported media types depends on the model; see
/// > [supported file
/// > formats](https://ai.google.dev/tutorials/prompting_with_media#supported_file_formats)
/// > for details.
case fileData(mimetype: String, uri: String)

/// A predicted function call returned from the model.
case functionCall(FunctionCall)

Expand Down Expand Up @@ -72,6 +91,13 @@ public struct ModelContent: Codable, Equatable {
)
try inlineDataContainer.encode(mimetype, forKey: .mimeType)
try inlineDataContainer.encode(bytes, forKey: .bytes)
case let .fileData(mimetype: mimetype, url):
var fileDataContainer = container.nestedContainer(
keyedBy: FileDataKeys.self,
forKey: .fileData
)
try fileDataContainer.encode(mimetype, forKey: .mimeType)
try fileDataContainer.encode(url, forKey: .url)
case let .functionCall(functionCall):
try container.encode(functionCall, forKey: .functionCall)
case let .functionResponse(functionResponse):
Expand Down