Skip to content

Commit d91cddc

Browse files
committed
fix: update Azure AI Foundry configuration and deployment handling in AI models service
1 parent 87a140b commit d91cddc

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

internal/migrations/init_data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ var (
353353
{ID: 128, Key: "rank.answer.undeleted", Value: `-1`},
354354
{ID: 129, Key: "rank.question.undeleted", Value: `-1`},
355355
{ID: 130, Key: "rank.tag.undeleted", Value: `-1`},
356-
{ID: 131, Key: "ai_config.provider", Value: `[{"default_api_host":"https://api.openai.com","display_name":"OpenAI","name":"openai"},{"default_api_host":"https://generativelanguage.googleapis.com","display_name":"Gemini","name":"gemini"},{"default_api_host":"https://api.anthropic.com","display_name":"Anthropic","name":"anthropic"},{"default_api_host":"https://{your-resource}.openai.azure.com","display_name":"Azure OpenAI","name":"azure_openai"}]`},
356+
{ID: 131, Key: "ai_config.provider", Value: `[{"default_api_host":"https://api.openai.com","display_name":"OpenAI","name":"openai"},{"default_api_host":"https://generativelanguage.googleapis.com","display_name":"Gemini","name":"gemini"},{"default_api_host":"https://api.anthropic.com","display_name":"Anthropic","name":"anthropic"},{"default_api_host":"https://{your-project}.services.ai.azure.com","display_name":"Azure AI Foundry","name":"azure_openai"}]`},
357357
}
358358

359359
defaultBadgeGroupTable = []*entity.BadgeGroup{

internal/migrations/v31.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func addAPIKey(ctx context.Context, x *xorm.Engine) error {
6161
}
6262

6363
defaultConfigTable := []*entity.Config{
64-
{ID: 131, Key: "ai_config.provider", Value: `[{"default_api_host":"https://api.openai.com","display_name":"OpenAI","name":"openai"},{"default_api_host":"https://generativelanguage.googleapis.com","display_name":"Gemini","name":"gemini"},{"default_api_host":"https://api.anthropic.com","display_name":"Anthropic","name":"anthropic"},{"default_api_host":"https://{your-resource}.openai.azure.com","display_name":"Azure OpenAI","name":"azure_openai"}]`},
64+
{ID: 131, Key: "ai_config.provider", Value: `[{"default_api_host":"https://api.openai.com","display_name":"OpenAI","name":"openai"},{"default_api_host":"https://generativelanguage.googleapis.com","display_name":"Gemini","name":"gemini"},{"default_api_host":"https://api.anthropic.com","display_name":"Anthropic","name":"anthropic"},{"default_api_host":"https://{your-project}.services.ai.azure.com","display_name":"Azure AI Foundry","name":"azure_openai"}]`},
6565
}
6666
for _, c := range defaultConfigTable {
6767
exist, err := x.Context(ctx).Get(&entity.Config{Key: c.Key})

internal/schema/ai_config_schema.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,15 @@ type GetAIModelResp struct {
5151
OwnedBy string `json:"owned_by"`
5252
}
5353

54-
// GetAzureDeploymentsResp get Azure OpenAI deployments response
54+
// GetAzureDeploymentsResp Azure OpenAI deployments response
5555
type GetAzureDeploymentsResp struct {
5656
Data []struct {
57-
Id string `json:"id"`
58-
Model string `json:"model"`
59-
Owner string `json:"owner"`
60-
Object string `json:"object"`
61-
Status string `json:"status"`
62-
CreatedAt int `json:"created_at"`
63-
UpdatedAt int `json:"updated_at"`
57+
Id string `json:"id"`
58+
Model string `json:"model"`
59+
Owner string `json:"owner"`
60+
Object string `json:"object"`
61+
Status string `json:"status"`
62+
CreatedAt int `json:"created_at"`
63+
UpdatedAt int `json:"updated_at"`
6464
} `json:"data"`
6565
}
66-
67-

internal/service/siteinfo/siteinfo_service.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"encoding/json"
2525
errpkg "errors"
2626
"fmt"
27+
"net/url"
2728
"strings"
2829

2930
"github.com/apache/answer/internal/base/constant"
@@ -728,11 +729,16 @@ func (s *SiteInfoService) GetAIModels(ctx context.Context, req *schema.GetAIMode
728729

729730
var respBody *resty.Response
730731
apiHost := strings.TrimRight(req.APIHost, "/")
731-
if req.Provider == "azure_openai" {
732-
// Azure OpenAI uses api-key header and lists deployments
732+
if req.Provider == "azure_ai" {
733+
// Azure AI: parse resource name from apiHost and list deployments via *.openai.azure.com
733734
r.SetHeader("api-key", req.APIKey)
734-
url := fmt.Sprintf("%s/openai/deployments?api-version=2022-12-01", apiHost)
735-
respBody, err = r.R().Get(url)
735+
parsedURL, parseErr := url.Parse(apiHost)
736+
if parseErr != nil || parsedURL.Host == "" {
737+
return resp, errors.BadRequest("invalid api_host URL")
738+
}
739+
resourceName := strings.Split(parsedURL.Hostname(), ".")[0]
740+
deploymentsURL := fmt.Sprintf("https://%s.openai.azure.com/openai/deployments?api-version=2022-12-01", resourceName)
741+
respBody, err = r.R().Get(deploymentsURL)
736742
} else {
737743
// Standard OpenAI-compatible providers
738744
r.SetHeader("Authorization", fmt.Sprintf("Bearer %s", req.APIKey))
@@ -747,7 +753,7 @@ func (s *SiteInfoService) GetAIModels(ctx context.Context, req *schema.GetAIMode
747753
return resp, errors.BadRequest(fmt.Sprintf("failed to get AI models, response: %s", respBody.String()))
748754
}
749755

750-
if req.Provider == "azure_openai" {
756+
if req.Provider == "azure_ai" {
751757
data := schema.GetAzureDeploymentsResp{}
752758
_ = json.Unmarshal(respBody.Body(), &data)
753759
for _, d := range data.Data {

0 commit comments

Comments
 (0)