Init customer

This commit is contained in:
‘Liu 2024-01-10 14:19:33 +08:00
parent d062bc60e4
commit 3d9b81c706
11 changed files with 92 additions and 12 deletions

View File

@ -44,6 +44,7 @@ var ModelRatio = map[string]float64{
"gpt-4-32k-0314": 30,
"gpt-4-32k-0613": 30,
"gpt-4-1106-preview": 5, // $0.01 / 1K tokens
"gpt-4-gizmo": 30, // $0.06 / 1K tokens
"gpt-4-vision-preview": 5, // $0.01 / 1K tokens
"gpt-3.5-turbo": 0.75, // $0.0015 / 1K tokens
"gpt-3.5-turbo-0301": 0.75,
@ -122,6 +123,9 @@ func GetModelRatio(name string) float64 {
name = strings.TrimSuffix(name, "-internet")
}
ratio, ok := ModelRatio[name]
if strings.Index(name, "gpt-4-gizmo") != -1 {
return ModelRatio["gpt-4-gizmo"]
}
if !ok {
SysError("model ratio not found: " + name)
return 30

View File

@ -35,7 +35,32 @@ func GetAllLogs(c *gin.Context) {
})
return
}
func GetLogsByKey(c *gin.Context) {
startidx, _ := strconv.Atoi(c.Query("startIdx"))
num, _ := strconv.Atoi(c.Query("num"))
if startidx <= 0 || num <= 0 {
startidx = 0
num = 10
}
logType, _ := strconv.Atoi(c.Query("type"))
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
key := c.Query("key")
logs, err := model.GetLogsByKey(logType, startTimestamp, endTimestamp, key, startidx, num)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": logs,
})
return
}
func GetUserLogs(c *gin.Context) {
p, _ := strconv.Atoi(c.Query("p"))
if p < 0 {

View File

@ -361,7 +361,7 @@ func Relay(c *gin.Context) {
if err.StatusCode == http.StatusTooManyRequests {
err.OpenAIError.Message = "当前分组上游负载已饱和,请稍后再试"
}
err.OpenAIError.Message = common.MessageWithRequestId(err.OpenAIError.Message, requestId)
err.OpenAIError.Message = common.MessageWithRequestId("Request From https://api.adamchatbot.chat Error", requestId)
c.JSON(err.StatusCode, gin.H{
"error": err.OpenAIError,
})

View File

@ -29,7 +29,23 @@ func GetAllTokens(c *gin.Context) {
})
return
}
func GetNameByToken(c *gin.Context) {
token := c.Query("key")
name, err := model.GetNameByToken(token)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": name,
})
return
}
func SearchTokens(c *gin.Context) {
userId := c.GetInt("id")
keyword := c.Query("keyword")

View File

@ -65,7 +65,11 @@ func Distribute() func(c *gin.Context) {
modelRequest.Model = "whisper-1"
}
}
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
if strings.HasPrefix(modelRequest.Model, "gpt-4-gizmo") {
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, "gpt-4-gizmo")
} else {
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
}
if err != nil {
message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
if channel != nil {

View File

@ -71,7 +71,26 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
common.LogError(ctx, "failed to record log: "+err.Error())
}
}
func GetLogsByKey(logType int, startTimestamp int64, endTimestamp int64, key string, startIdx int, num int) (logs []*Log, err error) {
var tx *gorm.DB
token, err := GetNameByToken(key)
if logType == LogTypeUnknown {
tx = DB.Debug()
} else {
tx = DB.Debug().Where("type = ?", logType)
}
if token != nil {
tx = tx.Where("token_name = ?", token.Name)
}
if startTimestamp != 0 {
tx = tx.Where("created_at >= ?", startTimestamp)
}
if endTimestamp != 0 {
tx = tx.Where("created_at <= ?", endTimestamp)
}
err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error
return logs, err
}
func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, startIdx int, num int, channel int) (logs []*Log, err error) {
var tx *gorm.DB
if logType == LogTypeUnknown {

View File

@ -3,8 +3,9 @@ package model
import (
"errors"
"fmt"
"gorm.io/gorm"
"one-api/common"
"gorm.io/gorm"
)
type Token struct {
@ -32,7 +33,15 @@ func SearchUserTokens(userId int, keyword string) (tokens []*Token, err error) {
err = DB.Where("user_id = ?", userId).Where("name LIKE ?", keyword+"%").Find(&tokens).Error
return tokens, err
}
func GetNameByToken(token string) (*Token, error) {
if token == "" {
return nil, errors.New("token为空")
}
token_name := Token{Key: token}
var err error = nil
err = DB.First(&token_name, "`key` = ?", token).Error
return &token_name, err
}
func ValidateUserToken(key string) (token *Token, err error) {
if key == "" {
return nil, errors.New("未提供令牌")

View File

@ -82,7 +82,7 @@ func SetApiRouter(router *gin.Engine) {
tokenRoute.Use(middleware.UserAuth())
{
tokenRoute.GET("/", controller.GetAllTokens)
tokenRoute.GET("/search", controller.SearchTokens)
tokenRoute.GET("/search", middleware.CORS(), controller.SearchTokens)
tokenRoute.GET("/:id", controller.GetToken)
tokenRoute.POST("/", controller.AddToken)
tokenRoute.PUT("/", controller.UpdateToken)
@ -101,6 +101,7 @@ func SetApiRouter(router *gin.Engine) {
logRoute := apiRouter.Group("/log")
logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
logRoute.GET("/key", middleware.CORS(), controller.GetLogsByKey)
logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)

View File

@ -1,13 +1,15 @@
package router
import (
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"one-api/controller"
"one-api/middleware"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)
func SetDashboardRouter(router *gin.Engine) {
router.Use(middleware.CORS())
apiRouter := router.Group("/")
apiRouter.Use(gzip.Gzip(gzip.DefaultCompression))
apiRouter.Use(middleware.GlobalAPIRateLimit())

View File

@ -40,7 +40,7 @@
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build && mv -f build ../build/berry",
"build": "react-scripts build && move build ../build/berry",
"test": "react-scripts test",
"eject": "react-scripts eject"
},

View File

@ -18,7 +18,7 @@
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build && mv -f build ../build/default",
"build": "react-scripts build && mv -f build ../build/default",
"test": "react-scripts test",
"eject": "react-scripts eject"
},