From 45aa99607be0b4c225af57c36fb5cff7328957de Mon Sep 17 00:00:00 2001 From: saileshd1402 Date: Sat, 1 Feb 2025 00:35:29 +0530 Subject: [PATCH] Make "Content" field in "ChatCompletionMessage" omitempty (#926) --- chat.go | 6 +++--- chat_test.go | 2 +- openai_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/chat.go b/chat.go index 7a44fd8..8ea7238 100644 --- a/chat.go +++ b/chat.go @@ -93,7 +93,7 @@ type ChatMessagePart struct { type ChatCompletionMessage struct { Role string `json:"role"` - Content string `json:"content"` + Content string `json:"content,omitempty"` Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart @@ -132,7 +132,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) { msg := struct { Role string `json:"role"` - Content string `json:"content"` + Content string `json:"content,omitempty"` Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart `json:"-"` Name string `json:"name,omitempty"` @@ -146,7 +146,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) { func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error { msg := struct { Role string `json:"role"` - Content string `json:"content"` + Content string `json:"content,omitempty"` Refusal string `json:"refusal,omitempty"` MultiContent []ChatMessagePart Name string `json:"name,omitempty"` diff --git a/chat_test.go b/chat_test.go index 134026c..cea549c 100644 --- a/chat_test.go +++ b/chat_test.go @@ -631,7 +631,7 @@ func TestMultipartChatMessageSerialization(t *testing.T) { t.Fatalf("Unexpected error") } res = strings.ReplaceAll(string(s), " ", "") - if res != `{"role":"user","content":""}` { + if res != `{"role":"user"}` { t.Fatalf("invalid message: %s", string(s)) } } diff --git a/openai_test.go b/openai_test.go index 48a00b9..6c26eeb 100644 --- a/openai_test.go +++ b/openai_test.go @@ -29,7 +29,7 @@ func setupAzureTestServer() (client *openai.Client, server *test.ServerTest, tea // numTokens Returns the number of GPT-3 encoded tokens in the given text. // This function approximates based on the rule of thumb stated by OpenAI: -// https://beta.openai.com/tokenizer +// https://beta.openai.com/tokenizer/ // // TODO: implement an actual tokenizer for GPT-3 and Codex (once available). func numTokens(s string) int {