Files
ragflow/internal/entity/models/base_model.go
Haruko386 9c30557ef7 Go: add dimensions for list models and fix some embed-bug in providers (#15940)
### What problem does this PR solve?

As title

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [x] Refactoring
2026-06-11 19:18:49 +08:00

110 lines
3.0 KiB
Go

//
// 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
AllowEmptyAPIKey bool
}
func (b *BaseModel) APIConfigCheck(apiConfig *APIConfig) error {
if b.AllowEmptyAPIKey {
return nil
}
if apiConfig == nil || apiConfig.ApiKey == nil || strings.TrimSpace(*apiConfig.ApiKey) == "" {
return fmt.Errorf("api key is required")
}
return nil
}
// BearerAuth returns the Bearer token for Authorization header,
// or empty string if apiConfig or its ApiKey is nil/empty.
func BearerAuth(apiConfig *APIConfig) string {
if apiConfig == nil || apiConfig.ApiKey == nil {
return ""
}
key := strings.TrimSpace(*apiConfig.ApiKey)
if key == "" {
return ""
}
return fmt.Sprintf("Bearer %s", key)
}
func (b *BaseModel) GetBaseURL(apiConfig *APIConfig) (string, error) {
if apiConfig != nil && apiConfig.BaseURL != nil && *apiConfig.BaseURL != "" {
return strings.TrimSuffix(*apiConfig.BaseURL, "/"), nil
}
region := "default"
hasRegion := false
if apiConfig != nil && apiConfig.Region != nil {
hasRegion = true
region = *apiConfig.Region
}
baseURL, ok := b.BaseURL[region]
if !ok || baseURL == "" {
if (!hasRegion || region == "") && b.BaseURL != nil {
if defaultBaseURL, ok := b.BaseURL["default"]; ok && defaultBaseURL != "" {
return defaultBaseURL, nil
}
}
return "", fmt.Errorf("no base URL configured for region %q", region)
}
return baseURL, nil
}
// ParseListModel Parse model list
func ParseListModel(modelList ModelList) []ListModelResponse {
var models []ListModelResponse
pm := GetProviderManager()
for _, model := range modelList.Models {
modelName := model.ID
var modelResponse ListModelResponse
var modelEntity *Model
if pm != nil {
modelEntity = pm.GetModelByNameOrAlias(modelName)
}
if model.OwnedBy != "" {
modelName = model.ID + "@" + model.OwnedBy
}
modelResponse.Name = modelName
if modelEntity != nil {
modelResponse.MaxDimension = modelEntity.MaxDimension
modelResponse.Dimensions = modelEntity.Dimensions
modelResponse.MaxTokens = modelEntity.MaxTokens
modelResponse.ModelTypes = modelEntity.ModelTypes
modelResponse.Thinking = modelEntity.Thinking
modelResponse.Dimensions = modelEntity.Dimensions
}
models = append(models, modelResponse)
}
return models
}