Skip to content

Commit

Permalink
fix: token update issue when status is not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Laisky committed Nov 20, 2024
1 parent 6ab87f8 commit 423c119
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ RUN apk update \
COPY --from=builder2 /build/one-api /
EXPOSE 3000
WORKDIR /data
ENTRYPOINT ["/one-api"]
ENTRYPOINT ["/one-api"]
28 changes: 23 additions & 5 deletions controller/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package controller

import (
"fmt"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
"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/network"
"github.com/songquanpeng/one-api/common/random"
"github.com/songquanpeng/one-api/model"
"net/http"
"strconv"
)

func GetAllTokens(c *gin.Context) {
Expand Down Expand Up @@ -213,22 +214,37 @@ func UpdateToken(c *gin.Context) {
})
return
}
if token.Status == model.TokenStatusEnabled {
if cleanToken.Status == model.TokenStatusExpired && cleanToken.ExpiredTime <= helper.GetTimestamp() && cleanToken.ExpiredTime != -1 {

switch token.Status {
case model.TokenStatusEnabled:
if cleanToken.Status == model.TokenStatusExpired &&
cleanToken.ExpiredTime <= helper.GetTimestamp() && cleanToken.ExpiredTime != -1 &&
token.ExpiredTime != -1 && token.ExpiredTime < helper.GetTimestamp() {

Check warning on line 222 in controller/token.go

View check run for this annotation

Codecov / codecov/patch

controller/token.go#L218-L222

Added lines #L218 - L222 were not covered by tests
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "令牌已过期,无法启用,请先修改令牌过期时间,或者设置为永不过期",
})
return
}
if cleanToken.Status == model.TokenStatusExhausted && cleanToken.RemainQuota <= 0 && !cleanToken.UnlimitedQuota {
if cleanToken.Status == model.TokenStatusExhausted &&
cleanToken.RemainQuota <= 0 && !cleanToken.UnlimitedQuota &&
token.RemainQuota <= 0 && !token.UnlimitedQuota {

Check warning on line 231 in controller/token.go

View check run for this annotation

Codecov / codecov/patch

controller/token.go#L229-L231

Added lines #L229 - L231 were not covered by tests
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "令牌可用额度已用尽,无法启用,请先修改令牌剩余额度,或者设置为无限额度",
})
return
}
case model.TokenStatusExhausted:
if token.RemainQuota > 0 || token.UnlimitedQuota {
token.Status = model.TokenStatusEnabled
}
case model.TokenStatusExpired:
if token.ExpiredTime == -1 || token.ExpiredTime > helper.GetTimestamp() {
token.Status = model.TokenStatusEnabled
}

Check warning on line 245 in controller/token.go

View check run for this annotation

Codecov / codecov/patch

controller/token.go#L238-L245

Added lines #L238 - L245 were not covered by tests
}

if statusOnly != "" {
cleanToken.Status = token.Status
} else {
Expand All @@ -239,6 +255,8 @@ func UpdateToken(c *gin.Context) {
cleanToken.UnlimitedQuota = token.UnlimitedQuota
cleanToken.Models = token.Models
cleanToken.Subnet = token.Subnet
cleanToken.RemainQuota = token.RemainQuota
cleanToken.Status = token.Status

Check warning on line 259 in controller/token.go

View check run for this annotation

Codecov / codecov/patch

controller/token.go#L258-L259

Added lines #L258 - L259 were not covered by tests
}
err = cleanToken.Update()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion monitor/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func ShouldDisableChannel(err *model.Error, statusCode int) bool {
strings.Contains(lowerMessage, "credit") ||
strings.Contains(lowerMessage, "balance") ||
strings.Contains(lowerMessage, "permission denied") ||
strings.Contains(lowerMessage, "organization has been restricted") || // groq
strings.Contains(lowerMessage, "organization has been restricted") || // groq

Check warning on line 37 in monitor/manage.go

View check run for this annotation

Codecov / codecov/patch

monitor/manage.go#L37

Added line #L37 was not covered by tests
strings.Contains(lowerMessage, "已欠费") {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions relay/adaptor/anthropic/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var ModelList = []string{
"claude-instant-1.2", "claude-2.0", "claude-2.1",
"claude-3-haiku-20240307",
"claude-3-5-haiku-20241022",
"claude-3-5-haiku-latest",
"claude-3-sonnet-20240229",
"claude-3-opus-20240229",
"claude-3-5-sonnet-20240620",
"claude-3-5-sonnet-20241022",
"claude-3-5-sonnet-latest",
"claude-3-5-haiku-20241022",
"claude-3-opus-20240229",
}
6 changes: 3 additions & 3 deletions relay/adaptor/ollama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func ConvertRequest(request model.GeneralOpenAIRequest) *ChatRequest {
TopP: request.TopP,
FrequencyPenalty: request.FrequencyPenalty,
PresencePenalty: request.PresencePenalty,
NumPredict: request.MaxTokens,
NumCtx: request.NumCtx,
NumPredict: request.MaxTokens,
NumCtx: request.NumCtx,

Check warning on line 35 in relay/adaptor/ollama/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/ollama/main.go#L34-L35

Added lines #L34 - L35 were not covered by tests
},
Stream: request.Stream,
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func StreamHandler(c *gin.Context, resp *http.Response) (*model.ErrorWithStatusC
for scanner.Scan() {
data := scanner.Text()
if strings.HasPrefix(data, "}") {
data = strings.TrimPrefix(data, "}") + "}"
data = strings.TrimPrefix(data, "}") + "}"

Check warning on line 125 in relay/adaptor/ollama/main.go

View check run for this annotation

Codecov / codecov/patch

relay/adaptor/ollama/main.go#L125

Added line #L125 was not covered by tests
}

var ollamaResponse ChatResponse
Expand Down
2 changes: 1 addition & 1 deletion relay/adaptor/vertexai/gemini/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

var ModelList = []string{
"gemini-1.5-pro-001", "gemini-1.5-flash-001", "gemini-pro", "gemini-pro-vision", "gemini-1.5-pro-002", "gemini-1.5-flash-002",
"gemini-1.5-pro-001", "gemini-1.5-flash-001", "gemini-pro", "gemini-pro-vision", "gemini-1.5-pro-002", "gemini-1.5-flash-002",
}

type Adaptor struct {
Expand Down
3 changes: 3 additions & 0 deletions relay/billing/ratio/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ var ModelRatio = map[string]float64{
"claude-2.1": 8.0 / 1000 * USD,
"claude-3-haiku-20240307": 0.25 / 1000 * USD,
"claude-3-5-haiku-20241022": 1.0 / 1000 * USD,
"claude-3-5-haiku-latest": 1.0 / 1000 * USD,
"claude-3-sonnet-20240229": 3.0 / 1000 * USD,
"claude-3-5-sonnet-20240620": 3.0 / 1000 * USD,
"claude-3-5-sonnet-20241022": 3.0 / 1000 * USD,
"claude-3-5-sonnet-latest": 3.0 / 1000 * USD,
"claude-3-opus-20240229": 15.0 / 1000 * USD,
"claude-3-opus-latest": 15.0 / 1000 * USD,
// https://cloud.baidu.com/doc/WENXINWORKSHOP/s/hlrk4akp7
"ERNIE-4.0-8K": 0.120 * RMB,
"ERNIE-3.5-8K": 0.012 * RMB,
Expand Down

0 comments on commit 423c119

Please sign in to comment.