forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprice_data.go
More file actions
112 lines (98 loc) · 2.99 KB
/
Copy pathprice_data.go
File metadata and controls
112 lines (98 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package types
import (
"fmt"
"math"
"github.com/shopspring/decimal"
)
type GroupRatioInfo struct {
GroupRatio float64
GroupSpecialRatio float64
HasSpecialRatio bool
}
type PriceData struct {
FreeModel bool
ModelPrice float64
ModelRatio float64
CompletionRatio float64
CacheRatio float64
CacheCreationRatio float64
CacheCreation5mRatio float64
CacheCreation1hRatio float64
ImageRatio float64
AudioRatio float64
AudioCompletionRatio float64
otherRatios map[string]float64
UsePrice bool
Quota int // 按次计费的最终额度(MJ / Task)
QuotaToPreConsume int // 按量计费的预消耗额度
GroupRatioInfo GroupRatioInfo
}
func (p *PriceData) AddOtherRatio(key string, ratio float64) {
if !isValidOtherRatio(ratio) {
return
}
if p.otherRatios == nil {
p.otherRatios = make(map[string]float64)
}
p.otherRatios[key] = ratio
}
func (p *PriceData) ReplaceOtherRatios(ratios map[string]float64) bool {
p.otherRatios = nil
for key, ratio := range ratios {
p.AddOtherRatio(key, ratio)
}
return len(p.otherRatios) > 0
}
func (p *PriceData) HasOtherRatio(key string) bool {
ratio, ok := p.otherRatios[key]
return ok && isValidOtherRatio(ratio)
}
func (p *PriceData) OtherRatios() map[string]float64 {
if len(p.otherRatios) == 0 {
return nil
}
ratios := make(map[string]float64, len(p.otherRatios))
for key, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) {
ratios[key] = ratio
}
}
if len(ratios) == 0 {
return nil
}
return ratios
}
func (p *PriceData) OtherRatioMultiplier() float64 {
multiplier := 1.0
for _, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) && ratio != 1.0 {
multiplier *= ratio
}
}
return multiplier
}
func (p *PriceData) ApplyOtherRatiosToFloat(value float64) float64 {
return value * p.OtherRatioMultiplier()
}
func (p *PriceData) ApplyOtherRatiosToDecimal(value decimal.Decimal) decimal.Decimal {
for _, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) && ratio != 1.0 {
value = value.Mul(decimal.NewFromFloat(ratio))
}
}
return value
}
func (p *PriceData) RemoveOtherRatiosFromFloat(value float64) float64 {
for _, ratio := range p.otherRatios {
if isValidOtherRatio(ratio) && ratio != 1.0 {
value /= ratio
}
}
return value
}
func isValidOtherRatio(ratio float64) bool {
return ratio > 0 && !math.IsInf(ratio, 1)
}
func (p *PriceData) ToSetting() string {
return fmt.Sprintf("ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f, CacheCreation5mRatio: %f, CacheCreation1hRatio: %f, QuotaToPreConsume: %d, ImageRatio: %f, AudioRatio: %f, AudioCompletionRatio: %f", p.ModelPrice, p.ModelRatio, p.CompletionRatio, p.CacheRatio, p.GroupRatioInfo.GroupRatio, p.UsePrice, p.CacheCreationRatio, p.CacheCreation5mRatio, p.CacheCreation1hRatio, p.QuotaToPreConsume, p.ImageRatio, p.AudioRatio, p.AudioCompletionRatio)
}