Skip to content

Commit 194a03e

Browse files
authored
Add refusal (#844)
* add custom marshaller, documentation and isolate tests * fix linter * add missing field
1 parent 643da8d commit 194a03e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

chat.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type ChatMessagePart struct {
8282
type ChatCompletionMessage struct {
8383
Role string `json:"role"`
8484
Content string `json:"content"`
85+
Refusal string `json:"refusal,omitempty"`
8586
MultiContent []ChatMessagePart
8687

8788
// This property isn't in the official documentation, but it's in
@@ -107,6 +108,7 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
107108
msg := struct {
108109
Role string `json:"role"`
109110
Content string `json:"-"`
111+
Refusal string `json:"refusal,omitempty"`
110112
MultiContent []ChatMessagePart `json:"content,omitempty"`
111113
Name string `json:"name,omitempty"`
112114
FunctionCall *FunctionCall `json:"function_call,omitempty"`
@@ -115,9 +117,11 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
115117
}(m)
116118
return json.Marshal(msg)
117119
}
120+
118121
msg := struct {
119122
Role string `json:"role"`
120123
Content string `json:"content"`
124+
Refusal string `json:"refusal,omitempty"`
121125
MultiContent []ChatMessagePart `json:"-"`
122126
Name string `json:"name,omitempty"`
123127
FunctionCall *FunctionCall `json:"function_call,omitempty"`
@@ -131,19 +135,22 @@ func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
131135
msg := struct {
132136
Role string `json:"role"`
133137
Content string `json:"content"`
138+
Refusal string `json:"refusal,omitempty"`
134139
MultiContent []ChatMessagePart
135140
Name string `json:"name,omitempty"`
136141
FunctionCall *FunctionCall `json:"function_call,omitempty"`
137142
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
138143
ToolCallID string `json:"tool_call_id,omitempty"`
139144
}{}
145+
140146
if err := json.Unmarshal(bs, &msg); err == nil {
141147
*m = ChatCompletionMessage(msg)
142148
return nil
143149
}
144150
multiMsg := struct {
145151
Role string `json:"role"`
146152
Content string
153+
Refusal string `json:"refusal,omitempty"`
147154
MultiContent []ChatMessagePart `json:"content"`
148155
Name string `json:"name,omitempty"`
149156
FunctionCall *FunctionCall `json:"function_call,omitempty"`

0 commit comments

Comments
 (0)