@@ -23,12 +23,9 @@ func Wait() {
23
23
func AsyncConsume (
24
24
postGroupConsumer balance.PostGroupConsumer ,
25
25
code int ,
26
- usage * relaymodel.Usage ,
27
26
meta * meta.Meta ,
28
- inputPrice ,
29
- outputPrice float64 ,
30
- cachedPrice float64 ,
31
- cacheCreationPrice float64 ,
27
+ usage relaymodel.Usage ,
28
+ modelPrice model.Price ,
32
29
content string ,
33
30
ip string ,
34
31
retryTimes int ,
@@ -47,12 +44,9 @@ func AsyncConsume(
47
44
context .Background (),
48
45
postGroupConsumer ,
49
46
code ,
50
- usage ,
51
47
meta ,
52
- inputPrice ,
53
- outputPrice ,
54
- cachedPrice ,
55
- cacheCreationPrice ,
48
+ usage ,
49
+ modelPrice ,
56
50
content ,
57
51
ip ,
58
52
retryTimes ,
@@ -65,29 +59,23 @@ func Consume(
65
59
ctx context.Context ,
66
60
postGroupConsumer balance.PostGroupConsumer ,
67
61
code int ,
68
- usage * relaymodel.Usage ,
69
62
meta * meta.Meta ,
70
- inputPrice ,
71
- outputPrice float64 ,
72
- cachedPrice float64 ,
73
- cacheCreationPrice float64 ,
63
+ usage relaymodel.Usage ,
64
+ modelPrice model.Price ,
74
65
content string ,
75
66
ip string ,
76
67
retryTimes int ,
77
68
requestDetail * model.RequestDetail ,
78
69
downstreamResult bool ,
79
70
) {
80
- amount := CalculateAmount (usage , inputPrice , outputPrice , cachedPrice , cacheCreationPrice )
71
+ amount := CalculateAmount (usage , modelPrice )
81
72
82
73
amount = consumeAmount (ctx , amount , postGroupConsumer , meta )
83
74
84
75
err := recordConsume (meta ,
85
76
code ,
86
77
usage ,
87
- inputPrice ,
88
- outputPrice ,
89
- cachedPrice ,
90
- cacheCreationPrice ,
78
+ modelPrice ,
91
79
content ,
92
80
ip ,
93
81
requestDetail ,
@@ -114,13 +102,9 @@ func consumeAmount(
114
102
}
115
103
116
104
func CalculateAmount (
117
- usage * relaymodel.Usage ,
118
- inputPrice , outputPrice , cachedPrice , cacheCreationPrice float64 ,
105
+ usage relaymodel.Usage ,
106
+ modelPrice model. Price ,
119
107
) float64 {
120
- if usage == nil {
121
- return 0
122
- }
123
-
124
108
promptTokens := usage .PromptTokens
125
109
completionTokens := usage .CompletionTokens
126
110
var cachedTokens int
@@ -130,24 +114,24 @@ func CalculateAmount(
130
114
cacheCreationTokens = usage .PromptTokensDetails .CacheCreationTokens
131
115
}
132
116
133
- if cachedPrice > 0 {
117
+ if modelPrice . CachedPrice > 0 {
134
118
promptTokens -= cachedTokens
135
119
}
136
- if cacheCreationPrice > 0 {
120
+ if modelPrice . CacheCreationPrice > 0 {
137
121
promptTokens -= cacheCreationTokens
138
122
}
139
123
140
124
promptAmount := decimal .NewFromInt (int64 (promptTokens )).
141
- Mul (decimal .NewFromFloat (inputPrice )).
125
+ Mul (decimal .NewFromFloat (modelPrice . InputPrice )).
142
126
Div (decimal .NewFromInt (model .PriceUnit ))
143
127
completionAmount := decimal .NewFromInt (int64 (completionTokens )).
144
- Mul (decimal .NewFromFloat (outputPrice )).
128
+ Mul (decimal .NewFromFloat (modelPrice . OutputPrice )).
145
129
Div (decimal .NewFromInt (model .PriceUnit ))
146
130
cachedAmount := decimal .NewFromInt (int64 (cachedTokens )).
147
- Mul (decimal .NewFromFloat (cachedPrice )).
131
+ Mul (decimal .NewFromFloat (modelPrice . CachedPrice )).
148
132
Div (decimal .NewFromInt (model .PriceUnit ))
149
133
cacheCreationAmount := decimal .NewFromInt (int64 (cacheCreationTokens )).
150
- Mul (decimal .NewFromFloat (cacheCreationPrice )).
134
+ Mul (decimal .NewFromFloat (modelPrice . CacheCreationPrice )).
151
135
Div (decimal .NewFromInt (model .PriceUnit ))
152
136
153
137
return promptAmount .
@@ -182,64 +166,3 @@ func processGroupConsume(
182
166
}
183
167
return consumedAmount
184
168
}
185
-
186
- func recordConsume (
187
- meta * meta.Meta ,
188
- code int ,
189
- usage * relaymodel.Usage ,
190
- inputPrice ,
191
- outputPrice float64 ,
192
- cachedPrice float64 ,
193
- cacheCreationPrice float64 ,
194
- content string ,
195
- ip string ,
196
- requestDetail * model.RequestDetail ,
197
- amount float64 ,
198
- retryTimes int ,
199
- downstreamResult bool ,
200
- ) error {
201
- promptTokens := 0
202
- completionTokens := 0
203
- cachedTokens := 0
204
- cacheCreationTokens := 0
205
- if usage != nil {
206
- promptTokens = usage .PromptTokens
207
- completionTokens = usage .CompletionTokens
208
- if usage .PromptTokensDetails != nil {
209
- cachedTokens = usage .PromptTokensDetails .CachedTokens
210
- cacheCreationTokens = usage .PromptTokensDetails .CacheCreationTokens
211
- }
212
- }
213
-
214
- var channelID int
215
- if meta .Channel != nil {
216
- channelID = meta .Channel .ID
217
- }
218
-
219
- return model .BatchRecordConsume (
220
- meta .RequestID ,
221
- meta .RequestAt ,
222
- meta .Group .ID ,
223
- code ,
224
- channelID ,
225
- promptTokens ,
226
- completionTokens ,
227
- cachedTokens ,
228
- cacheCreationTokens ,
229
- meta .OriginModel ,
230
- meta .Token .ID ,
231
- meta .Token .Name ,
232
- amount ,
233
- inputPrice ,
234
- outputPrice ,
235
- cachedPrice ,
236
- cacheCreationPrice ,
237
- meta .Endpoint ,
238
- content ,
239
- int (meta .Mode ),
240
- ip ,
241
- retryTimes ,
242
- requestDetail ,
243
- downstreamResult ,
244
- )
245
- }
0 commit comments