Skip to content

Commit 4ed1148

Browse files
committed
Add ModelContent tests for fileData parts
1 parent fcc1084 commit 4ed1148

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

Sources/GoogleAI/ModelContent.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public struct ModelContent: Equatable {
3535

3636
/// URI-based data with a specified media type.
3737
///
38+
/// > Important: Files must be uploaded using the
39+
/// > [`media.upload` REST API](https://ai.google.dev/api/rest/v1beta/media/upload) or another
40+
/// > Gemini SDK.
41+
///
3842
/// > Note: Supported media types depends on the model; see
3943
/// > [supported file
4044
/// > formats](https://ai.google.dev/tutorials/prompting_with_media#supported_file_formats)

Tests/GoogleAITests/GoogleAITests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ final class GoogleGenerativeAITests: XCTestCase {
9090
let _ = try await genAI.generateContent(str)
9191
let _ = try await genAI.generateContent([str])
9292
let _ = try await genAI.generateContent(str, "abc", "def")
93+
let _ = try await genAI.generateContent(
94+
str,
95+
ModelContent.Part.fileData(
96+
mimetype: "image/jpeg",
97+
uri: "https://generativelanguage.googleapis.com/v1beta/files/rand0mha5sh"
98+
)
99+
)
93100
#if canImport(UIKit)
94101
_ = try await genAI.generateContent(UIImage())
95102
_ = try await genAI.generateContent([UIImage()])
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
import GoogleGenerativeAI
17+
import XCTest
18+
19+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
20+
final class ModelContentTests: XCTestCase {
21+
let encoder = JSONEncoder()
22+
23+
override func setUp() {
24+
encoder.outputFormatting = .init(
25+
arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
26+
)
27+
}
28+
29+
// MARK: ModelContent.Part Encoding
30+
31+
func testEncodeFileDataPart() throws {
32+
let mimeType = "image/jpeg"
33+
let fileURI = "gs://test-bucket/image.jpg"
34+
let fileDataPart = ModelContent.Part.fileData(mimetype: mimeType, uri: fileURI)
35+
36+
let jsonData = try encoder.encode(fileDataPart)
37+
38+
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
39+
XCTAssertEqual(json, """
40+
{
41+
"fileData" : {
42+
"file_uri" : "\(fileURI)",
43+
"mime_type" : "\(mimeType)"
44+
}
45+
}
46+
""")
47+
}
48+
}

0 commit comments

Comments
 (0)