|
@@ -16,6 +16,7 @@ import (
|
|
|
func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
channelType := c.GetInt("channel")
|
|
channelType := c.GetInt("channel")
|
|
|
tokenId := c.GetInt("token_id")
|
|
tokenId := c.GetInt("token_id")
|
|
|
|
|
+ userId := c.GetInt("id")
|
|
|
consumeQuota := c.GetBool("consume_quota")
|
|
consumeQuota := c.GetBool("consume_quota")
|
|
|
group := c.GetString("group")
|
|
group := c.GetString("group")
|
|
|
var textRequest GeneralOpenAIRequest
|
|
var textRequest GeneralOpenAIRequest
|
|
@@ -73,7 +74,16 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
groupRatio := common.GetGroupRatio(group)
|
|
groupRatio := common.GetGroupRatio(group)
|
|
|
ratio := modelRatio * groupRatio
|
|
ratio := modelRatio * groupRatio
|
|
|
preConsumedQuota := int(float64(preConsumedTokens) * ratio)
|
|
preConsumedQuota := int(float64(preConsumedTokens) * ratio)
|
|
|
- if consumeQuota {
|
|
|
|
|
|
|
+ userQuota, err := model.CacheGetUserQuota(userId)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return errorWrapper(err, "get_user_quota_failed", http.StatusOK)
|
|
|
|
|
+ }
|
|
|
|
|
+ if userQuota > 10*preConsumedQuota {
|
|
|
|
|
+ // in this case, we do not pre-consume quota
|
|
|
|
|
+ // because the user has enough quota
|
|
|
|
|
+ preConsumedQuota = 0
|
|
|
|
|
+ }
|
|
|
|
|
+ if consumeQuota && preConsumedQuota > 0 {
|
|
|
err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota)
|
|
err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusOK)
|
|
return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusOK)
|
|
@@ -133,7 +143,6 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|
|
common.SysError("Error consuming token remain quota: " + err.Error())
|
|
common.SysError("Error consuming token remain quota: " + err.Error())
|
|
|
}
|
|
}
|
|
|
tokenName := c.GetString("token_name")
|
|
tokenName := c.GetString("token_name")
|
|
|
- userId := c.GetInt("id")
|
|
|
|
|
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("通过令牌「%s」使用模型 %s 消耗 %s(模型倍率 %.2f,分组倍率 %.2f)", tokenName, textRequest.Model, common.LogQuota(quota), modelRatio, groupRatio))
|
|
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("通过令牌「%s」使用模型 %s 消耗 %s(模型倍率 %.2f,分组倍率 %.2f)", tokenName, textRequest.Model, common.LogQuota(quota), modelRatio, groupRatio))
|
|
|
model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
|
|
model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
|
|
|
channelId := c.GetInt("channel_id")
|
|
channelId := c.GetInt("channel_id")
|