From a6e1b2fc5d759da9bbe51365d546d481a0a689d3 Mon Sep 17 00:00:00 2001 From: yoshiaki-yamada Date: Tue, 27 Jun 2023 14:49:05 +0900 Subject: [PATCH] Encode content property with nil value when responsing for function calling --- Sources/OpenAI/Public/Models/ChatQuery.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Sources/OpenAI/Public/Models/ChatQuery.swift b/Sources/OpenAI/Public/Models/ChatQuery.swift index 1ca78826..ad50d605 100644 --- a/Sources/OpenAI/Public/Models/ChatQuery.swift +++ b/Sources/OpenAI/Public/Models/ChatQuery.swift @@ -35,6 +35,25 @@ public struct Chat: Codable, Equatable { self.name = name self.functionCall = functionCall } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(role, forKey: .role) + + if let name = name { + try container.encode(name, forKey: .name) + } + + if let functionCall = functionCall { + try container.encode(functionCall, forKey: .functionCall) + } + + // Should add 'nil' to 'content' property for function calling response + // See https://openai.com/blog/function-calling-and-other-api-updates + if content != nil || (role == .assistant && functionCall != nil) { + try container.encode(content, forKey: .content) + } + } } public struct ChatFunctionCall: Codable, Equatable {