Skip to content

Commit 9bc777a

Browse files
author
王聪
committed
兼容deepseek 的reasoning_content 字段
1 parent 85f578b commit 9bc777a

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

chat.go

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ type ChatCompletionMessage struct {
9898
Refusal string `json:"refusal,omitempty"`
9999
MultiContent []ChatMessagePart
100100

101+
// supported by deepseek-reasoner https://api-docs.deepseek.com/
102+
ReasoningContent string `json:"reasoning_content,omitempty"`
103+
101104
// This property isn't in the official documentation, but it's in
102105
// the documentation for the official library for python:
103106
// - https://github.com/openai/openai-python/blob/main/chatml.md
@@ -119,56 +122,60 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
119122
}
120123
if len(m.MultiContent) > 0 {
121124
msg := struct {
122-
Role string `json:"role"`
123-
Content string `json:"-"`
124-
Refusal string `json:"refusal,omitempty"`
125-
MultiContent []ChatMessagePart `json:"content,omitempty"`
126-
Name string `json:"name,omitempty"`
127-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
128-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
129-
ToolCallID string `json:"tool_call_id,omitempty"`
125+
Role string `json:"role"`
126+
Content string `json:"-"`
127+
Refusal string `json:"refusal,omitempty"`
128+
MultiContent []ChatMessagePart `json:"content,omitempty"`
129+
ReasoningContent string `json:"reasoning_content,omitempty"`
130+
Name string `json:"name,omitempty"`
131+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
132+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
133+
ToolCallID string `json:"tool_call_id,omitempty"`
130134
}(m)
131135
return json.Marshal(msg)
132136
}
133137

134138
msg := struct {
135-
Role string `json:"role"`
136-
Content string `json:"content,omitempty"`
137-
Refusal string `json:"refusal,omitempty"`
138-
MultiContent []ChatMessagePart `json:"-"`
139-
Name string `json:"name,omitempty"`
140-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
141-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
142-
ToolCallID string `json:"tool_call_id,omitempty"`
139+
Role string `json:"role"`
140+
Content string `json:"content,omitempty"`
141+
Refusal string `json:"refusal,omitempty"`
142+
MultiContent []ChatMessagePart `json:"-"`
143+
ReasoningContent string `json:"reasoning_content,omitempty"`
144+
Name string `json:"name,omitempty"`
145+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
146+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
147+
ToolCallID string `json:"tool_call_id,omitempty"`
143148
}(m)
144149
return json.Marshal(msg)
145150
}
146151

147152
func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
148153
msg := struct {
149-
Role string `json:"role"`
150-
Content string `json:"content,omitempty"`
151-
Refusal string `json:"refusal,omitempty"`
152-
MultiContent []ChatMessagePart
153-
Name string `json:"name,omitempty"`
154-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
155-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
156-
ToolCallID string `json:"tool_call_id,omitempty"`
154+
Role string `json:"role"`
155+
Content string `json:"content,omitempty"`
156+
Refusal string `json:"refusal,omitempty"`
157+
MultiContent []ChatMessagePart
158+
ReasoningContent string `json:"reasoning_content,omitempty"`
159+
Name string `json:"name,omitempty"`
160+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
161+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
162+
ToolCallID string `json:"tool_call_id,omitempty"`
157163
}{}
158164

159165
if err := json.Unmarshal(bs, &msg); err == nil {
160166
*m = ChatCompletionMessage(msg)
161167
return nil
162168
}
163169
multiMsg := struct {
164-
Role string `json:"role"`
165-
Content string
166-
Refusal string `json:"refusal,omitempty"`
167-
MultiContent []ChatMessagePart `json:"content"`
168-
Name string `json:"name,omitempty"`
169-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
170-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
171-
ToolCallID string `json:"tool_call_id,omitempty"`
170+
Role string `json:"role"`
171+
Content string
172+
Refusal string `json:"refusal,omitempty"`
173+
MultiContent []ChatMessagePart `json:"content"`
174+
ReasoningContent string `json:"reasoning_content,omitempty"`
175+
Name string `json:"name,omitempty"`
176+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
177+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
178+
ToolCallID string `json:"tool_call_id,omitempty"`
172179
}{}
173180
if err := json.Unmarshal(bs, &multiMsg); err != nil {
174181
return err

chat_stream.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
)
77

88
type ChatCompletionStreamChoiceDelta struct {
9-
Content string `json:"content,omitempty"`
10-
Role string `json:"role,omitempty"`
11-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
12-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
13-
Refusal string `json:"refusal,omitempty"`
9+
Content string `json:"content,omitempty"`
10+
Role string `json:"role,omitempty"`
11+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
12+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
13+
Refusal string `json:"refusal,omitempty"`
14+
ReasoningContent string `json:"reasoning_content,omitempty"`
1415
}
1516

1617
type ChatCompletionStreamChoiceLogprobs struct {

0 commit comments

Comments
 (0)