forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_internal_test.go
147 lines (139 loc) · 2.9 KB
/
api_internal_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
package openai
import (
"context"
"testing"
)
func TestOpenAIFullURL(t *testing.T) {
cases := []struct {
Name string
Suffix string
Expect string
}{
{
"ChatCompletionsURL",
"/chat/completions",
"https://api.openai.com/v1/chat/completions",
},
{
"CompletionsURL",
"/completions",
"https://api.openai.com/v1/completions",
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
az := DefaultConfig("dummy")
cli := NewClientWithConfig(az)
actual := cli.fullURL(c.Suffix)
if actual != c.Expect {
t.Errorf("Expected %s, got %s", c.Expect, actual)
}
t.Logf("Full URL: %s", actual)
})
}
}
func TestRequestAuthHeader(t *testing.T) {
cases := []struct {
Name string
APIType APIType
HeaderKey string
Token string
OrgID string
Expect string
}{
{
"OpenAIDefault",
"",
"Authorization",
"dummy-token-openai",
"",
"Bearer dummy-token-openai",
},
{
"OpenAIOrg",
APITypeOpenAI,
"Authorization",
"dummy-token-openai",
"dummy-org-openai",
"Bearer dummy-token-openai",
},
{
"OpenAI",
APITypeOpenAI,
"Authorization",
"dummy-token-openai",
"",
"Bearer dummy-token-openai",
},
{
"AzureAD",
APITypeAzureAD,
"Authorization",
"dummy-token-azure",
"",
"Bearer dummy-token-azure",
},
{
"Azure",
APITypeAzure,
AzureAPIKeyHeader,
"dummy-api-key-here",
"",
"dummy-api-key-here",
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
az := DefaultConfig(c.Token)
az.APIType = c.APIType
az.OrgID = c.OrgID
cli := NewClientWithConfig(az)
req, err := cli.newStreamRequest(context.Background(), "POST", "/chat/completions", nil)
if err != nil {
t.Errorf("Failed to create request: %v", err)
}
actual := req.Header.Get(c.HeaderKey)
if actual != c.Expect {
t.Errorf("Expected %s, got %s", c.Expect, actual)
}
t.Logf("%s: %s", c.HeaderKey, actual)
})
}
}
func TestAzureFullURL(t *testing.T) {
cases := []struct {
Name string
BaseURL string
Engine string
Expect string
}{
{
"AzureBaseURLWithSlashAutoStrip",
"https://httpbin.org/",
"chatgpt-demo",
"https://httpbin.org/" +
"openai/deployments/chatgpt-demo" +
"/chat/completions?api-version=2023-03-15-preview",
},
{
"AzureBaseURLWithoutSlashOK",
"https://httpbin.org",
"chatgpt-demo",
"https://httpbin.org/" +
"openai/deployments/chatgpt-demo" +
"/chat/completions?api-version=2023-03-15-preview",
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
az := DefaultAzureConfig("dummy", c.BaseURL, c.Engine)
cli := NewClientWithConfig(az)
// /openai/deployments/{engine}/chat/completions?api-version={api_version}
actual := cli.fullURL("/chat/completions")
if actual != c.Expect {
t.Errorf("Expected %s, got %s", c.Expect, actual)
}
t.Logf("Full URL: %s", actual)
})
}
}