[Vertex AI] Replace ModelContent.Part enum with protocol/structs (#13767)

This commit is contained in:
Andrew Heard authored and GitHub committed 2024-10-07 15:37:58 -04:00
1 parent a3e7a2090f
commit f27e34d2e5
22 files changed
+700 -498

No files matched your search

@@ -30,7 +30,7 @@ class FunctionCallingViewModel: ObservableObject {
}
/// Function calls pending processing
private var functionCalls = [FunctionCall]()
private var functionCalls = [FunctionCallPart]()
private var model: GenerativeModel
private var chat: Chat
@@ -144,26 +144,26 @@ class FunctionCallingViewModel: ObservableObject {
for part in candidate.content.parts {
switch part {
case let .text(text):
case let textPart as TextPart:
// replace pending message with backend response
messages[messages.count - 1].message += text
messages[messages.count - 1].message += textPart.text
messages[messages.count - 1].pending = false
case let .functionCall(functionCall):
messages.insert(functionCall.chatMessage(), at: messages.count - 1)
functionCalls.append(functionCall)
case .inlineData, .fileData, .functionResponse:
fatalError("Unsupported response content.")
case let functionCallPart as FunctionCallPart:
messages.insert(functionCallPart.chatMessage(), at: messages.count - 1)
functionCalls.append(functionCallPart)
default:
fatalError("Unsupported response part: \(part)")
}
}
}
func processFunctionCalls() async throws -> [FunctionResponse] {
var functionResponses = [FunctionResponse]()
func processFunctionCalls() async throws -> [FunctionResponsePart] {
var functionResponses = [FunctionResponsePart]()
for functionCall in functionCalls {
switch functionCall.name {
case "get_exchange_rate":
let exchangeRates = getExchangeRate(args: functionCall.args)
functionResponses.append(FunctionResponse(
functionResponses.append(FunctionResponsePart(
name: "get_exchange_rate",
response: exchangeRates
))
@@ -208,7 +208,7 @@ class FunctionCallingViewModel: ObservableObject {
}
}
private extension FunctionCall {
private extension FunctionCallPart {
func chatMessage() -> ChatMessage {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
@@ -228,7 +228,7 @@ private extension FunctionCall {
}
}
private extension FunctionResponse {
private extension FunctionResponsePart {
func chatMessage() -> ChatMessage {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
@@ -248,12 +248,8 @@ private extension FunctionResponse {
}
}
private extension [FunctionResponse] {
private extension [FunctionResponsePart] {
func modelContent() -> [ModelContent] {
return self.map { ModelContent(
role: "function",
parts: [ModelContent.Part.functionResponse($0)]
)
}
return self.map { ModelContent(role: "function", parts: [$0]) }
}
}