small fix
This commit is contained in:
@@ -144,7 +144,7 @@ func (r *Request[P]) DoStreamWithContext(ctx context.Context, api *API) (iter.Se
|
||||
var zero AIResponse
|
||||
for {
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
if err != nil {
|
||||
yield(zero, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"iter"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Server-sent event
|
||||
|
||||
func ReadSSE(r io.ReadCloser) iter.Seq[string] {
|
||||
reader := bufio.NewReader(r)
|
||||
return func(yield func(string) bool) {
|
||||
for {
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if line == "" || line == "\n" {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, "data: ") {
|
||||
line = line[len("data: "):]
|
||||
}
|
||||
line = strings.TrimSpace(line)
|
||||
line = strings.Trim(line, "\r")
|
||||
line = strings.Trim(line, "\n")
|
||||
if strings.HasPrefix(line, "[DONE]") {
|
||||
return
|
||||
}
|
||||
if !yield(line) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user