Streaming support (#61)
* Add streaming support feature (#54) * Add streaming support feature removes golangci linting deprecation warnings See: [Issue #49](https://github.com/sashabaranov/go-gpt3/issues/49) * remove dead token * Remove the goroutines from previous implementation Set up separate test and file for streaming support Add client code under cmd dir * Supress CI errors Need to update import path to test under feature/streaming-support branch * suppress linting errors --------- Co-authored-by: sashabaranov <677093+sashabaranov@users.noreply.github.com> * remove main.go * remove code duplication * use int64 * finalize streaming support * lint * fix tests --------- Co-authored-by: e. alvarez <55966724+ealvar3z@users.noreply.github.com>
This commit is contained in:
83
stream.go
Normal file
83
stream.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package gogpt
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CompletionStream struct {
|
||||
reader *bufio.Reader
|
||||
response *http.Response
|
||||
}
|
||||
|
||||
func (stream *CompletionStream) Recv() (response CompletionResponse, err error) {
|
||||
waitForData:
|
||||
line, err := stream.reader.ReadBytes('\n')
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var headerData = []byte("data: ")
|
||||
line = bytes.TrimSpace(line)
|
||||
if !bytes.HasPrefix(line, headerData) {
|
||||
goto waitForData
|
||||
}
|
||||
|
||||
line = bytes.TrimPrefix(line, headerData)
|
||||
if string(line) == "[DONE]" {
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(line, &response)
|
||||
return
|
||||
}
|
||||
|
||||
func (stream *CompletionStream) Close() {
|
||||
stream.response.Body.Close()
|
||||
}
|
||||
|
||||
// CreateCompletionStream — API call to create a completion w/ streaming
|
||||
// support. It sets whether to stream back partial progress. If set, tokens will be
|
||||
// sent as data-only server-sent events as they become available, with the
|
||||
// stream terminated by a data: [DONE] message.
|
||||
func (c *Client) CreateCompletionStream(
|
||||
ctx context.Context,
|
||||
request CompletionRequest,
|
||||
) (stream *CompletionStream, err error) {
|
||||
request.Stream = true
|
||||
reqBytes, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
urlSuffix := "/completions"
|
||||
req, err := http.NewRequest("POST", c.fullURL(urlSuffix), bytes.NewBuffer(reqBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "text/event-stream")
|
||||
req.Header.Set("Cache-Control", "no-cache")
|
||||
req.Header.Set("Connection", "keep-alive")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.authToken))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
req = req.WithContext(ctx)
|
||||
resp, err := c.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stream = &CompletionStream{
|
||||
reader: bufio.NewReader(resp.Body),
|
||||
response: resp,
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user