feat: add Azure OpenAI support for chat models

This commit is contained in:
liweixin
2026-04-07 19:12:58 +08:00
committed by lyingbug
parent c9001ef9d9
commit 22ec575727
2 changed files with 40 additions and 7 deletions
+13
View File
@@ -394,6 +394,8 @@ func (s *modelService) GetChatModel(ctx context.Context, modelId string) (chat.C
BaseURL: model.Parameters.BaseURL,
ModelName: model.Name,
Source: model.Source,
Provider: model.Parameters.Provider,
Extra: stringMapToAnyMap(model.Parameters.ExtraConfig),
}, s.ollamaService)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
@@ -500,3 +502,14 @@ func (s *modelService) GetASRModel(ctx context.Context, modelId string) (asr.ASR
return sttModel, nil
}
func stringMapToAnyMap(m map[string]string) map[string]any {
if m == nil {
return nil
}
result := make(map[string]any, len(m))
for k, v := range m {
result[k] = v
}
return result
}
+27 -7
View File
@@ -58,16 +58,28 @@ func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
}
apiKey := chatConfig.APIKey
config := openai.DefaultConfig(apiKey)
if baseURL := chatConfig.BaseURL; baseURL != "" {
config.BaseURL = baseURL
}
providerName := provider.ProviderName(chatConfig.Provider)
if providerName == "" {
providerName = provider.DetectProvider(chatConfig.BaseURL)
}
var config openai.ClientConfig
if providerName == provider.ProviderAzureOpenAI {
config = openai.DefaultAzureConfig(apiKey, chatConfig.BaseURL)
if chatConfig.Extra != nil {
if v, ok := chatConfig.Extra["api_version"]; ok {
if vs, ok := v.(string); ok && vs != "" {
config.APIVersion = vs
}
}
}
} else {
config = openai.DefaultConfig(apiKey)
if baseURL := chatConfig.BaseURL; baseURL != "" {
config.BaseURL = baseURL
}
}
return &RemoteAPIChat{
modelName: chatConfig.ModelName,
client: openai.NewClientWithConfig(config),
@@ -315,7 +327,11 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
if c.provider == provider.ProviderAzureOpenAI {
httpReq.Header.Set("api-key", c.apiKey)
} else {
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
}
resp, err := rawHTTPClient.Do(httpReq)
if err != nil {
@@ -467,7 +483,11 @@ func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, endpoint stri
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
if c.provider == provider.ProviderAzureOpenAI {
httpReq.Header.Set("api-key", c.apiKey)
} else {
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
}
httpReq.Header.Set("Accept", "text/event-stream")
resp, err := rawHTTPClient.Do(httpReq)