Implement chat completion streaming (#101)

* Implement chat completion streaming

* Optimize the implementation of chat completion stream

* Fix linter error
This commit is contained in:
Afeyer
2023-03-03 13:52:02 +08:00
committed by GitHub
parent 58d99eb220
commit 39ca4e9488
4 changed files with 142 additions and 14 deletions

View File

@@ -6,7 +6,6 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
@@ -73,18 +72,7 @@ func (c *Client) CreateCompletionStream(
request CompletionRequest,
) (stream *CompletionStream, err error) {
request.Stream = true
reqBytes, err := json.Marshal(request)
if err != nil {
return
}
urlSuffix := "/completions"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, 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.config.authToken))
req, err := c.newStreamRequest(ctx, "POST", "/completions", request)
if err != nil {
return
}