fetch model & add apitype

This commit is contained in:
Sakurasan
2025-04-19 01:21:28 +08:00
parent f8e539c9b4
commit ca305f4199
5 changed files with 128 additions and 5 deletions

View File

@@ -1,10 +1,13 @@
package controller
import (
"bytes"
"encoding/json"
"net/http"
"opencatd-open/internal/consts"
"opencatd-open/internal/dto"
"opencatd-open/internal/model"
"opencatd-open/internal/utils"
"strconv"
"strings"
@@ -18,13 +21,22 @@ func (a Api) CreateApiKey(c *gin.Context) {
dto.Fail(c, 403, "Permission denied")
return
}
req := new(model.ApiKey)
err := c.ShouldBind(&req)
newkey := new(model.ApiKey)
err := c.ShouldBind(newkey)
if err != nil {
dto.Fail(c, 400, err.Error())
}
if slice.Contain([]string{"openai", "azure", "claude"}, *newkey.ApiType) {
sma, err := utils.FetchKeyModel(a.db, newkey)
if err == nil {
newkey.SupportModelsArray = sma
var buf = new(bytes.Buffer)
json.NewEncoder(buf).Encode(sma) //nolint:errcheck
newkey.SupportModels = utils.ToPtr(buf.String())
}
}
err = a.keyService.CreateApiKey(c, req)
err = a.keyService.CreateApiKey(c, newkey)
if err != nil {
dto.Fail(c, 400, err.Error())
} else {
@@ -79,6 +91,10 @@ func (a Api) ListApiKey(c *gin.Context) {
}
str = str[:slen]
key.ApiKey = &str
var sma []string
json.NewDecoder(strings.NewReader(*key.SupportModels)).Decode(&sma) //nolint:errcheck
key.SupportModelsArray = sma
}
dto.Success(c, gin.H{
"total": total,

View File

@@ -186,7 +186,7 @@ func (p *Proxy) SelectApiKey(model string) error {
akpikeys, err := p.apiKeyDao.FindApiKeysBySupportModel(p.db, model)
if err != nil || len(akpikeys) == 0 {
if strings.HasPrefix(model, "gpt") || strings.HasPrefix(model, "o1") || strings.HasPrefix(model, "o3") {
if strings.HasPrefix(model, "gpt") || strings.HasPrefix(model, "o1") || strings.HasPrefix(model, "o3") || strings.HasPrefix(model, "o4") {
keys, err := p.apiKeyDao.FindKeys(map[string]any{"apitype = ?": "openai"})
if err != nil {
return err
@@ -227,7 +227,7 @@ func (p *Proxy) SelectApiKey(model string) error {
func (p *Proxy) updateSupportModel() {
keys, err := p.apiKeyDao.FindKeys(map[string]interface{}{"apitype in ?": "openai,azure,claude"})
keys, err := p.apiKeyDao.FindKeys(map[string]interface{}{"apitype in ?": []string{"openai", "azure", "claude"}})
if err != nil {
return
}