fix select key

This commit is contained in:
Sakurasan
2025-04-20 18:33:59 +08:00
parent b83c6d9786
commit b80f0759a5
5 changed files with 29 additions and 26 deletions

View File

@@ -28,7 +28,7 @@ func (a Api) CreateApiKey(c *gin.Context) {
}
if slice.Contain([]string{"openai", "azure", "claude"}, *newkey.ApiType) {
sma, err := utils.FetchKeyModel(a.db, newkey)
if err == nil {
if err == nil && len(sma) > 0 {
newkey.SupportModelsArray = sma
var buf = new(bytes.Buffer)
json.NewEncoder(buf).Encode(sma) //nolint:errcheck

View File

@@ -91,11 +91,11 @@ func (dao *ApiKeyDAO) FindApiKeysBySupportModel(db *gorm.DB, modelName string) (
case "postgres":
return nil, errors.New("not support")
}
err := db.Model(&model.ApiKey{}).
Joins("CROSS JOIN JSON_EACH(apikeys.support_models)").
Where("active = true").
Where("value = ?", modelName).
Find(&apiKeys).Error
err := db.Raw(`
SELECT a.*
FROM apikeys a
JOIN json_each(a.support_models) AS je ON je.value = ?
WHERE a.active = true`, modelName).Scan(&apiKeys).Error
return apiKeys, err
}

View File

@@ -8,12 +8,13 @@ import (
"opencatd-open/internal/model"
"os"
"strings"
"time"
"github.com/tidwall/gjson"
"gorm.io/gorm"
)
var client = &http.Client{}
var client = &http.Client{Timeout: 2 * time.Second}
func init() {
if os.Getenv("LOCAL_PROXY") != "" {
@@ -29,13 +30,13 @@ func FetchKeyModel(db *gorm.DB, key *model.ApiKey) ([]string, error) {
var err error
if *key.ApiType == "openai" || *key.ApiType == "azure" {
supportModels, err = FetchOpenAISupportModels(db, key)
if err != nil {
fmt.Println(err)
}
}
if *key.ApiType == "claude" {
supportModels, err = FetchClaudeSupportModels(db, key)
}
if err != nil {
fmt.Println(err)
}
return supportModels, err
}