Skip to content

Commit 104c0c0

Browse files
authored
Azure openai list models (#290)
* feat(models): include flow for azure openai endpoint * feat(models): include flow for azure openai endpoint * feat(models): include flow for azure openai endpoint * chore(fullURL): update logic to run in fullURL function * chore(fullURL): update based on pr comments to use c.config.APIVersion
1 parent 1b8feae commit 104c0c0

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ func (c *Client) fullURL(suffix string) string {
103103
if c.config.APIType == APITypeAzure || c.config.APIType == APITypeAzureAD {
104104
baseURL := c.config.BaseURL
105105
baseURL = strings.TrimRight(baseURL, "/")
106+
// if suffix is /models change to {endpoint}/openai/models?api-version=2022-12-01
107+
// https://learn.microsoft.com/en-us/rest/api/cognitiveservices/azureopenaistable/models/list?tabs=HTTP
108+
if strings.Contains(suffix, "/models") {
109+
return fmt.Sprintf("%s/%s%s?api-version=%s", baseURL, azureAPIPrefix, suffix, c.config.APIVersion)
110+
}
106111
return fmt.Sprintf("%s/%s/%s/%s%s?api-version=%s",
107112
baseURL, azureAPIPrefix, azureDeploymentsPrefix, c.config.Engine, suffix, c.config.APIVersion)
108113
}

internal/test/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (ts *ServerTest) OpenAITestServer() *httptest.Server {
3131
log.Printf("received request at path %q\n", r.URL.Path)
3232

3333
// check auth
34-
if r.Header.Get("Authorization") != "Bearer "+GetTestToken() {
34+
if r.Header.Get("Authorization") != "Bearer "+GetTestToken() && r.Header.Get("api-key") != GetTestToken() {
3535
w.WriteHeader(http.StatusUnauthorized)
3636
return
3737
}

models_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ func TestListModels(t *testing.T) {
3131
checks.NoError(t, err, "ListModels error")
3232
}
3333

34+
func TestAzureListModels(t *testing.T) {
35+
server := test.NewTestServer()
36+
server.RegisterHandler("/openai/models", handleModelsEndpoint)
37+
// create the test server
38+
var err error
39+
ts := server.OpenAITestServer()
40+
ts.Start()
41+
defer ts.Close()
42+
43+
config := DefaultAzureConfig(test.GetTestToken(), "https://dummylab.openai.azure.com/", "dummyengine")
44+
config.BaseURL = ts.URL
45+
client := NewClientWithConfig(config)
46+
ctx := context.Background()
47+
48+
_, err = client.ListModels(ctx)
49+
checks.NoError(t, err, "ListModels error")
50+
}
51+
3452
// handleModelsEndpoint Handles the models endpoint by the test server.
3553
func handleModelsEndpoint(w http.ResponseWriter, _ *http.Request) {
3654
resBytes, _ := json.Marshal(ModelsList{})

0 commit comments

Comments
 (0)