[BUG FIX] Encode content property with nil value when responding to function calling #84
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Encode
content
property withnil
value inChat
struct buildChat
instance for responding to function calling result.Why
We must send two chat instances when sending function calling result to OpenAI.
Ref:
Function calling example
section of https://openai.com/blog/function-calling-and-other-api-updatesMost importantly, we must encode
content
property withnil
value for the first one. But current encoder behavior removescontent
property if value isnil
which is the default behavior ofJSONEncoder
.Actually, if I send above messages to chat endpoint, then I get 400 error as follows because payload doesn't include
content
property.Payload doesn't have
content
property actually because it is nil value.{"role":"assistant","function_call":{"name":"add","arguments":"{\"a\": \"1\", \"b\": \"1\"}"}}
So I want to fix this behavior.
Affected Areas
I modified encoding behavior of
Chat
struct not to removecontent
property even if it is nil but hasfunctionCall
value. by overridingencode(to encoder:)
method. Other properties would be encoded same as originally which means it would be included if it has the value, otherwise, not.