Skip to content

Commit fd41f7a

Browse files
authored
Fix integration test (#762)
* added TestCompletionStream test moved completion stream testing to seperate function added NoErrorF fixes nil pointer reference on stream object * update integration test models
1 parent 8618492 commit fd41f7a

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

api_integration_test.go

+37-27
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAPI(t *testing.T) {
2626
_, err = c.ListEngines(ctx)
2727
checks.NoError(t, err, "ListEngines error")
2828

29-
_, err = c.GetEngine(ctx, "davinci")
29+
_, err = c.GetEngine(ctx, openai.GPT3Davinci002)
3030
checks.NoError(t, err, "GetEngine error")
3131

3232
fileRes, err := c.ListFiles(ctx)
@@ -42,7 +42,7 @@ func TestAPI(t *testing.T) {
4242
"The food was delicious and the waiter",
4343
"Other examples of embedding request",
4444
},
45-
Model: openai.AdaSearchQuery,
45+
Model: openai.AdaEmbeddingV2,
4646
}
4747
_, err = c.CreateEmbeddings(ctx, embeddingReq)
4848
checks.NoError(t, err, "Embedding error")
@@ -77,31 +77,6 @@ func TestAPI(t *testing.T) {
7777
)
7878
checks.NoError(t, err, "CreateChatCompletion (with name) returned error")
7979

80-
stream, err := c.CreateCompletionStream(ctx, openai.CompletionRequest{
81-
Prompt: "Ex falso quodlibet",
82-
Model: openai.GPT3Ada,
83-
MaxTokens: 5,
84-
Stream: true,
85-
})
86-
checks.NoError(t, err, "CreateCompletionStream returned error")
87-
defer stream.Close()
88-
89-
counter := 0
90-
for {
91-
_, err = stream.Recv()
92-
if err != nil {
93-
if errors.Is(err, io.EOF) {
94-
break
95-
}
96-
t.Errorf("Stream error: %v", err)
97-
} else {
98-
counter++
99-
}
100-
}
101-
if counter == 0 {
102-
t.Error("Stream did not return any responses")
103-
}
104-
10580
_, err = c.CreateChatCompletion(
10681
context.Background(),
10782
openai.ChatCompletionRequest{
@@ -134,6 +109,41 @@ func TestAPI(t *testing.T) {
134109
checks.NoError(t, err, "CreateChatCompletion (with functions) returned error")
135110
}
136111

112+
func TestCompletionStream(t *testing.T) {
113+
apiToken := os.Getenv("OPENAI_TOKEN")
114+
if apiToken == "" {
115+
t.Skip("Skipping testing against production OpenAI API. Set OPENAI_TOKEN environment variable to enable it.")
116+
}
117+
118+
c := openai.NewClient(apiToken)
119+
ctx := context.Background()
120+
121+
stream, err := c.CreateCompletionStream(ctx, openai.CompletionRequest{
122+
Prompt: "Ex falso quodlibet",
123+
Model: openai.GPT3Babbage002,
124+
MaxTokens: 5,
125+
Stream: true,
126+
})
127+
checks.NoError(t, err, "CreateCompletionStream returned error")
128+
defer stream.Close()
129+
130+
counter := 0
131+
for {
132+
_, err = stream.Recv()
133+
if err != nil {
134+
if errors.Is(err, io.EOF) {
135+
break
136+
}
137+
t.Errorf("Stream error: %v", err)
138+
} else {
139+
counter++
140+
}
141+
}
142+
if counter == 0 {
143+
t.Error("Stream did not return any responses")
144+
}
145+
}
146+
137147
func TestAPIError(t *testing.T) {
138148
apiToken := os.Getenv("OPENAI_TOKEN")
139149
if apiToken == "" {

completion.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,33 @@ const (
3939
GPT3Dot5Turbo16K0613 = "gpt-3.5-turbo-16k-0613"
4040
GPT3Dot5Turbo = "gpt-3.5-turbo"
4141
GPT3Dot5TurboInstruct = "gpt-3.5-turbo-instruct"
42-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
42+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
4343
GPT3TextDavinci003 = "text-davinci-003"
44-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
44+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
4545
GPT3TextDavinci002 = "text-davinci-002"
46-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
46+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
4747
GPT3TextCurie001 = "text-curie-001"
48-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
48+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
4949
GPT3TextBabbage001 = "text-babbage-001"
50-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
50+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
5151
GPT3TextAda001 = "text-ada-001"
52-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
52+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
5353
GPT3TextDavinci001 = "text-davinci-001"
54-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
54+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
5555
GPT3DavinciInstructBeta = "davinci-instruct-beta"
56-
GPT3Davinci = "davinci"
57-
GPT3Davinci002 = "davinci-002"
58-
// Deprecated: Will be shut down on January 04, 2024. Use gpt-3.5-turbo-instruct instead.
56+
// Deprecated: Model is shutdown. Use davinci-002 instead.
57+
GPT3Davinci = "davinci"
58+
GPT3Davinci002 = "davinci-002"
59+
// Deprecated: Model is shutdown. Use gpt-3.5-turbo-instruct instead.
5960
GPT3CurieInstructBeta = "curie-instruct-beta"
6061
GPT3Curie = "curie"
6162
GPT3Curie002 = "curie-002"
62-
GPT3Ada = "ada"
63-
GPT3Ada002 = "ada-002"
64-
GPT3Babbage = "babbage"
65-
GPT3Babbage002 = "babbage-002"
63+
// Deprecated: Model is shutdown. Use babbage-002 instead.
64+
GPT3Ada = "ada"
65+
GPT3Ada002 = "ada-002"
66+
// Deprecated: Model is shutdown. Use babbage-002 instead.
67+
GPT3Babbage = "babbage"
68+
GPT3Babbage002 = "babbage-002"
6669
)
6770

6871
// Codex Defines the models provided by OpenAI.

embeddings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var ErrVectorLengthMismatch = errors.New("vector length mismatch")
1616
type EmbeddingModel string
1717

1818
const (
19-
// Deprecated: The following block will be shut down on January 04, 2024. Use text-embedding-ada-002 instead.
19+
// Deprecated: The following block is shut down. Use text-embedding-ada-002 instead.
2020
AdaSimilarity EmbeddingModel = "text-similarity-ada-001"
2121
BabbageSimilarity EmbeddingModel = "text-similarity-babbage-001"
2222
CurieSimilarity EmbeddingModel = "text-similarity-curie-001"

internal/test/checks/checks.go

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ func NoError(t *testing.T, err error, message ...string) {
1212
}
1313
}
1414

15+
func NoErrorF(t *testing.T, err error, message ...string) {
16+
t.Helper()
17+
if err != nil {
18+
t.Fatal(err, message)
19+
}
20+
}
21+
1522
func HasError(t *testing.T, err error, message ...string) {
1623
t.Helper()
1724
if err == nil {

0 commit comments

Comments
 (0)