Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Go: refactor model provider (#15568)

Jin Haid736f358ba8a
diff --git a/internal/entity/models/302ai.go b/internal/entity/models/302ai.go
index 2195b76721..da1973a2fc 100644
--- a/internal/entity/models/302ai.go
+++ b/internal/entity/models/302ai.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/baichuan.go b/internal/entity/models/baichuan.go
index 1e2e4ba52f..115fc2f66e 100644
--- a/internal/entity/models/baichuan.go
+++ b/internal/entity/models/baichuan.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/baidu.go b/internal/entity/models/baidu.go
index 7f1224dc60..ca1eb9b6f8 100644
--- a/internal/entity/models/baidu.go
+++ b/internal/entity/models/baidu.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/base_model.go b/internal/entity/models/base_model.go
new file mode 100644
index 0000000000..59e8b198d3
--- /dev/null
+++ b/internal/entity/models/base_model.go
@@ -0,0 +1,62 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package models
+
+import (
+ "fmt"
+ "net/http"
+ "strings"
+)
+
+type BaseModel struct {
+ BaseURL map[string]string
+ URLSuffix URLSuffix
+ httpClient *http.Client
+}
+
+func (b *BaseModel) APIConfigCheck(apiConfig *APIConfig) error {
+ if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
+ return fmt.Errorf("api key is nil or empty")
+ }
+
+ if apiConfig.BaseURL == nil || *apiConfig.BaseURL == "" {
+ if apiConfig.Region == nil || *apiConfig.Region == "" {
+ return fmt.Errorf("no base url and region")
+ }
+ }
+
+ return nil
+}
+
+func (b *BaseModel) GetBaseURL(apiConfig *APIConfig) (string, error) {
+
+ if apiConfig.BaseURL != nil && *apiConfig.BaseURL != "" {
+ return strings.TrimSuffix(*apiConfig.BaseURL, "/"), nil
+ }
+
+ region := "default"
+ if apiConfig.Region != nil {
+ region = *apiConfig.Region
+ }
+
+ baseURL, ok := b.BaseURL[region]
+ if !ok || baseURL == "" {
+ return "", fmt.Errorf("no base URL configured for region %q", region)
+ }
+
+ return baseURL, nil
+}
diff --git a/internal/entity/models/cohere.go b/internal/entity/models/cohere.go
index 2b99eeb10d..b3285709ea 100644
--- a/internal/entity/models/cohere.go
+++ b/internal/entity/models/cohere.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/deepinfra.go b/internal/entity/models/deepinfra.go
index de3397dbcf..9ff3f51b61 100644
--- a/internal/entity/models/deepinfra.go
+++ b/internal/entity/models/deepinfra.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/fishaudio.go b/internal/entity/models/fishaudio.go
index ad91274f74..cee2fd2e10 100644
--- a/internal/entity/models/fishaudio.go
+++ b/internal/entity/models/fishaudio.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/huaweicloud.go b/internal/entity/models/huaweicloud.go
index 1e977ca7ba..ec3e4da07c 100644
--- a/internal/entity/models/huaweicloud.go
+++ b/internal/entity/models/huaweicloud.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/huggingface.go b/internal/entity/models/huggingface.go
index e92ac3cecb..87d80e76bd 100644
--- a/internal/entity/models/huggingface.go
+++ b/internal/entity/models/huggingface.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/jiekouai.go b/internal/entity/models/jiekouai.go
index 69c798fe31..3aedbe2abf 100644
--- a/internal/entity/models/jiekouai.go
+++ b/internal/entity/models/jiekouai.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/jina.go b/internal/entity/models/jina.go
index 519a40f1fd..1b117c83d2 100644
--- a/internal/entity/models/jina.go
+++ b/internal/entity/models/jina.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/lmstudio.go b/internal/entity/models/lmstudio.go
index e490623cb5..892b02b191 100644
--- a/internal/entity/models/lmstudio.go
+++ b/internal/entity/models/lmstudio.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/mineru.go b/internal/entity/models/mineru.go
index 2a395b2600..b657c961d3 100644
--- a/internal/entity/models/mineru.go
+++ b/internal/entity/models/mineru.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/mineru_local.go b/internal/entity/models/mineru_local.go
index 7fc5d6c53a..02a16ea93e 100644
--- a/internal/entity/models/mineru_local.go
+++ b/internal/entity/models/mineru_local.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/nvidia.go b/internal/entity/models/nvidia.go
index cdfd4acb6f..864b9f89d3 100644
--- a/internal/entity/models/nvidia.go
+++ b/internal/entity/models/nvidia.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/ollama.go b/internal/entity/models/ollama.go
index 98280c11de..d1dd752e56 100644
--- a/internal/entity/models/ollama.go
+++ b/internal/entity/models/ollama.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/openrouter.go b/internal/entity/models/openrouter.go
index 3239ae4b0a..4361f48a65 100644
--- a/internal/entity/models/openrouter.go
+++ b/internal/entity/models/openrouter.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/orcarouter.go b/internal/entity/models/orcarouter.go
index 09a315f650..62056f0742 100644
--- a/internal/entity/models/orcarouter.go
+++ b/internal/entity/models/orcarouter.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/paddleocr.go b/internal/entity/models/paddleocr.go
index f9705638f7..edd6d12c2b 100644
--- a/internal/entity/models/paddleocr.go
+++ b/internal/entity/models/paddleocr.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/paddleocr_local.go b/internal/entity/models/paddleocr_local.go
index c95c0b4778..308849caf4 100644
--- a/internal/entity/models/paddleocr_local.go
+++ b/internal/entity/models/paddleocr_local.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/qiniu.go b/internal/entity/models/qiniu.go
index c7ee144a06..0ddf4d471f 100644
--- a/internal/entity/models/qiniu.go
+++ b/internal/entity/models/qiniu.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/tokenhub.go b/internal/entity/models/tokenhub.go
index 71cd1a763a..05d2b11f63 100644
--- a/internal/entity/models/tokenhub.go
+++ b/internal/entity/models/tokenhub.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/xunfei.go b/internal/entity/models/xunfei.go
index dfd3234d71..3cde72eb29 100644
--- a/internal/entity/models/xunfei.go
+++ b/internal/entity/models/xunfei.go
@@ -1,3 +1,19 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
package models
import (
diff --git a/internal/entity/models/zhipu-ai.go b/internal/entity/models/zhipu-ai.go
index f525ccf3a9..b900212017 100644
--- a/internal/entity/models/zhipu-ai.go
+++ b/internal/entity/models/zhipu-ai.go
@@ -35,22 +35,22 @@ import (
// ZhipuAIModel implements ModelDriver for Zhipu AI
type ZhipuAIModel struct {
- BaseURL map[string]string
- URLSuffix URLSuffix
- httpClient *http.Client // Reusable HTTP client with connection pool
+ baseModel BaseModel
}
// NewZhipuAIModel creates a new Zhipu AI model instance
func NewZhipuAIModel(baseURL map[string]string, urlSuffix URLSuffix) *ZhipuAIModel {
return &ZhipuAIModel{
- BaseURL: baseURL,
- URLSuffix: urlSuffix,
- httpClient: &http.Client{
- Transport: &http.Transport{
- MaxIdleConns: 100,
- MaxIdleConnsPerHost: 10,
- IdleConnTimeout: 90 * time.Second,
- DisableCompression: false,
+ baseModel: BaseModel{
+ BaseURL: baseURL,
+ URLSuffix: urlSuffix,
+ httpClient: &http.Client{
+ Transport: &http.Transport{
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 10,
+ IdleConnTimeout: 90 * time.Second,
+ DisableCompression: false,
+ },
},
},
}
@@ -66,19 +66,21 @@ func (z *ZhipuAIModel) Name() string {
// ChatWithMessages sends multiple messages with roles and returns response
func (z *ZhipuAIModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
- if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
- return nil, fmt.Errorf("api key is nil or empty")
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return nil, err
}
if len(messages) == 0 {
return nil, fmt.Errorf("messages is empty")
}
- region := "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
}
- url := fmt.Sprintf("%s/%s", z.BaseURL[region], z.URLSuffix.Chat)
+
+ url := fmt.Sprintf("%s/%s", baseURL, z.baseModel.URLSuffix.Chat)
// Convert messages to the format expected by API
apiMessages := make([]map[string]interface{}, len(messages))
@@ -147,7 +149,7 @@ func (z *ZhipuAIModel) ChatWithMessages(modelName string, messages []Message, ap
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
@@ -210,16 +212,21 @@ func (z *ZhipuAIModel) ChatWithMessages(modelName string, messages []Message, ap
// ChatStreamlyWithSender sends messages and streams response via sender function (best performance, no channel)
func (z *ZhipuAIModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, sender func(*string, *string) error) error {
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return err
+ }
+
if len(messages) == 0 {
return fmt.Errorf("messages is empty")
}
- var region = "default"
- if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return err
}
- url := fmt.Sprintf("%s/%s", strings.TrimSuffix(z.BaseURL[region], "/"), z.URLSuffix.Chat)
+ url := fmt.Sprintf("%s/%s", baseURL, z.baseModel.URLSuffix.Chat)
// Convert messages to API format
apiMessages := make([]map[string]interface{}, len(messages))
@@ -292,7 +299,7 @@ func (z *ZhipuAIModel) ChatStreamlyWithSender(modelName string, messages []Messa
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
@@ -398,20 +405,21 @@ func (z *ZhipuAIModel) Embed(modelName *string, texts []string, apiConfig *APICo
return []EmbeddingData{}, nil
}
- if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
- return nil, fmt.Errorf("api key is required")
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return nil, err
}
if modelName == nil || *modelName == "" {
return nil, fmt.Errorf("model name is required")
}
- var region = "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
}
- url := fmt.Sprintf("%s/%s", strings.TrimSuffix(z.BaseURL[region], "/"), z.URLSuffix.Embedding)
+ url := fmt.Sprintf("%s/%s", baseURL, z.baseModel.URLSuffix.Embedding)
reqBody := map[string]interface{}{}
reqBody["model"] = modelName
@@ -436,7 +444,7 @@ func (z *ZhipuAIModel) Embed(modelName *string, texts []string, apiConfig *APICo
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
@@ -470,22 +478,17 @@ func (z *ZhipuAIModel) Embed(modelName *string, texts []string, apiConfig *APICo
}
func (z *ZhipuAIModel) ListModels(apiConfig *APIConfig) ([]string, error) {
- region := "default"
- if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return nil, err
}
- baseURL := z.BaseURL["default"]
- if region != "default" {
- if regional, ok := z.BaseURL[region]; ok && regional != "" {
- baseURL = regional
- }
- }
- if baseURL == "" {
- return nil, fmt.Errorf("zhipu-ai: no base URL configured for default region")
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
}
- url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), z.URLSuffix.Models)
+ url := fmt.Sprintf("%s/%s", baseURL, z.baseModel.URLSuffix.Models)
ctx, cancel := context.WithTimeout(context.Background(), nonStreamCallTimeout)
defer cancel()
@@ -500,7 +503,7 @@ func (z *ZhipuAIModel) ListModels(apiConfig *APIConfig) ([]string, error) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
}
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
@@ -523,9 +526,6 @@ func (z *ZhipuAIModel) ListModels(apiConfig *APIConfig) ([]string, error) {
models := make([]string, 0, len(modelList.Models))
for _, model := range modelList.Models {
modelName := model.ID
- if model.OwnedBy != "" {
- modelName = model.ID + "@" + model.OwnedBy
- }
models = append(models, modelName)
}
@@ -537,12 +537,17 @@ func (z *ZhipuAIModel) Balance(apiConfig *APIConfig) (map[string]interface{}, er
}
func (z *ZhipuAIModel) CheckConnection(apiConfig *APIConfig) error {
- var region = "default"
- if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return err
}
- url := fmt.Sprintf("%s/%s", z.BaseURL[region], z.URLSuffix.Files)
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return err
+ }
+
+ url := fmt.Sprintf("%s/%s", baseURL, z.baseModel.URLSuffix.Files)
ctx, cancel := context.WithTimeout(context.Background(), nonStreamCallTimeout)
defer cancel()
@@ -555,7 +560,7 @@ func (z *ZhipuAIModel) CheckConnection(apiConfig *APIConfig) error {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
@@ -613,25 +618,21 @@ func (z *ZhipuAIModel) Rerank(modelName *string, query string, documents []strin
return &RerankResponse{}, nil
}
- if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
- return nil, fmt.Errorf("api key is required")
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return nil, err
}
if modelName == nil || *modelName == "" {
return nil, fmt.Errorf("model name is required")
}
- region := "default"
- if apiConfig.Region != nil {
- region = *apiConfig.Region
- }
-
- baseURL, ok := z.BaseURL[region]
- if !ok || baseURL == "" {
- return nil, fmt.Errorf("zhipu-ai: no base URL configured for region %q", region)
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
}
- url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), z.URLSuffix.Rerank)
+ url := fmt.Sprintf("%s/%s", baseURL, z.baseModel.URLSuffix.Rerank)
var topN = rerankConfig.TopN
if rerankConfig.TopN == 0 {
@@ -662,7 +663,7 @@ func (z *ZhipuAIModel) Rerank(modelName *string, query string, documents []strin
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
@@ -696,28 +697,27 @@ func (z *ZhipuAIModel) Rerank(modelName *string, query string, documents []strin
// TranscribeAudio transcribe audio
func (z *ZhipuAIModel) TranscribeAudio(modelName *string, file *string, apiConfig *APIConfig, asrConfig *ASRConfig) (*ASRResponse, error) {
- if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
- return nil, fmt.Errorf("api key is required")
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return nil, err
}
+
if modelName == nil || *modelName == "" {
return nil, fmt.Errorf("model name is required")
}
if file == nil || *file == "" {
return nil, fmt.Errorf("file is required")
}
- if z.URLSuffix.ASR == "" {
+ if z.baseModel.URLSuffix.ASR == "" {
return nil, fmt.Errorf("zhipu-ai: ASR URL suffix is not configured")
}
- region := "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
- }
- baseURL, ok := z.BaseURL[region]
- if !ok || baseURL == "" {
- return nil, fmt.Errorf("zhipu-ai: no base URL configured for region %q", region)
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
}
- url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), strings.TrimLeft(z.URLSuffix.ASR, "/"))
+
+ url := fmt.Sprintf("%s/%s", baseURL, strings.TrimLeft(z.baseModel.URLSuffix.ASR, "/"))
var body bytes.Buffer
writer := multipart.NewWriter(&body)
@@ -758,7 +758,7 @@ func (z *ZhipuAIModel) TranscribeAudio(modelName *string, file *string, apiConfi
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
req.Header.Set("Content-Type", writer.FormDataContentType())
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
@@ -829,6 +829,11 @@ func (z *ZhipuAIModel) TranscribeAudioWithSender(modelName *string, file *string
// AudioSpeech convert text to audio
func (z *ZhipuAIModel) AudioSpeech(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig) (*TTSResponse, error) {
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return nil, err
+ }
+
reqBody, url, err := z.buildTTSRequest(modelName, audioContent, apiConfig, ttsConfig, false)
if err != nil {
return nil, err
@@ -849,7 +854,7 @@ func (z *ZhipuAIModel) AudioSpeech(modelName *string, audioContent *string, apiC
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
@@ -867,6 +872,11 @@ func (z *ZhipuAIModel) AudioSpeech(modelName *string, audioContent *string, apiC
}
func (z *ZhipuAIModel) AudioSpeechWithSender(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, sender func(*string, *string) error) error {
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return err
+ }
+
if sender == nil {
return fmt.Errorf("sender is required")
}
@@ -890,7 +900,7 @@ func (z *ZhipuAIModel) AudioSpeechWithSender(modelName *string, audioContent *st
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
@@ -921,26 +931,20 @@ func (z *ZhipuAIModel) AudioSpeechWithSender(modelName *string, audioContent *st
}
func (z *ZhipuAIModel) buildTTSRequest(modelName *string, audioContent *string, apiConfig *APIConfig, ttsConfig *TTSConfig, stream bool) (map[string]interface{}, string, error) {
- if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
- return nil, "", fmt.Errorf("api key is required")
- }
+
if modelName == nil || *modelName == "" {
return nil, "", fmt.Errorf("model name is required")
}
if audioContent == nil || *audioContent == "" {
return nil, "", fmt.Errorf("audio content is empty")
}
- if z.URLSuffix.TTS == "" {
+ if z.baseModel.URLSuffix.TTS == "" {
return nil, "", fmt.Errorf("zhipu-ai: TTS URL suffix is not configured")
}
- region := "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
- }
- baseURL, ok := z.BaseURL[region]
- if !ok || baseURL == "" {
- return nil, "", fmt.Errorf("zhipu-ai: no base URL configured for region %q", region)
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, "", err
}
reqBody := map[string]interface{}{
@@ -961,14 +965,15 @@ func (z *ZhipuAIModel) buildTTSRequest(modelName *string, audioContent *string,
}
}
- url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), strings.TrimLeft(z.URLSuffix.TTS, "/"))
+ url := fmt.Sprintf("%s/%s", baseURL, strings.TrimLeft(z.baseModel.URLSuffix.TTS, "/"))
return reqBody, url, nil
}
// OCRFile OCR file
func (z *ZhipuAIModel) OCRFile(modelName *string, content []byte, fileURL *string, apiConfig *APIConfig, ocrConfig *OCRConfig) (*OCRFileResponse, error) {
- if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
- return nil, fmt.Errorf("api key is required")
+ err := z.baseModel.APIConfigCheck(apiConfig)
+ if err != nil {
+ return nil, err
}
if modelName == nil || *modelName == "" {
@@ -979,17 +984,12 @@ func (z *ZhipuAIModel) OCRFile(modelName *string, content []byte, fileURL *strin
return nil, fmt.Errorf("file url or content is required")
}
- region := "default"
- if apiConfig.Region != nil && *apiConfig.Region != "" {
- region = *apiConfig.Region
- }
-
- baseURL, ok := z.BaseURL[region]
- if !ok || baseURL == "" {
- return nil, fmt.Errorf("zhipu-ai: no base URL configured for region %q", region)
+ baseURL, err := z.baseModel.GetBaseURL(apiConfig)
+ if err != nil {
+ return nil, err
}
- if z.URLSuffix.OCR == "" {
+ if z.baseModel.URLSuffix.OCR == "" {
return nil, fmt.Errorf("zhipu-ai: no OCR URL suffix configured")
}
@@ -1014,11 +1014,11 @@ func (z *ZhipuAIModel) OCRFile(modelName *string, content []byte, fileURL *strin
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- endpoint := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), strings.TrimPrefix(z.URLSuffix.OCR, "/"))
+ url := fmt.Sprintf("%s/%s", baseURL, strings.TrimPrefix(z.baseModel.URLSuffix.OCR, "/"))
ctx, cancel := context.WithTimeout(context.Background(), longOpCallTimeout)
defer cancel()
- req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -1026,7 +1026,7 @@ func (z *ZhipuAIModel) OCRFile(modelName *string, content []byte, fileURL *strin
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
- resp, err := z.httpClient.Do(req)
+ resp, err := z.baseModel.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}