|
|
@@ -1,10 +1,12 @@ |
|
|
|
package gogpt |
|
|
|
|
|
|
|
import ( |
|
|
|
"bufio" |
|
|
|
"bytes" |
|
|
|
"context" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"net/http" |
|
|
|
) |
|
|
|
|
|
|
@@ -75,3 +77,39 @@ func (c *Client) CreateChatCompletion( |
|
|
|
err = c.sendRequest(req, &response) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// CreateChatCompletionStream — API call to create a completion w/ streaming |
|
|
|
func (c *Client) CreateChatCompletionStream( |
|
|
|
ctx context.Context, |
|
|
|
request CompletionRequest, |
|
|
|
) (stream *CompletionStream, err error) { |
|
|
|
request.Stream = true |
|
|
|
reqBytes, err := json.Marshal(request) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
urlSuffix := "/chat/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)) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
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, |
|
|
|
} |
|
|
|
return |
|
|
|
} |