Skip to content

Commit

Permalink
Improve error logging in the fetchDeployedModels function to get more…
Browse files Browse the repository at this point in the history
… detailed information.
  • Loading branch information
Gyarbij authored Oct 8, 2024
1 parent 238ad29 commit 374d594
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,39 +159,42 @@ func handleGetModels(c *gin.Context) {
}

func fetchDeployedModels(originalReq *http.Request) ([]Model, error) {
endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
if endpoint == "" {
endpoint = azure.AzureOpenAIEndpoint
}

url := fmt.Sprintf("%s/openai/models?api-version=%s", endpoint, azure.AzureOpenAIAPIVersion)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

req.Header.Set("Authorization", originalReq.Header.Get("Authorization"))

azure.HandleToken(req)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to fetch deployed models: %s", string(body))
}

var deployedModelsResponse ModelList
if err := json.NewDecoder(resp.Body).Decode(&deployedModelsResponse); err != nil {
return nil, err
}

return deployedModelsResponse.Data, nil
endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
if endpoint == "" {
endpoint = azure.AzureOpenAIEndpoint
}

url := fmt.Sprintf("%s/openai/models?api-version=%s", endpoint, azure.AzureOpenAIAPIVersion)
log.Printf("Requesting deployed models from URL: %s", url)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}

req.Header.Set("Authorization", originalReq.Header.Get("Authorization"))

azure.HandleToken(req)

client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()

body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch deployed models: Status: %d, Body: %s, URL: %s", resp.StatusCode, string(body), url)
}

var deployedModelsResponse ModelList
if err := json.Unmarshal(body, &deployedModelsResponse); err != nil {
return nil, fmt.Errorf("error unmarshaling response: %v, Body: %s", err, string(body))
}

log.Printf("Successfully fetched %d deployed models", len(deployedModelsResponse.Data))
return deployedModelsResponse.Data, nil
}

func handleOptions(c *gin.Context) {
Expand Down

0 comments on commit 374d594

Please sign in to comment.