Init customer
This commit is contained in:
parent
d062bc60e4
commit
3d9b81c706
@ -44,6 +44,7 @@ var ModelRatio = map[string]float64{
|
|||||||
"gpt-4-32k-0314": 30,
|
"gpt-4-32k-0314": 30,
|
||||||
"gpt-4-32k-0613": 30,
|
"gpt-4-32k-0613": 30,
|
||||||
"gpt-4-1106-preview": 5, // $0.01 / 1K tokens
|
"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-4-vision-preview": 5, // $0.01 / 1K tokens
|
||||||
"gpt-3.5-turbo": 0.75, // $0.0015 / 1K tokens
|
"gpt-3.5-turbo": 0.75, // $0.0015 / 1K tokens
|
||||||
"gpt-3.5-turbo-0301": 0.75,
|
"gpt-3.5-turbo-0301": 0.75,
|
||||||
@ -122,6 +123,9 @@ func GetModelRatio(name string) float64 {
|
|||||||
name = strings.TrimSuffix(name, "-internet")
|
name = strings.TrimSuffix(name, "-internet")
|
||||||
}
|
}
|
||||||
ratio, ok := ModelRatio[name]
|
ratio, ok := ModelRatio[name]
|
||||||
|
if strings.Index(name, "gpt-4-gizmo") != -1 {
|
||||||
|
return ModelRatio["gpt-4-gizmo"]
|
||||||
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
SysError("model ratio not found: " + name)
|
SysError("model ratio not found: " + name)
|
||||||
return 30
|
return 30
|
||||||
|
@ -35,7 +35,32 @@ func GetAllLogs(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
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) {
|
func GetUserLogs(c *gin.Context) {
|
||||||
p, _ := strconv.Atoi(c.Query("p"))
|
p, _ := strconv.Atoi(c.Query("p"))
|
||||||
if p < 0 {
|
if p < 0 {
|
||||||
|
@ -361,7 +361,7 @@ func Relay(c *gin.Context) {
|
|||||||
if err.StatusCode == http.StatusTooManyRequests {
|
if err.StatusCode == http.StatusTooManyRequests {
|
||||||
err.OpenAIError.Message = "当前分组上游负载已饱和,请稍后再试"
|
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{
|
c.JSON(err.StatusCode, gin.H{
|
||||||
"error": err.OpenAIError,
|
"error": err.OpenAIError,
|
||||||
})
|
})
|
||||||
|
@ -29,7 +29,23 @@ func GetAllTokens(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
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) {
|
func SearchTokens(c *gin.Context) {
|
||||||
userId := c.GetInt("id")
|
userId := c.GetInt("id")
|
||||||
keyword := c.Query("keyword")
|
keyword := c.Query("keyword")
|
||||||
|
@ -65,7 +65,11 @@ func Distribute() func(c *gin.Context) {
|
|||||||
modelRequest.Model = "whisper-1"
|
modelRequest.Model = "whisper-1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if strings.HasPrefix(modelRequest.Model, "gpt-4-gizmo") {
|
||||||
|
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, "gpt-4-gizmo")
|
||||||
|
} else {
|
||||||
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
|
channel, err = model.CacheGetRandomSatisfiedChannel(userGroup, modelRequest.Model)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
|
message := fmt.Sprintf("当前分组 %s 下对于模型 %s 无可用渠道", userGroup, modelRequest.Model)
|
||||||
if channel != nil {
|
if channel != nil {
|
||||||
|
21
model/log.go
21
model/log.go
@ -71,7 +71,26 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
|
|||||||
common.LogError(ctx, "failed to record log: "+err.Error())
|
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) {
|
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
|
var tx *gorm.DB
|
||||||
if logType == LogTypeUnknown {
|
if logType == LogTypeUnknown {
|
||||||
|
@ -3,8 +3,9 @@ package model
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/gorm"
|
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Token struct {
|
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
|
err = DB.Where("user_id = ?", userId).Where("name LIKE ?", keyword+"%").Find(&tokens).Error
|
||||||
return tokens, err
|
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) {
|
func ValidateUserToken(key string) (token *Token, err error) {
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return nil, errors.New("未提供令牌")
|
return nil, errors.New("未提供令牌")
|
||||||
|
@ -82,7 +82,7 @@ func SetApiRouter(router *gin.Engine) {
|
|||||||
tokenRoute.Use(middleware.UserAuth())
|
tokenRoute.Use(middleware.UserAuth())
|
||||||
{
|
{
|
||||||
tokenRoute.GET("/", controller.GetAllTokens)
|
tokenRoute.GET("/", controller.GetAllTokens)
|
||||||
tokenRoute.GET("/search", controller.SearchTokens)
|
tokenRoute.GET("/search", middleware.CORS(), controller.SearchTokens)
|
||||||
tokenRoute.GET("/:id", controller.GetToken)
|
tokenRoute.GET("/:id", controller.GetToken)
|
||||||
tokenRoute.POST("/", controller.AddToken)
|
tokenRoute.POST("/", controller.AddToken)
|
||||||
tokenRoute.PUT("/", controller.UpdateToken)
|
tokenRoute.PUT("/", controller.UpdateToken)
|
||||||
@ -101,6 +101,7 @@ func SetApiRouter(router *gin.Engine) {
|
|||||||
logRoute := apiRouter.Group("/log")
|
logRoute := apiRouter.Group("/log")
|
||||||
logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
|
logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
|
||||||
logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
|
logRoute.DELETE("/", middleware.AdminAuth(), controller.DeleteHistoryLogs)
|
||||||
|
logRoute.GET("/key", middleware.CORS(), controller.GetLogsByKey)
|
||||||
logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
|
logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
|
||||||
logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
|
logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
|
||||||
logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
|
logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-contrib/gzip"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"one-api/controller"
|
"one-api/controller"
|
||||||
"one-api/middleware"
|
"one-api/middleware"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/gzip"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetDashboardRouter(router *gin.Engine) {
|
func SetDashboardRouter(router *gin.Engine) {
|
||||||
|
router.Use(middleware.CORS())
|
||||||
apiRouter := router.Group("/")
|
apiRouter := router.Group("/")
|
||||||
apiRouter.Use(gzip.Gzip(gzip.DefaultCompression))
|
apiRouter.Use(gzip.Gzip(gzip.DefaultCompression))
|
||||||
apiRouter.Use(middleware.GlobalAPIRateLimit())
|
apiRouter.Use(middleware.GlobalAPIRateLimit())
|
||||||
|
@ -40,7 +40,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start",
|
"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",
|
"test": "react-scripts test",
|
||||||
"eject": "react-scripts eject"
|
"eject": "react-scripts eject"
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user