mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-01 16:25:44 +08:00
### What problem does this PR solve? 1. Add license announcement 2. Add sanity check on API config 3. Add base class: BaseModel 4. Add GetBaseURL ### Type of change - [x] Refactoring --------- Signed-off-by: Jin Hai <haijin.chn@gmail.com>
63 lines
1.6 KiB
Go
63 lines
1.6 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
|
|
}
|
|
|
|
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
|
|
}
|