Skip to content

Commit ec2001f

Browse files
committed
[Vertex AI] Replace ModelContent.Part enum with protocol/structs
1 parent 5632b27 commit ec2001f

File tree

9 files changed

+280
-202
lines changed

9 files changed

+280
-202
lines changed

FirebaseVertexAI/Sample/FunctionCallingSample/ViewModels/FunctionCallingViewModel.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ class FunctionCallingViewModel: ObservableObject {
144144

145145
for part in candidate.content.parts {
146146
switch part {
147-
case let .text(text):
147+
case let text as TextPart:
148148
// replace pending message with backend response
149-
messages[messages.count - 1].message += text
149+
messages[messages.count - 1].message += text.textValue
150150
messages[messages.count - 1].pending = false
151-
case let .functionCall(functionCall):
151+
case let functionCall as FunctionCall:
152152
messages.insert(functionCall.chatMessage(), at: messages.count - 1)
153153
functionCalls.append(functionCall)
154-
case .inlineData, .fileData, .functionResponse:
154+
default:
155155
fatalError("Unsupported response content.")
156156
}
157157
}
@@ -250,10 +250,6 @@ private extension FunctionResponse {
250250

251251
private extension [FunctionResponse] {
252252
func modelContent() -> [ModelContent] {
253-
return self.map { ModelContent(
254-
role: "function",
255-
parts: [ModelContent.Part.functionResponse($0)]
256-
)
257-
}
253+
return self.map { ModelContent(role: "function", parts: [$0]) }
258254
}
259255
}

FirebaseVertexAI/Sources/Chat.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,20 @@ public class Chat {
146146
}
147147

148148
private func aggregatedChunks(_ chunks: [ModelContent]) -> ModelContent {
149-
var parts: [ModelContent.Part] = []
149+
var parts: [any ModelContent.Part] = []
150150
var combinedText = ""
151151
for aggregate in chunks {
152152
// Loop through all the parts, aggregating the text and adding the images.
153153
for part in aggregate.parts {
154154
switch part {
155-
case let .text(str):
156-
combinedText += str
155+
case let textPart as TextPart:
156+
combinedText += textPart.textValue
157157

158-
case .inlineData, .fileData, .functionCall, .functionResponse:
158+
default:
159159
// Don't combine it, just add to the content. If there's any text pending, add that as
160160
// a part.
161161
if !combinedText.isEmpty {
162-
parts.append(.text(combinedText))
162+
parts.append(TextPart(text: combinedText))
163163
combinedText = ""
164164
}
165165

@@ -169,7 +169,7 @@ public class Chat {
169169
}
170170

171171
if !combinedText.isEmpty {
172-
parts.append(.text(combinedText))
172+
parts.append(TextPart(text: combinedText))
173173
}
174174

175175
return ModelContent(role: "model", parts: parts)

FirebaseVertexAI/Sources/GenerateContentResponse.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public struct GenerateContentResponse: Sendable {
4949
return nil
5050
}
5151
let textValues: [String] = candidate.content.parts.compactMap { part in
52-
guard case let .text(text) = part else {
52+
guard let textPart = part as? TextPart else {
5353
return nil
5454
}
55-
return text
55+
return textPart.textValue
5656
}
5757
guard textValues.count > 0 else {
5858
VertexLog.error(
@@ -70,7 +70,7 @@ public struct GenerateContentResponse: Sendable {
7070
return []
7171
}
7272
return candidate.content.parts.compactMap { part in
73-
guard case let .functionCall(functionCall) = part else {
73+
guard let functionCall = part as? FunctionCall else {
7474
return nil
7575
}
7676
return functionCall

0 commit comments

Comments
 (0)