-
Notifications
You must be signed in to change notification settings - Fork 1
/
openAPI_test.go
221 lines (211 loc) · 5.86 KB
/
openAPI_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"errors"
"fmt"
"github.com/sashabaranov/go-openai"
"testing"
)
func testGetKey() error {
content, err := getKey()
if err != nil {
return err
}
if content == "" {
return errors.New("key is empty")
}
return nil
}
func Test_GetKey(t *testing.T) {
if err := testGetKey(); err != nil {
t.Error(err)
}
if err := testGetKey(); err != nil {
t.Error(err)
}
}
func TestKey_Invalid(t *testing.T) {
if res := key.invalid(); !res {
t.Error("The key was empty before invalidating")
}
if res := key.invalid(); res {
t.Error("The key plain after an invalidation")
}
if string(key) != "" {
t.Error("How tf is it not empty now ?")
}
}
func testGetClient() error {
content, err := GetClient()
if err != nil {
return err
}
if content == nil {
return errors.New("client is nil")
}
return nil
}
func Test_GetClient(t *testing.T) {
if err := testGetClient(); err != nil {
t.Error(err)
}
if err := testGetClient(); err != nil {
t.Error(err)
}
}
func TestOpenClient_Invalid(t *testing.T) {
if res := openClient.invalid(); !res {
t.Error("The client was empty before invalidating")
}
if res := openClient.invalid(); res {
t.Error("The client plain after an invalidation")
}
if openClient.client != nil {
t.Error("How tf is it not empty now ?")
}
}
func TestConversation_ChatCompletion_Empty(t *testing.T) {
choice := openai.ChatCompletionChoice{
Index: 0,
Message: openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: "You are an helpful assistant",
},
FinishReason: "",
}
conversation := Conversation{
ID: "conv1",
LastModel: openai.GPT3Dot5Turbo,
Name: "Conversation 1",
Messages: []Message{gptMessage(choice).toMessage(openai.GPT3Dot5Turbo)},
HasChange: false,
}
question := Message{
Role: roleUser,
Content: "Hello",
FinishReason: finishUser,
Model: openai.GPT3Dot5Turbo,
}
conversation.Messages = append(conversation.Messages, question)
err := conversation.chatCompletion()
if err != nil {
t.Error(err)
}
fmt.Printf("The response : %s\n", conversation.Messages[len(conversation.Messages)-1].Content)
if conversation.Messages[len(conversation.Messages)-1].FinishReason != finishReason(openai.FinishReasonStop) {
t.Errorf("The conversation should have stopped : %s", conversation.Messages[len(conversation.Messages)-1].FinishReason)
}
if len(conversation.Messages) != 3 {
t.Error("The conversation should have 3 messages but have ", len(conversation.Messages))
}
if conversation.HasChange != true {
t.Error("The conversation should have change flag")
}
}
func TestConversation_ChatCompletion_Full(t *testing.T) {
choices := []openai.ChatCompletionChoice{
{
Message: openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: "You are a cool friend",
},
},
{
Message: openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: "Hey",
},
},
{
Message: openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: "Yo",
},
},
}
messages := make([]Message, 3, 5)
for i, choice := range choices {
messages[i] = gptMessage(choice).toMessage(openai.GPT3Dot5Turbo)
}
conversation := Conversation{
ID: "conv2",
LastModel: openai.GPT3Dot5Turbo,
Name: "Conversation 2",
Messages: messages,
HasChange: false,
}
question := Message{
Role: roleUser,
Content: "Say 'banana'",
FinishReason: finishUser,
Model: openai.GPT3Dot5Turbo,
}
conversation.Messages = append(conversation.Messages, question)
err := conversation.chatCompletion()
if err != nil {
t.Error(err)
}
fmt.Printf("The response : %s\n", conversation.Messages[len(conversation.Messages)-1].Content)
if conversation.Messages[len(conversation.Messages)-1].FinishReason != finishReason(openai.FinishReasonStop) {
t.Errorf("The conversation should have stopped : %s", conversation.Messages[len(conversation.Messages)-1].FinishReason)
}
if len(conversation.Messages) != 5 {
t.Error("The conversation should have 5 messages")
}
if conversation.HasChange != true {
t.Error("The conversation should have change flag")
}
}
func TestConversation_ChatCompletion_TooLong(t *testing.T) {
choices := []openai.ChatCompletionChoice{
{
Message: openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: "You are a cool friend",
},
},
{
Message: openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: "Hey",
},
},
{
Message: openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: "Yo",
},
},
}
messages := make([]Message, 3, 5)
for i, choice := range choices {
messages[i] = gptMessage(choice).toMessage(openai.GPT3Dot5Turbo)
}
conversation := Conversation{
ID: "conv3",
LastModel: openai.GPT3Dot5Turbo,
Name: "Conversation 3",
Messages: messages,
HasChange: false,
}
question := Message{
Role: roleUser,
Content: "how do you do ? I do fine for my self, I think the most important thing here is that you feel good too. I would like you to explain to me in a few page how you feel",
FinishReason: finishUser,
Model: openai.GPT3Dot5Turbo,
}
conversation.Messages = append(conversation.Messages, question)
err := conversation.chatCompletionSize(10)
if err != nil {
t.Error(err)
}
fmt.Printf("The response : %s\n", conversation.Messages[len(conversation.Messages)-1].Content)
if conversation.Messages[len(conversation.Messages)-1].FinishReason != finishReason(openai.FinishReasonLength) {
t.Errorf("The conversation should have stopped : %s", conversation.Messages[len(conversation.Messages)-1].FinishReason)
}
if len(conversation.Messages) != 5 {
t.Error("The conversation should have 5 messages")
}
if conversation.HasChange != true {
t.Error("The conversation should not have change flag")
}
}