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
This commit is contained in:
Juan
2023-05-03 19:02:35 +10:00
committed by GitHub
parent 1b8feae0b6
commit 104c0c0b63
3 changed files with 24 additions and 1 deletions

View File

@@ -103,6 +103,11 @@ func (c *Client) fullURL(suffix string) string {
if c.config.APIType == APITypeAzure || c.config.APIType == APITypeAzureAD {
baseURL := c.config.BaseURL
baseURL = strings.TrimRight(baseURL, "/")
// if suffix is /models change to {endpoint}/openai/models?api-version=2022-12-01
// https://learn.microsoft.com/en-us/rest/api/cognitiveservices/azureopenaistable/models/list?tabs=HTTP
if strings.Contains(suffix, "/models") {
return fmt.Sprintf("%s/%s%s?api-version=%s", baseURL, azureAPIPrefix, suffix, c.config.APIVersion)
}
return fmt.Sprintf("%s/%s/%s/%s%s?api-version=%s",
baseURL, azureAPIPrefix, azureDeploymentsPrefix, c.config.Engine, suffix, c.config.APIVersion)
}

View File

@@ -31,7 +31,7 @@ func (ts *ServerTest) OpenAITestServer() *httptest.Server {
log.Printf("received request at path %q\n", r.URL.Path)
// check auth
if r.Header.Get("Authorization") != "Bearer "+GetTestToken() {
if r.Header.Get("Authorization") != "Bearer "+GetTestToken() && r.Header.Get("api-key") != GetTestToken() {
w.WriteHeader(http.StatusUnauthorized)
return
}

View File

@@ -31,6 +31,24 @@ func TestListModels(t *testing.T) {
checks.NoError(t, err, "ListModels error")
}
func TestAzureListModels(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/openai/models", handleModelsEndpoint)
// create the test server
var err error
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()
config := DefaultAzureConfig(test.GetTestToken(), "https://dummylab.openai.azure.com/", "dummyengine")
config.BaseURL = ts.URL
client := NewClientWithConfig(config)
ctx := context.Background()
_, err = client.ListModels(ctx)
checks.NoError(t, err, "ListModels error")
}
// handleModelsEndpoint Handles the models endpoint by the test server.
func handleModelsEndpoint(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(ModelsList{})