Skip to content

Commit ea6a697

Browse files
authored
Merge pull request #147 from songquanpeng/main
Fork Sync: Update from parent repository
2 parents 4bd23ae + c2c455c commit ea6a697

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

controller/relay-zhipu.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ func zhipuStreamHandler(c *gin.Context, resp *http.Response) (*OpenAIErrorWithSt
194194
if atEOF && len(data) == 0 {
195195
return 0, nil, nil
196196
}
197-
if i := strings.Index(string(data), "\n"); i >= 0 {
198-
return i + 1, data[0:i], nil
197+
if i := strings.Index(string(data), "\n\n"); i >= 0 && strings.Index(string(data), ":") >= 0 {
198+
return i + 2, data[0:i], nil
199199
}
200200
if atEOF {
201201
return len(data), data, nil
@@ -208,14 +208,19 @@ func zhipuStreamHandler(c *gin.Context, resp *http.Response) (*OpenAIErrorWithSt
208208
go func() {
209209
for scanner.Scan() {
210210
data := scanner.Text()
211-
data = strings.Trim(data, "\"")
212-
if len(data) < 5 { // ignore blank line or wrong format
213-
continue
214-
}
215-
if data[:5] == "data:" {
216-
dataChan <- data[5:]
217-
} else if data[:5] == "meta:" {
218-
metaChan <- data[5:]
211+
lines := strings.Split(data, "\n")
212+
for i, line := range lines {
213+
if len(line) < 5 {
214+
continue
215+
}
216+
if line[:5] == "data:" {
217+
dataChan <- line[5:]
218+
if i != len(lines)-1 {
219+
dataChan <- "\n"
220+
}
221+
} else if line[:5] == "meta:" {
222+
metaChan <- line[5:]
223+
}
219224
}
220225
}
221226
stopChan <- true

0 commit comments

Comments
 (0)