Skip to content

Commit

Permalink
fix: fix zhipu streaming (#349)
Browse files Browse the repository at this point in the history
* Fix #348

* chore: update implementation

---------

Co-authored-by: JustSong <[email protected]>
  • Loading branch information
glzjin and songquanpeng authored Aug 1, 2023
1 parent 30a7f1a commit c2c455c
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions controller/relay-zhipu.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ func zhipuStreamHandler(c *gin.Context, resp *http.Response) (*OpenAIErrorWithSt
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := strings.Index(string(data), "\n"); i >= 0 {
return i + 1, data[0:i], nil
if i := strings.Index(string(data), "\n\n"); i >= 0 && strings.Index(string(data), ":") >= 0 {
return i + 2, data[0:i], nil
}
if atEOF {
return len(data), data, nil
Expand All @@ -208,14 +208,19 @@ func zhipuStreamHandler(c *gin.Context, resp *http.Response) (*OpenAIErrorWithSt
go func() {
for scanner.Scan() {
data := scanner.Text()
data = strings.Trim(data, "\"")
if len(data) < 5 { // ignore blank line or wrong format
continue
}
if data[:5] == "data:" {
dataChan <- data[5:]
} else if data[:5] == "meta:" {
metaChan <- data[5:]
lines := strings.Split(data, "\n")
for i, line := range lines {
if len(line) < 5 {
continue
}
if line[:5] == "data:" {
dataChan <- line[5:]
if i != len(lines)-1 {
dataChan <- "\n"
}
} else if line[:5] == "meta:" {
metaChan <- line[5:]
}
}
}
stopChan <- true
Expand Down

0 comments on commit c2c455c

Please sign in to comment.