Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: add Azure OpenAI support for chat models

liweixin22ec5757274c
diff --git a/internal/application/service/model.go b/internal/application/service/model.go
index f6c33db0c..750ff301d 100644
--- a/internal/application/service/model.go
+++ b/internal/application/service/model.go
@@ -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
+}
diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go
index f8775932c..357eb66ae 100644
--- a/internal/models/chat/remote_api.go
+++ b/internal/models/chat/remote_api.go
@@ -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)