diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/go-chatgpt.iml b/.idea/go-chatgpt.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/go-chatgpt.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..64c204d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/go-gpt3/chat.go b/go-gpt3/chat.go index 81e5a39..9200665 100644 --- a/go-gpt3/chat.go +++ b/go-gpt3/chat.go @@ -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 +}