Skip to content

Commit 08c5636

Browse files
committed
Add URI-based file data support
1 parent fad5c49 commit 08c5636

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Examples/GenerativeAISample/FunctionCallingSample/ViewModels/FunctionCallingViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class FunctionCallingViewModel: ObservableObject {
158158
case let .functionCall(functionCall):
159159
messages.insert(functionCall.chatMessage(), at: messages.count - 1)
160160
functionCalls.append(functionCall)
161-
case .data, .functionResponse:
161+
case .data, .fileData, .functionResponse:
162162
fatalError("Unsupported response content.")
163163
}
164164
}

Sources/GoogleAI/Chat.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public class Chat {
153153
case let .text(str):
154154
combinedText += str
155155

156-
case .data, .functionCall, .functionResponse:
156+
case .data, .fileData, .functionCall, .functionResponse:
157157
// Don't combine it, just add to the content. If there's any text pending, add that as
158158
// a part.
159159
if !combinedText.isEmpty {

Sources/GoogleAI/ModelContent.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct ModelContent: Codable, Equatable {
2525
enum CodingKeys: String, CodingKey {
2626
case text
2727
case inlineData
28+
case fileData
2829
case functionCall
2930
case functionResponse
3031
}
@@ -34,12 +35,24 @@ public struct ModelContent: Codable, Equatable {
3435
case bytes = "data"
3536
}
3637

38+
enum FileDataKeys: String, CodingKey {
39+
case mimeType = "mime_type"
40+
case url = "file_uri"
41+
}
42+
3743
/// Text value.
3844
case text(String)
3945

40-
/// Data with a specified media type. Not all media types may be supported by the AI model.
46+
/// Data with a specified media type.
47+
///
48+
/// > Note: Supported media types depends on the model.
4149
case data(mimetype: String, Data)
4250

51+
/// URI-based data with a specified media type.
52+
///
53+
/// > Note: Supported media types depends on the model.
54+
case fileData(mimetype: String, URL)
55+
4356
/// A predicted function call returned from the model.
4457
case functionCall(FunctionCall)
4558

@@ -72,6 +85,13 @@ public struct ModelContent: Codable, Equatable {
7285
)
7386
try inlineDataContainer.encode(mimetype, forKey: .mimeType)
7487
try inlineDataContainer.encode(bytes, forKey: .bytes)
88+
case let .fileData(mimetype: mimetype, url):
89+
var fileDataContainer = container.nestedContainer(
90+
keyedBy: FileDataKeys.self,
91+
forKey: .fileData
92+
)
93+
try fileDataContainer.encode(mimetype, forKey: .mimeType)
94+
try fileDataContainer.encode(url, forKey: .url)
7595
case let .functionCall(functionCall):
7696
try container.encode(functionCall, forKey: .functionCall)
7797
case let .functionResponse(functionResponse):

0 commit comments

Comments
 (0)