Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG FIX] Encode content property with nil value when responding to function calling #84

Merged

Conversation

ysak-y
Copy link
Contributor

@ysak-y ysak-y commented Jun 27, 2023

What

Encode content property with nil value in Chat struct build Chat 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-updates

Chat(role: .assistant, content: nil, functionCall: functionCall),
Chat(role: .function, content: functionContent, name: functionName)

Most importantly, we must encode content property with nil value for the first one. But current encoder behavior removes content property if value is nil which is the default behavior of JSONEncoder.

Actually, if I send above messages to chat endpoint, then I get 400 error as follows because payload doesn't include content property.

{
  "error": {
    "message": "'content' is a required property - 'messages.1'",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

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 remove content property even if it is nil but has functionCall value. by overriding encode(to encoder:) method. Other properties would be encoded same as originally which means it would be included if it has the value, otherwise, not.

@ysak-y ysak-y changed the title Encode content property with nil value when responding to function calling [BUG FIX] Encode content property with nil value when responding to function calling Jun 28, 2023
@Krivoblotsky Krivoblotsky self-requested a review July 8, 2023 08:38
Copy link
Collaborator

@Krivoblotsky Krivoblotsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks!

@Krivoblotsky Krivoblotsky merged commit edfa1e6 into MacPaw:main Jul 10, 2023
@ysak-y ysak-y deleted the send_property_even_if_content_is_nil branch July 10, 2023 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants