Skip to content

Commit d8ab72c

Browse files
ryanwilsonandrewheard
authored andcommitted
Simplify ModelContent initializers.
The initialzer with `some PartsRepresentable` offers a strict subset of the calls possible with the variadic parameter version. The array of PartsRepresentable doesn't gain anything over the variadic parameter version, and can actually lead to compiler confusion and require awkward casting.
1 parent ba223a9 commit d8ab72c

File tree

2 files changed

+17
-55
lines changed

2 files changed

+17
-55
lines changed

FirebaseVertexAI/Sources/ModelContent.swift

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,6 @@ public struct ModelContent: Equatable, Sendable {
106106
// TODO: Refactor this
107107
let internalParts: [InternalPart]
108108

109-
/// Creates a new value from any data or `Array` of data interpretable as a
110-
/// ``Part``. See ``PartsRepresentable`` for types that can be interpreted as `Part`s.
111-
public init(role: String? = "user", parts: some PartsRepresentable) {
112-
self.role = role
113-
var convertedParts = [InternalPart]()
114-
for part in parts.partsValue {
115-
switch part {
116-
case let textPart as TextPart:
117-
convertedParts.append(.text(textPart.text))
118-
case let inlineDataPart as InlineDataPart:
119-
let inlineData = inlineDataPart.inlineData
120-
convertedParts.append(.inlineData(mimetype: inlineData.mimeType, inlineData.data))
121-
case let fileDataPart as FileDataPart:
122-
let fileData = fileDataPart.fileData
123-
convertedParts.append(.fileData(mimetype: fileData.mimeType, uri: fileData.fileURI))
124-
case let functionCallPart as FunctionCallPart:
125-
convertedParts.append(.functionCall(functionCallPart.functionCall))
126-
case let functionResponsePart as FunctionResponsePart:
127-
convertedParts.append(.functionResponse(functionResponsePart.functionResponse))
128-
default:
129-
fatalError()
130-
}
131-
}
132-
internalParts = convertedParts
133-
}
134109

135110
/// Creates a new value from a list of ``Part``s.
136111
public init(role: String? = "user", parts: [any Part]) {
@@ -159,14 +134,7 @@ public struct ModelContent: Equatable, Sendable {
159134

160135
/// Creates a new value from any data interpretable as a ``Part``.
161136
/// See ``PartsRepresentable`` for types that can be interpreted as `Part`s.
162-
public init(role: String? = "user", _ parts: any PartsRepresentable...) {
163-
let content = parts.flatMap { $0.partsValue }
164-
self.init(role: role, parts: content)
165-
}
166-
167-
/// Creates a new value from any data interpretable as a ``Part``.
168-
/// See ``PartsRepresentable``for types that can be interpreted as `Part`s.
169-
public init(role: String? = "user", _ parts: [PartsRepresentable]) {
137+
public init(role: String? = "user", parts: any PartsRepresentable...) {
170138
let content = parts.flatMap { $0.partsValue }
171139
self.init(role: role, parts: content)
172140
}

FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ final class VertexAIAPITests: XCTestCase {
106106
_ = try await genAI.generateContent([str, UIImage(), TextPart(str)])
107107
_ = try await genAI.generateContent(str, UIImage(), "def", UIImage())
108108
_ = try await genAI.generateContent([str, UIImage(), "def", UIImage()])
109-
_ = try await genAI.generateContent([ModelContent("def", UIImage()),
110-
ModelContent("def", UIImage())])
109+
_ = try await genAI.generateContent([ModelContent(parts: "def", UIImage()),
110+
ModelContent(parts: "def", UIImage())])
111111
#elseif canImport(AppKit)
112112
_ = try await genAI.generateContent(NSImage())
113113
_ = try await genAI.generateContent([NSImage()])
@@ -121,37 +121,31 @@ final class VertexAIAPITests: XCTestCase {
121121
let _ = ModelContent(parts: "Constant String")
122122
let _ = ModelContent(parts: str)
123123
let _ = ModelContent(parts: [str])
124-
// Note: without `as [any PartsRepresentable]` this will fail to compile with "Cannot
125-
// convert value of type 'String' to expected element type
126-
// 'Array<Part>.ArrayLiteralElement'. Not sure if there's a way we can get it to
127-
// work.
128124
let _ = ModelContent(
129-
parts: [str, InlineDataPart(data: Data(), mimeType: "foo")] as [any PartsRepresentable]
125+
parts: [str, InlineDataPart(inlineData: InlineData(
126+
mimeType: "foo",
127+
data: Data()
128+
))]
130129
)
131130
#if canImport(UIKit)
132131
_ = ModelContent(role: "user", parts: UIImage())
133132
_ = ModelContent(role: "user", parts: [UIImage()])
134-
// Note: without `as [any PartsRepresentable]` this will fail to compile with "Cannot convert
135-
// value of type `[Any]` to expected type `[any PartsRepresentable]`. Not sure if there's a
136-
// way we can get it to work.
137-
_ = ModelContent(parts: [str, UIImage()] as [any PartsRepresentable])
138-
// Alternatively, you can explicitly declare the type in a variable and pass it in.
133+
_ = ModelContent(parts: [str, UIImage()])
134+
// Note: without explicitly specifying`: [any PartsRepresentable]` this will fail to compile
135+
// below with "Cannot convert value of type `[Any]` to expected type `[any Part]`.
139136
let representable2: [any PartsRepresentable] = [str, UIImage()]
140137
_ = ModelContent(parts: representable2)
141-
_ =
142-
ModelContent(parts: [str, UIImage(), TextPart(str)] as [any PartsRepresentable])
138+
_ = ModelContent(parts: [str, UIImage(), TextPart(str)])
143139
#elseif canImport(AppKit)
144140
_ = ModelContent(role: "user", parts: NSImage())
145141
_ = ModelContent(role: "user", parts: [NSImage()])
146-
// Note: without `as [any PartsRepresentable]` this will fail to compile with "Cannot convert
147-
// value of type `[Any]` to expected type `[any PartsRepresentable]`. Not sure if there's a
148-
// way we can get it to work.
149-
_ = ModelContent(parts: [str, NSImage()] as [any PartsRepresentable])
150-
// Alternatively, you can explicitly declare the type in a variable and pass it in.
142+
_ = ModelContent(parts: [str, NSImage()])
143+
// Note: without explicitly specifying`: [any PartsRepresentable]` this will fail to compile
144+
// below with "Cannot convert value of type `[Any]` to expected type `[any Part]`.
151145
let representable2: [any PartsRepresentable] = [str, NSImage()]
152146
_ = ModelContent(parts: representable2)
153147
_ =
154-
ModelContent(parts: [str, NSImage(), TextPart(str)] as [any PartsRepresentable])
148+
ModelContent(parts: [str, NSImage(), TextPart(str)])
155149
#endif
156150

157151
// countTokens API
@@ -160,8 +154,8 @@ final class VertexAIAPITests: XCTestCase {
160154
let _: CountTokensResponse = try await genAI.countTokens("What color is the Sky?",
161155
UIImage())
162156
let _: CountTokensResponse = try await genAI.countTokens([
163-
ModelContent("What color is the Sky?", UIImage()),
164-
ModelContent(UIImage(), "What color is the Sky?", UIImage()),
157+
ModelContent(parts: "What color is the Sky?", UIImage()),
158+
ModelContent(parts: UIImage(), "What color is the Sky?", UIImage()),
165159
])
166160
#endif
167161

0 commit comments

Comments
 (0)