Better configuration (#79)

* Configurable Transport (#75)

* new functions to allow HTTPClient configuration

* updated go.mod for testing from remote

* updated go.mod for remote testing

* revert go.mod replace directives

* Fixed NewOrgClientWithTransport comment

* Make client fully configurable

* make empty messages limit configurable #70 #71

* make auth token private in config

* add docs

* lint

---------

Co-authored-by: Michael Fox <m.will.fox@gmail.com>
This commit is contained in:
sashabaranov
2023-02-21 00:16:44 +04:00
committed by GitHub
parent 133d2c9184
commit 1eb5d625f8
10 changed files with 89 additions and 53 deletions

View File

@@ -11,17 +11,18 @@ import (
)
var (
emptyMessagesLimit = 300
ErrTooManyEmptyStreamMessages = errors.New("stream has sent too many empty messages")
)
type CompletionStream struct {
emptyMessagesLimit uint
reader *bufio.Reader
response *http.Response
}
func (stream *CompletionStream) Recv() (response CompletionResponse, err error) {
emptyMessagesCount := 0
var emptyMessagesCount uint
waitForData:
line, err := stream.reader.ReadBytes('\n')
@@ -33,7 +34,7 @@ waitForData:
line = bytes.TrimSpace(line)
if !bytes.HasPrefix(line, headerData) {
emptyMessagesCount++
if emptyMessagesCount > emptyMessagesLimit {
if emptyMessagesCount > stream.emptyMessagesLimit {
err = ErrTooManyEmptyStreamMessages
return
}
@@ -74,18 +75,20 @@ func (c *Client) CreateCompletionStream(
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))
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.authToken))
if err != nil {
return
}
req = req.WithContext(ctx)
resp, err := c.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
if err != nil {
return
}
stream = &CompletionStream{
emptyMessagesLimit: c.config.EmptyMessagesLimit,
reader: bufio.NewReader(resp.Body),
response: resp,
}