Skip to content

Commit a3aa451

Browse files
committed
🐞 fix(异常响应): 修复空信息会panic的bug
1 parent 017b3ec commit a3aa451

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

chat.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func (client Client) Chat(model string, messages []Message) (*Message, error) {
115115
}
116116
return nil, errors.New(errorMessage)
117117
}
118+
if len(res.Choices) == 0 {
119+
return &Message{}, nil
120+
}
118121
return &res.Choices[0].Message, nil
119122
}
120123

@@ -148,6 +151,9 @@ func (client Client) EasyChat(model string, prompt string, message string) (stri
148151
}
149152
return "", errors.New(errorMessage)
150153
}
154+
if len(res.Choices) == 0 {
155+
return "", nil
156+
}
151157
return res.Choices[0].Message.Content, nil
152158
}
153159

@@ -203,6 +209,9 @@ func (client Client) ChatStream(model string, messages []Message, during func(st
203209
if e != nil {
204210
return e
205211
}
212+
if len(_json.Choices) == 0 {
213+
continue
214+
}
206215
during(_json.Choices[0].Delta.Content)
207216
}
208217
return nil
@@ -231,6 +240,9 @@ func (client Client) ChatWithConfig(config ChatRequest) (*Message, error) {
231240
}
232241
return nil, errors.New(errorMessage)
233242
}
243+
if len(res.Choices) == 0 {
244+
return &Message{}, nil
245+
}
234246
return &res.Choices[0].Message, nil
235247
}
236248

@@ -274,6 +286,9 @@ func (client Client) ChatStreamWithConfig(config ChatRequest, during func(string
274286
if e != nil {
275287
return e
276288
}
289+
if len(_json.Choices) == 0 {
290+
continue
291+
}
277292
during(_json.Choices[0].Delta.Content)
278293
}
279294
return nil
@@ -331,6 +346,9 @@ func (client Client) ChatReasonStream(model string, messages []Message, think fu
331346
if e != nil {
332347
return e
333348
}
349+
if len(_json.Choices) == 0 {
350+
continue
351+
}
334352
if _json.Choices[0].Delta.ReasoningContent != "" {
335353
think(_json.Choices[0].Delta.ReasoningContent)
336354
}

0 commit comments

Comments
 (0)