refactor: refactoring http request creation and sending (#395)

* refactoring http request creation and sending

* fix lint error

* increase the test coverage of client.go

* refactor: Change the style of HTTPRequestBuilder.Build func to one-argument-per-line.
This commit is contained in:
渡邉祐一 / Yuichi Watanabe
2023-06-22 18:57:52 +09:00
committed by GitHub
parent 157de0680f
commit f1b66967a4
20 changed files with 215 additions and 132 deletions

View File

@@ -1,11 +1,8 @@
package openai
import (
"bufio"
"context"
"errors"
utils "github.com/sashabaranov/go-openai/internal"
)
var (
@@ -36,27 +33,17 @@ func (c *Client) CreateCompletionStream(
}
request.Stream = true
req, err := c.newStreamRequest(ctx, "POST", urlSuffix, request, request.Model)
req, err := c.newRequest(ctx, "POST", c.fullURL(urlSuffix, request.Model), withBody(request))
if err != nil {
return nil, err
}
resp, err := sendRequestStream[CompletionResponse](c, req)
if err != nil {
return
}
resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
if err != nil {
return
}
if isFailureStatusCode(resp) {
return nil, c.handleErrorResp(resp)
}
stream = &CompletionStream{
streamReader: &streamReader[CompletionResponse]{
emptyMessagesLimit: c.config.EmptyMessagesLimit,
reader: bufio.NewReader(resp.Body),
response: resp,
errAccumulator: utils.NewErrorAccumulator(),
unmarshaler: &utils.JSONUnmarshaler{},
},
streamReader: resp,
}
return
}