@@ -13,7 +13,6 @@ import (
1313 "mime"
1414 "net/http"
1515 "os"
16- "regexp"
1716 "strings"
1817 "testing"
1918
@@ -76,7 +75,7 @@ func getEnvVariable(varName string, playbackValue string) string {
7675 return val
7776}
7877
79- var azureOpenAI , openAI , servers = func () (testVars , testVars , [] string ) {
78+ var azureOpenAI , openAI = func () (testVars , testVars ) {
8079 servers := struct {
8180 USEast endpoint
8281 USNorthCentral endpoint
@@ -85,41 +84,32 @@ var azureOpenAI, openAI, servers = func() (testVars, testVars, []string) {
8584 OpenAI endpoint
8685 }{
8786 OpenAI : endpoint {
88- URL : getEndpoint ("OPENAI_ENDPOINT" ), // ex: https://api.openai.com/v1/
87+ URL : getEndpoint ("OPENAI_ENDPOINT" , false ), // ex: https://api.openai.com/v1/
8988 APIKey : getEnvVariable ("OPENAI_API_KEY" , fakeAPIKey ),
9089 Azure : false ,
9190 },
9291 USEast : endpoint {
93- URL : getEndpoint ("AOAI_ENDPOINT_USEAST" ),
92+ URL : getEndpoint ("AOAI_ENDPOINT_USEAST" , true ),
9493 APIKey : getEnvVariable ("AOAI_ENDPOINT_USEAST_API_KEY" , fakeAPIKey ),
9594 Azure : true ,
9695 },
9796 USEast2 : endpoint {
98- URL : getEndpoint ("AOAI_ENDPOINT_USEAST2" ),
97+ URL : getEndpoint ("AOAI_ENDPOINT_USEAST2" , true ),
9998 APIKey : getEnvVariable ("AOAI_ENDPOINT_USEAST2_API_KEY" , fakeAPIKey ),
10099 Azure : true ,
101100 },
102101 USNorthCentral : endpoint {
103- URL : getEndpoint ("AOAI_ENDPOINT_USNORTHCENTRAL" ),
102+ URL : getEndpoint ("AOAI_ENDPOINT_USNORTHCENTRAL" , true ),
104103 APIKey : getEnvVariable ("AOAI_ENDPOINT_USNORTHCENTRAL_API_KEY" , fakeAPIKey ),
105104 Azure : true ,
106105 },
107106 SWECentral : endpoint {
108- URL : getEndpoint ("AOAI_ENDPOINT_SWECENTRAL" ),
107+ URL : getEndpoint ("AOAI_ENDPOINT_SWECENTRAL" , true ),
109108 APIKey : getEnvVariable ("AOAI_ENDPOINT_SWECENTRAL_API_KEY" , fakeAPIKey ),
110109 Azure : true ,
111110 },
112111 }
113112
114- // used when we setup the recording policy
115- endpoints := []string {
116- servers .OpenAI .URL ,
117- servers .USEast .URL ,
118- servers .USEast2 .URL ,
119- servers .USNorthCentral .URL ,
120- servers .SWECentral .URL ,
121- }
122-
123113 newTestVarsFn := func (azure bool ) testVars {
124114 return testVars {
125115 ChatCompletions : endpointWithModel {
@@ -172,11 +162,9 @@ var azureOpenAI, openAI, servers = func() (testVars, testVars, []string) {
172162 },
173163 Cognitive : azopenai.AzureSearchChatExtensionConfiguration {
174164 Parameters : & azopenai.AzureSearchChatExtensionParameters {
175- Endpoint : to .Ptr (recording .GetEnvVariable ("COGNITIVE_SEARCH_API_ENDPOINT" , fakeCognitiveEndpoint )),
176- IndexName : to .Ptr (recording .GetEnvVariable ("COGNITIVE_SEARCH_API_INDEX" , fakeCognitiveIndexName )),
177- Authentication : & azopenai.OnYourDataAPIKeyAuthenticationOptions {
178- Key : to .Ptr (recording .GetEnvVariable ("COGNITIVE_SEARCH_API_KEY" , fakeAPIKey )),
179- },
165+ Endpoint : to .Ptr (recording .GetEnvVariable ("COGNITIVE_SEARCH_API_ENDPOINT" , fakeCognitiveEndpoint )),
166+ IndexName : to .Ptr (recording .GetEnvVariable ("COGNITIVE_SEARCH_API_INDEX" , fakeCognitiveIndexName )),
167+ Authentication : & azopenai.OnYourDataSystemAssignedManagedIdentityAuthenticationOptions {},
180168 },
181169 },
182170 }
@@ -195,8 +183,8 @@ var azureOpenAI, openAI, servers = func() (testVars, testVars, []string) {
195183 "DALLE" : azureTestVars .DallE ,
196184 "EMBEDDINGS" : azureTestVars .Embeddings ,
197185 // these resources are oversubscribed and occasionally fail in live testing.
198- //"VISION": azureTestVars.Vision,
199- //"WHISPER": azureTestVars.Whisper,
186+ // "VISION": azureTestVars.Vision,
187+ // "WHISPER": azureTestVars.Whisper,
200188 }
201189
202190 for area , epm := range remaps {
@@ -206,7 +194,7 @@ var azureOpenAI, openAI, servers = func() (testVars, testVars, []string) {
206194 }
207195 }
208196
209- return azureTestVars , newTestVarsFn (false ), endpoints
197+ return azureTestVars , newTestVarsFn (false )
210198}()
211199
212200type testClientOption func (opt * azopenai.ClientOptions )
@@ -241,9 +229,10 @@ func newTestClient(t *testing.T, ep endpoint, options ...testClientOption) *azop
241229 }
242230}
243231
244- const fakeEndpoint = "https://fake-recorded-host.microsoft.com/"
232+ const fakeAzureEndpoint = "https://Sanitized.openai.azure.com/"
233+ const fakeOpenAIEndpoint = "https://Sanitized.openai.com/v1"
245234const fakeAPIKey = "redacted"
246- const fakeCognitiveEndpoint = "https://fake-cognitive-endpoint.microsoft .com"
235+ const fakeCognitiveEndpoint = "https://Sanitized.openai.azure .com"
247236const fakeCognitiveIndexName = "index"
248237
249238type MultipartRecordingPolicy struct {
@@ -266,23 +255,12 @@ func newRecordingTransporter(t *testing.T) policy.Transporter {
266255 err = recording .AddHeaderRegexSanitizer ("Api-Key" , fakeAPIKey , "" , nil )
267256 require .NoError (t , err )
268257
269- err = recording .AddHeaderRegexSanitizer ("User -Agent" , "fake-user-agent" , "" , nil )
258+ err = recording .AddHeaderRegexSanitizer ("Uer -Agent" , "fake-user-agent" , "" , nil )
270259 require .NoError (t , err )
271260
272- for _ , ep := range servers {
273- err = recording .AddURISanitizer (fakeEndpoint , regexp .QuoteMeta (ep ), nil )
274- require .NoError (t , err )
275- }
276-
277261 err = recording .AddURISanitizer ("/openai/operations/images/00000000-AAAA-BBBB-CCCC-DDDDDDDDDDDD" , "/openai/operations/images/[A-Za-z-0-9]+" , nil )
278262 require .NoError (t , err )
279263
280- // there's only one OpenAI endpoint
281- if openAI .ChatCompletions .Endpoint .URL != "" {
282- err = recording .AddURISanitizer (fakeEndpoint , regexp .QuoteMeta (openAI .ChatCompletions .Endpoint .URL ), nil )
283- require .NoError (t , err )
284- }
285-
286264 err = recording .AddGeneralRegexSanitizer (
287265 fmt .Sprintf (`"endpoint": "%s"` , fakeCognitiveEndpoint ),
288266 fmt .Sprintf (`"endpoint":\s*"%s"` , * azureOpenAI .Cognitive .Parameters .Endpoint ), nil )
@@ -292,11 +270,6 @@ func newRecordingTransporter(t *testing.T) policy.Transporter {
292270 fmt .Sprintf (`"index_name": "%s"` , fakeCognitiveIndexName ),
293271 fmt .Sprintf (`"index_name":\s*"%s"` , * azureOpenAI .Cognitive .Parameters .IndexName ), nil )
294272 require .NoError (t , err )
295-
296- err = recording .AddGeneralRegexSanitizer (
297- fmt .Sprintf (`"key": "%s"` , fakeAPIKey ),
298- fmt .Sprintf (`"key":\s*"%s"` , * azureOpenAI .Cognitive .Parameters .Authentication .(* azopenai.OnYourDataAPIKeyAuthenticationOptions ).Key ), nil )
299- require .NoError (t , err )
300273 }
301274
302275 t .Cleanup (func () {
@@ -377,11 +350,17 @@ func assertResponseIsError(t *testing.T, err error) {
377350 require .Truef (t , respErr .StatusCode == http .StatusUnauthorized || respErr .StatusCode == http .StatusTooManyRequests , "An acceptable error comes back (actual: %d)" , respErr .StatusCode )
378351}
379352
380- func getEndpoint (ev string ) string {
381- v := getEnvVariable (ev , fakeEndpoint )
353+ func getEndpoint (ev string , azure bool ) string {
354+ fakeEP := fakeAzureEndpoint
355+
356+ if ! azure {
357+ fakeEP = fakeOpenAIEndpoint
358+ }
359+
360+ v := getEnvVariable (ev , fakeEP )
382361
383362 if ! strings .HasSuffix (v , "/" ) {
384- // (this just makes recording replacement easier)
363+ // (this just makes recording replacement easier)j
385364 v += "/"
386365 }
387366
0 commit comments