-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_test.go
89 lines (76 loc) · 2.04 KB
/
chat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"fmt"
"strings"
"testing"
gpt3 "github.com/PullRequestInc/go-gpt3"
)
type testStream struct {
t *testing.T
response string
request gpt3.ChatCompletionRequest
}
// ChatCompletionStream implements streamClient.
func (t *testStream) ChatCompletionStream(ctx context.Context, request gpt3.ChatCompletionRequest, onData func(*gpt3.ChatCompletionStreamResponse) error) error {
convert := func(s string) *gpt3.ChatCompletionStreamResponse {
return &gpt3.ChatCompletionStreamResponse{
Choices: []gpt3.ChatCompletionStreamResponseChoice{
{
Delta: gpt3.ChatCompletionResponseMessage{
Content: s,
},
},
},
}
}
t.request = request
for i, elem := range strings.Split(t.response, " ") {
t.t.Logf("inserting: %#v", elem)
elem := elem
if i == 0 {
if err := onData(convert(elem)); err != nil {
return err
}
} else {
if err := onData(convert(fmt.Sprintf(" %s", elem))); err != nil {
return err
}
}
}
return nil
}
func TestAIStream(t *testing.T) {
t.Parallel()
t.Run("valid stream", func(t *testing.T) {
stream := &testStream{response: "valid stream", t: t}
var sb strings.Builder
err := aiStream(context.Background(), stream, aiStreamInput{}, func(message string) error {
t.Logf("retrieved: %#v", message)
_, err := sb.WriteString(message)
return err
})
if err != nil {
t.Errorf("stream should not error: %v", err)
}
expected := stream.response
test := sb.String()
if expected != test {
t.Errorf("invalid stream response. Expected %#v got %#v", expected, test)
}
})
t.Run("valid stream", func(t *testing.T) {
stream := &testStream{response: "valid stream", t: t}
var sb strings.Builder
aiStream(context.Background(), stream, aiStreamInput{}, func(message string) error {
t.Logf("retrieved: %#v", message)
_, err := sb.WriteString(message)
return err
})
expected := stream.response
test := sb.String()
if expected != test {
t.Errorf("invalid stream response. Expected %#v got %#v", expected, test)
}
})
}