|
| 1 | +package gogpt_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + . "github.com/sashabaranov/go-gpt3" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCreateCompletionStream(t *testing.T) { |
| 13 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 14 | + w.Header().Set("Content-Type", "text/event-stream") |
| 15 | + |
| 16 | + // Send test responses |
| 17 | + dataBytes := []byte{} |
| 18 | + dataBytes = append(dataBytes, []byte("event: message\n")...) |
| 19 | + //nolint:lll |
| 20 | + data := `{"id":"1","object":"completion","created":1598069254,"model":"text-davinci-002","choices":[{"text":"response1","finish_reason":"max_tokens"}]}` |
| 21 | + dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) |
| 22 | + |
| 23 | + dataBytes = append(dataBytes, []byte("event: message\n")...) |
| 24 | + //nolint:lll |
| 25 | + data = `{"id":"2","object":"completion","created":1598069255,"model":"text-davinci-002","choices":[{"text":"response2","finish_reason":"max_tokens"}]}` |
| 26 | + dataBytes = append(dataBytes, []byte("data: "+data+"\n\n")...) |
| 27 | + |
| 28 | + dataBytes = append(dataBytes, []byte("event: done\n")...) |
| 29 | + dataBytes = append(dataBytes, []byte("data: [DONE]\n\n")...) |
| 30 | + |
| 31 | + _, err := w.Write(dataBytes) |
| 32 | + if err != nil { |
| 33 | + t.Errorf("Write error: %s", err) |
| 34 | + } |
| 35 | + })) |
| 36 | + defer server.Close() |
| 37 | + |
| 38 | + // Client portion of the test |
| 39 | + client := NewClient(testAPIToken) |
| 40 | + ctx := context.Background() |
| 41 | + client.BaseURL = server.URL + "/v1" |
| 42 | + |
| 43 | + request := CompletionRequest{ |
| 44 | + Prompt: "Ex falso quodlibet", |
| 45 | + Model: "text-davinci-002", |
| 46 | + MaxTokens: 10, |
| 47 | + Stream: true, |
| 48 | + } |
| 49 | + |
| 50 | + client.HTTPClient.Transport = &tokenRoundTripper{ |
| 51 | + testAPIToken, |
| 52 | + http.DefaultTransport, |
| 53 | + } |
| 54 | + |
| 55 | + stream, err := client.CreateCompletionStream(ctx, request) |
| 56 | + if err != nil { |
| 57 | + t.Errorf("CreateCompletionStream returned error: %v", err) |
| 58 | + } |
| 59 | + defer stream.Close() |
| 60 | + |
| 61 | + expectedResponses := []CompletionResponse{ |
| 62 | + { |
| 63 | + ID: "1", |
| 64 | + Object: "completion", |
| 65 | + Created: 1598069254, |
| 66 | + Model: "text-davinci-002", |
| 67 | + Choices: []CompletionChoice{{Text: "response1", FinishReason: "max_tokens"}}, |
| 68 | + }, |
| 69 | + { |
| 70 | + ID: "2", |
| 71 | + Object: "completion", |
| 72 | + Created: 1598069255, |
| 73 | + Model: "text-davinci-002", |
| 74 | + Choices: []CompletionChoice{{Text: "response2", FinishReason: "max_tokens"}}, |
| 75 | + }, |
| 76 | + {}, |
| 77 | + } |
| 78 | + |
| 79 | + for ix, expectedResponse := range expectedResponses { |
| 80 | + receivedResponse, streamErr := stream.Recv() |
| 81 | + if streamErr != nil { |
| 82 | + t.Errorf("stream.Recv() failed: %v", streamErr) |
| 83 | + } |
| 84 | + if !compareResponses(expectedResponse, receivedResponse) { |
| 85 | + t.Errorf("Stream response %v is %v, expected %v", ix, receivedResponse, expectedResponse) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// A "tokenRoundTripper" is a struct that implements the RoundTripper |
| 91 | +// interface, specifically to handle the authentication token by adding a token |
| 92 | +// to the request header. We need this because the API requires that each |
| 93 | +// request include a valid API token in the headers for authentication and |
| 94 | +// authorization. |
| 95 | +type tokenRoundTripper struct { |
| 96 | + token string |
| 97 | + fallback http.RoundTripper |
| 98 | +} |
| 99 | + |
| 100 | +// RoundTrip takes an *http.Request as input and returns an |
| 101 | +// *http.Response and an error. |
| 102 | +// |
| 103 | +// It is expected to use the provided request to create a connection to an HTTP |
| 104 | +// server and return the response, or an error if one occurred. The returned |
| 105 | +// Response should have its Body closed. If the RoundTrip method returns an |
| 106 | +// error, the Client's Get, Head, Post, and PostForm methods return the same |
| 107 | +// error. |
| 108 | +func (t *tokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| 109 | + req.Header.Set("Authorization", "Bearer "+t.token) |
| 110 | + return t.fallback.RoundTrip(req) |
| 111 | +} |
| 112 | + |
| 113 | +// Helper funcs. |
| 114 | +func compareResponses(r1, r2 CompletionResponse) bool { |
| 115 | + if r1.ID != r2.ID || r1.Object != r2.Object || r1.Created != r2.Created || r1.Model != r2.Model { |
| 116 | + return false |
| 117 | + } |
| 118 | + if len(r1.Choices) != len(r2.Choices) { |
| 119 | + return false |
| 120 | + } |
| 121 | + for i := range r1.Choices { |
| 122 | + if !compareResponseChoices(r1.Choices[i], r2.Choices[i]) { |
| 123 | + return false |
| 124 | + } |
| 125 | + } |
| 126 | + return true |
| 127 | +} |
| 128 | + |
| 129 | +func compareResponseChoices(c1, c2 CompletionChoice) bool { |
| 130 | + if c1.Text != c2.Text || c1.FinishReason != c2.FinishReason { |
| 131 | + return false |
| 132 | + } |
| 133 | + return true |
| 134 | +} |
0 commit comments