Skip to content

Commit

Permalink
fix claude valid
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliming committed Dec 28, 2024
1 parent 84243de commit c3fb9c8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
35 changes: 35 additions & 0 deletions controller/relay-claude.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/songquanpeng/one-api/common"
"github.com/songquanpeng/one-api/common/config"
"github.com/songquanpeng/one-api/common/ctxkey"
"github.com/songquanpeng/one-api/common/helper"
"github.com/songquanpeng/one-api/common/logger"
Expand Down Expand Up @@ -113,6 +114,14 @@ func relayTextHelper(c *gin.Context) *relay_model.ErrorWithStatusCode {
modelRatio := billingratio.GetModelRatio(request.Model, meta.ChannelType)
groupRatio := billingratio.GetGroupRatio(meta.Group)
ratio := modelRatio * groupRatio
// pre-consume quota
promptTokens := getPromptTokens(request)
meta.PromptTokens = promptTokens
bizErr := validQuota(ctx, request, promptTokens, ratio, meta)
if bizErr != nil {
logger.Warnf(ctx, "validQuota failed: %+v", *bizErr)
return bizErr
}

adaptor := getAdaptor(meta.APIType)
usage, bizError := adaptor.DoRequest(c, request, meta)
Expand All @@ -125,6 +134,32 @@ func relayTextHelper(c *gin.Context) *relay_model.ErrorWithStatusCode {
return nil
}

func getPromptTokens(request *anthropic.Request) int {
//todo calc input tokens
return 1
}

func validQuota(ctx context.Context, request *anthropic.Request, promptTokens int, ratio float64, meta *meta.Meta) *relay_model.ErrorWithStatusCode {
preConsumedQuota := getPreConsumedQuota(request, promptTokens, ratio)

userQuota, err := model.CacheGetUserQuota(ctx, meta.UserId)
if err != nil {
return openai.ErrorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError)
}
if userQuota-preConsumedQuota < 0 {
return openai.ErrorWrapper(errors.New("user quota is not enough"), "insufficient_user_quota", http.StatusForbidden)
}
return nil
}

func getPreConsumedQuota(request *anthropic.Request, promptTokens int, ratio float64) int64 {
preConsumedTokens := config.PreConsumedQuota + int64(promptTokens)
if request.MaxTokens != 0 {
preConsumedTokens += int64(request.MaxTokens)
}
return int64(float64(preConsumedTokens) * ratio)
}

func getAndValidateRequest(c *gin.Context, mode int) (*anthropic.Request, error) {
request := &anthropic.Request{}
err := common.UnmarshalBodyReusable(c, request)
Expand Down
1 change: 1 addition & 0 deletions model/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (channel *Channel) Insert() error {

func (channel *Channel) Update() error {
var err error
channel.UsedQuota = 0
err = DB.Model(channel).Updates(channel).Error
if err != nil {
return err
Expand Down

0 comments on commit c3fb9c8

Please sign in to comment.