Skip to content

Commit 017b3ec

Browse files
committed
🐞 fix(chatstream): 修复星火大模型和豆包吞字的bug
1 parent 21027ed commit 017b3ec

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
golang的支持调用所有openai范式的ai的api的库
44

5+
[![Go Report Card](https://goreportcard.com/badge/github.com/dingdinglz/openai)](https://goreportcard.com/report/github.com/dingdinglz/openai)
6+
57
## 特征
68

79
- 支持任何符合openai范式的api,如deepseek、kimi等

chat.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"encoding/json"
66
"errors"
7+
"strings"
78
)
89

910
type Message struct {
@@ -180,11 +181,20 @@ func (client Client) ChatStream(model string, messages []Message, during func(st
180181
break
181182
}
182183
if initFlag {
183-
resError, e := parseRealError([]byte(_res))
184-
if e == nil {
184+
inError := _res
185+
if strings.HasPrefix(_res, "data:") {
186+
inError = inError[6:]
187+
}
188+
resError, e := parseRealError([]byte(inError))
189+
if e != nil {
190+
return errors.New(_res)
191+
}
192+
if resError != "" {
185193
return errors.New(resError)
186194
}
187195
initFlag = false
196+
}
197+
if len(_res) < 7 {
188198
continue
189199
}
190200
_res = _res[6:]
@@ -299,11 +309,20 @@ func (client Client) ChatReasonStream(model string, messages []Message, think fu
299309
break
300310
}
301311
if initFlag {
302-
resError, e := parseRealError([]byte(_res))
303-
if e == nil {
312+
inError := _res
313+
if strings.HasPrefix(_res, "data:") {
314+
inError = inError[6:]
315+
}
316+
resError, e := parseRealError([]byte(inError))
317+
if e != nil {
318+
return errors.New(_res)
319+
}
320+
if resError != "" {
304321
return errors.New(resError)
305322
}
306323
initFlag = false
324+
}
325+
if len(_res) < 7 {
307326
continue
308327
}
309328
_res = _res[6:]

chat_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func TestChatStream(t *testing.T) {
6060
})
6161
if e != nil {
6262
t.Error(e.Error())
63+
return
6364
}
6465
e = client.ChatStreamWithConfig(ChatRequest{
6566
Model: "deepseek-chat",

0 commit comments

Comments
 (0)