@@ -5,15 +5,11 @@ import (
55 "fmt"
66 "github.com/gin-gonic/gin"
77 "github.com/gorilla/websocket"
8- "math"
98 "net/http"
109 "one-api/common"
1110 "one-api/dto"
12- "one-api/model"
1311 relaycommon "one-api/relay/common"
1412 "one-api/service"
15- "strings"
16- "time"
1713)
1814
1915//func getAndValidateWssRequest(c *gin.Context, ws *websocket.Conn) (*dto.RealtimeEvent, error) {
@@ -91,6 +87,7 @@ func WssHelper(c *gin.Context, ws *websocket.Conn) *dto.OpenAIErrorWithStatusCod
9187 preConsumedQuota = int (float64 (preConsumedTokens ) * ratio )
9288 } else {
9389 preConsumedQuota = int (modelPrice * common .QuotaPerUnit * groupRatio )
90+ relayInfo .UsePrice = true
9491 }
9592
9693 // pre-consume quota 预消耗配额
@@ -126,95 +123,10 @@ func WssHelper(c *gin.Context, ws *websocket.Conn) *dto.OpenAIErrorWithStatusCod
126123 service .ResetStatusCode (openaiErr , statusCodeMappingStr )
127124 return openaiErr
128125 }
129- postWssConsumeQuota (c , relayInfo , relayInfo .UpstreamModelName , usage .(* dto.RealtimeUsage ), ratio , preConsumedQuota , userQuota , modelRatio , groupRatio , modelPrice , getModelPriceSuccess , "" )
126+ service . PostWssConsumeQuota (c , relayInfo , relayInfo .UpstreamModelName , usage .(* dto.RealtimeUsage ), ratio , preConsumedQuota , userQuota , modelRatio , groupRatio , modelPrice , getModelPriceSuccess , "" )
130127 return nil
131128}
132129
133- func postWssConsumeQuota (ctx * gin.Context , relayInfo * relaycommon.RelayInfo , modelName string ,
134- usage * dto.RealtimeUsage , ratio float64 , preConsumedQuota int , userQuota int , modelRatio float64 ,
135- groupRatio float64 ,
136- modelPrice float64 , usePrice bool , extraContent string ) {
137-
138- useTimeSeconds := time .Now ().Unix () - relayInfo .StartTime .Unix ()
139- textInputTokens := usage .InputTokenDetails .TextTokens
140- textOutTokens := usage .OutputTokenDetails .TextTokens
141-
142- audioInputTokens := usage .InputTokenDetails .AudioTokens
143- audioOutTokens := usage .OutputTokenDetails .AudioTokens
144-
145- tokenName := ctx .GetString ("token_name" )
146- completionRatio := common .GetCompletionRatio (modelName )
147- audioRatio := common .GetAudioRatio (relayInfo .UpstreamModelName )
148- audioCompletionRatio := common .GetAudioCompletionRatio (modelName )
149-
150- quota := 0
151- if ! usePrice {
152- quota = textInputTokens + int (math .Round (float64 (textOutTokens )* completionRatio ))
153- quota += int (math .Round (float64 (audioInputTokens )* audioRatio )) + int (math .Round (float64 (audioOutTokens )* audioRatio * audioCompletionRatio ))
154-
155- quota = int (math .Round (float64 (quota ) * ratio ))
156- if ratio != 0 && quota <= 0 {
157- quota = 1
158- }
159- } else {
160- quota = int (modelPrice * common .QuotaPerUnit * groupRatio )
161- }
162- totalTokens := usage .TotalTokens
163- var logContent string
164- if ! usePrice {
165- logContent = fmt .Sprintf ("模型倍率 %.2f,补全倍率 %.2f,音频倍率 %.2f,音频补全倍率 %.2f,分组倍率 %.2f" , modelRatio , completionRatio , audioRatio , audioCompletionRatio , groupRatio )
166- } else {
167- logContent = fmt .Sprintf ("模型价格 %.2f,分组倍率 %.2f" , modelPrice , groupRatio )
168- }
169-
170- // record all the consume log even if quota is 0
171- if totalTokens == 0 {
172- // in this case, must be some error happened
173- // we cannot just return, because we may have to return the pre-consumed quota
174- quota = 0
175- logContent += fmt .Sprintf ("(可能是上游超时)" )
176- common .LogError (ctx , fmt .Sprintf ("total tokens is 0, cannot consume quota, userId %d, channelId %d, " +
177- "tokenId %d, model %s, pre-consumed quota %d" , relayInfo .UserId , relayInfo .ChannelId , relayInfo .TokenId , modelName , preConsumedQuota ))
178- } else {
179- //if sensitiveResp != nil {
180- // logContent += fmt.Sprintf(",敏感词:%s", strings.Join(sensitiveResp.SensitiveWords, ", "))
181- //}
182- quotaDelta := quota - preConsumedQuota
183- if quotaDelta != 0 {
184- err := model .PostConsumeTokenQuota (relayInfo , userQuota , quotaDelta , preConsumedQuota , true )
185- if err != nil {
186- common .LogError (ctx , "error consuming token remain quota: " + err .Error ())
187- }
188- }
189- err := model .CacheUpdateUserQuota (relayInfo .UserId )
190- if err != nil {
191- common .LogError (ctx , "error update user quota cache: " + err .Error ())
192- }
193- model .UpdateUserUsedQuotaAndRequestCount (relayInfo .UserId , quota )
194- model .UpdateChannelUsedQuota (relayInfo .ChannelId , quota )
195- }
196-
197- logModel := modelName
198- if strings .HasPrefix (logModel , "gpt-4-gizmo" ) {
199- logModel = "gpt-4-gizmo-*"
200- logContent += fmt .Sprintf (",模型 %s" , modelName )
201- }
202- if strings .HasPrefix (logModel , "gpt-4o-gizmo" ) {
203- logModel = "gpt-4o-gizmo-*"
204- logContent += fmt .Sprintf (",模型 %s" , modelName )
205- }
206- if extraContent != "" {
207- logContent += ", " + extraContent
208- }
209- other := service .GenerateWssOtherInfo (ctx , relayInfo , usage , modelRatio , groupRatio , completionRatio , modelPrice )
210- model .RecordConsumeLog (ctx , relayInfo .UserId , relayInfo .ChannelId , usage .InputTokens , usage .OutputTokens , logModel ,
211- tokenName , quota , logContent , relayInfo .TokenId , userQuota , int (useTimeSeconds ), relayInfo .IsStream , other )
212-
213- //if quota != 0 {
214- //
215- //}
216- }
217-
218130//func getWssPromptTokens(textRequest *dto.RealtimeEvent, info *relaycommon.RelayInfo) (int, error) {
219131// var promptTokens int
220132// var err error
0 commit comments