Streaming support (#61)

* Add streaming support feature (#54)

* Add streaming support feature

removes golangci linting deprecation warnings
See: [Issue #49](https://github.com/sashabaranov/go-gpt3/issues/49)

* remove dead token

* Remove the goroutines from previous implementation

Set up separate test and file for streaming support
Add client code under cmd dir

* Supress CI errors

Need to update import path to test under feature/streaming-support
branch

* suppress linting errors

---------

Co-authored-by: sashabaranov <677093+sashabaranov@users.noreply.github.com>

* remove main.go

* remove code duplication

* use int64

* finalize streaming support

* lint

* fix tests

---------

Co-authored-by: e. alvarez <55966724+ealvar3z@users.noreply.github.com>
This commit is contained in:
sashabaranov
2023-02-07 20:42:53 +04:00
committed by GitHub
parent 3695eb3ade
commit 6758ec4d96
4 changed files with 296 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
@@ -19,7 +20,7 @@ import (
)
const (
testAPIToken = "this-is-my-secure-token-do-not-steal!!"
testAPIToken = "this-is-my-secure-token-do-not-steal!"
)
func TestAPI(t *testing.T) {
@@ -64,6 +65,33 @@ func TestAPI(t *testing.T) {
if err != nil {
t.Fatalf("Embedding error: %v", err)
}
stream, err := c.CreateCompletionStream(ctx, CompletionRequest{
Prompt: "Ex falso quodlibet",
Model: GPT3Ada,
MaxTokens: 5,
Stream: true,
})
if err != nil {
t.Errorf("CreateCompletionStream returned error: %v", err)
}
defer stream.Close()
counter := 0
for {
_, err = stream.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
t.Errorf("Stream error: %v", err)
} else {
counter++
}
}
if counter == 0 {
t.Error("Stream did not return any responses")
}
}
// TestCompletions Tests the completions endpoint of the API using the mocked server.
@@ -272,6 +300,7 @@ func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
http.Error(w, "could not read request", http.StatusInternalServerError)
return
}
res := CompletionResponse{
ID: strconv.Itoa(int(time.Now().Unix())),
Object: "test-object",
@@ -281,6 +310,7 @@ func handleCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
// would be required / wouldn't make much sense
Model: completionReq.Model,
}
// create completions
for i := 0; i < completionReq.N; i++ {
// generate a random string of length completionReq.Length