Skip to content

Commit

Permalink
Encode content property with nil value when responsing for function c…
Browse files Browse the repository at this point in the history
…alling
  • Loading branch information
yoshiaki-yamada committed Jun 27, 2023
1 parent d7f478e commit a6e1b2f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/OpenAI/Public/Models/ChatQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a6e1b2f

Please sign in to comment.