Skip to content

Commit 83c3b76

Browse files
andrewheardG.Dev.Ssomsak
authored andcommitted
Add URI-based file data support (google-gemini#134)
1 parent dc8d29f commit 83c3b76

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-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
@@ -175,7 +175,7 @@ public class Chat {
175175
case let .text(str):
176176
combinedText += str
177177

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

Sources/GoogleAI/ModelContent.swift

Lines changed: 27 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,30 @@ 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; see
49+
/// > [supported file
50+
/// > formats](https://ai.google.dev/tutorials/prompting_with_media#supported_file_formats)
51+
/// > for details.
4152
case data(mimetype: String, Data)
4253

54+
/// URI-based data with a specified media type.
55+
///
56+
/// > Note: Supported media types depends on the model; see
57+
/// > [supported file
58+
/// > formats](https://ai.google.dev/tutorials/prompting_with_media#supported_file_formats)
59+
/// > for details.
60+
case fileData(mimetype: String, uri: String)
61+
4362
/// A predicted function call returned from the model.
4463
case functionCall(FunctionCall)
4564

@@ -71,6 +90,13 @@ public struct ModelContent: Codable, Equatable {
7190
)
7291
try inlineDataContainer.encode(mimetype, forKey: .mimeType)
7392
try inlineDataContainer.encode(bytes, forKey: .bytes)
93+
case let .fileData(mimetype: mimetype, url):
94+
var fileDataContainer = container.nestedContainer(
95+
keyedBy: FileDataKeys.self,
96+
forKey: .fileData
97+
)
98+
try fileDataContainer.encode(mimetype, forKey: .mimeType)
99+
try fileDataContainer.encode(url, forKey: .url)
74100
case let .functionCall(functionCall):
75101
try container.encode(functionCall, forKey: .functionCall)
76102
case let .functionResponse(functionResponse):

0 commit comments

Comments
 (0)