Skip to content

Commit 87a140b

Browse files
committed
fix: update AI provider configuration to include Azure OpenAI and adjust request handling
1 parent 7952075 commit 87a140b

5 files changed

Lines changed: 50 additions & 6 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"}]`},
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"}]`},
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"}]`},
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"}]`},
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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ type GetAIModelsResp struct {
3838
}
3939

4040
type GetAIModelsReq struct {
41-
APIHost string `json:"api_host"`
42-
APIKey string `json:"api_key"`
41+
Provider string `json:"provider"`
42+
APIHost string `json:"api_host"`
43+
APIKey string `json:"api_key"`
4344
}
4445

4546
// GetAIModelResp get AI model response
@@ -49,3 +50,18 @@ type GetAIModelResp struct {
4950
Created int `json:"created"`
5051
OwnedBy string `json:"owned_by"`
5152
}
53+
54+
// GetAzureDeploymentsResp get Azure OpenAI deployments response
55+
type GetAzureDeploymentsResp struct {
56+
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"`
64+
} `json:"data"`
65+
}
66+
67+

internal/service/siteinfo/siteinfo_service.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,20 @@ func (s *SiteInfoService) GetAIModels(ctx context.Context, req *schema.GetAIMode
724724
}
725725

726726
r := resty.New()
727-
r.SetHeader("Authorization", fmt.Sprintf("Bearer %s", req.APIKey))
728727
r.SetHeader("Content-Type", "application/json")
729-
respBody, err := r.R().Get(req.APIHost + "/v1/models")
728+
729+
var respBody *resty.Response
730+
apiHost := strings.TrimRight(req.APIHost, "/")
731+
if req.Provider == "azure_openai" {
732+
// Azure OpenAI uses api-key header and lists deployments
733+
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)
736+
} else {
737+
// Standard OpenAI-compatible providers
738+
r.SetHeader("Authorization", fmt.Sprintf("Bearer %s", req.APIKey))
739+
respBody, err = r.R().Get(apiHost + "/v1/models")
740+
}
730741
if err != nil {
731742
log.Error(err)
732743
return resp, errors.BadRequest(fmt.Sprintf("failed to get AI models %s", err.Error()))
@@ -736,6 +747,20 @@ func (s *SiteInfoService) GetAIModels(ctx context.Context, req *schema.GetAIMode
736747
return resp, errors.BadRequest(fmt.Sprintf("failed to get AI models, response: %s", respBody.String()))
737748
}
738749

750+
if req.Provider == "azure_openai" {
751+
data := schema.GetAzureDeploymentsResp{}
752+
_ = json.Unmarshal(respBody.Body(), &data)
753+
for _, d := range data.Data {
754+
resp = append(resp, &schema.GetAIModelResp{
755+
Id: d.Id,
756+
Object: d.Object,
757+
Created: d.CreatedAt,
758+
OwnedBy: d.Model,
759+
})
760+
}
761+
return resp, nil
762+
}
763+
739764
data := schema.GetAIModelsResp{}
740765
_ = json.Unmarshal(respBody.Body(), &data)
741766

ui/src/pages/Admin/AiSettings/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const Index = () => {
8484

8585
const checkAiConfigData = (data) => {
8686
const params = data || {
87+
provider: formData.provider.value,
8788
api_host: formData.api_host.value || apiHostPlaceholder,
8889
api_key: formData.api_key.value,
8990
};
@@ -151,6 +152,7 @@ const Index = () => {
151152
const host = findHistoryProvider?.api_host || provider?.default_api_host;
152153
if (findHistoryProvider?.model) {
153154
checkAiConfigData({
155+
provider: value,
154156
api_host: host,
155157
api_key: findHistoryProvider.api_key,
156158
});
@@ -263,6 +265,7 @@ const Index = () => {
263265
);
264266
const host = currentAiConfig.api_host || provider?.default_api_host;
265267
checkAiConfigData({
268+
provider: currentAiConfig.provider,
266269
api_host: host,
267270
api_key: currentAiConfig.api_key,
268271
});

0 commit comments

Comments
 (0)