-
Notifications
You must be signed in to change notification settings - Fork 58
/
cost.go
150 lines (121 loc) · 3.4 KB
/
cost.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package azure
import (
"errors"
"fmt"
goopenai "github.com/sashabaranov/go-openai"
)
var AzureOpenAiPerThousandTokenCost = map[string]map[string]float64{
"prompt": {
"gpt-4": 0.03,
"gpt-4-32k": 0.06,
"gpt-4-vision": 0.06,
"gpt-35-turbo": 0.0015,
"gpt-35-turbo-instruct": 0.0015,
"gpt-35-turbo-16k": 0.003,
},
"embeddings": {
"ada": 0.0001,
},
"completion": {
"gpt-4": 0.06,
"gpt-4-32k": 0.12,
"gpt-4-vision": 0.12,
"gpt-35-turbo": 0.002,
"gpt-35-turbo-instruct": 0.002,
"gpt-35-turbo-16k": 0.004,
},
}
type CostEstimator struct {
tokenCostMap map[string]map[string]float64
}
func NewCostEstimator() *CostEstimator {
return &CostEstimator{
tokenCostMap: AzureOpenAiPerThousandTokenCost,
}
}
func (ce *CostEstimator) EstimateTotalCost(model string, promptTks, completionTks int) (float64, error) {
promptCost, err := ce.EstimatePromptCost(model, promptTks)
if err != nil {
return 0, err
}
completionCost, err := ce.EstimateCompletionCost(model, completionTks)
if err != nil {
return 0, err
}
return promptCost + completionCost, nil
}
func (ce *CostEstimator) EstimatePromptCost(model string, tks int) (float64, error) {
costMap, ok := ce.tokenCostMap["prompt"]
if !ok {
return 0, errors.New("prompt token cost is not provided")
}
cost, ok := costMap[model]
if !ok {
return 0, fmt.Errorf("%s is not present in the cost map provided", model)
}
tksInFloat := float64(tks)
return tksInFloat / 1000 * cost, nil
}
func (ce *CostEstimator) EstimateEmbeddingsInputCost(model string, tks int) (float64, error) {
costMap, ok := ce.tokenCostMap["embeddings"]
if !ok {
return 0, errors.New("embeddings token cost is not provided")
}
cost, ok := costMap[model]
if !ok {
return 0, fmt.Errorf("%s is not present in the cost map provided", model)
}
tksInFloat := float64(tks)
return tksInFloat / 1000 * cost, nil
}
func (ce *CostEstimator) EstimateCompletionCost(model string, tks int) (float64, error) {
costMap, ok := ce.tokenCostMap["completion"]
if !ok {
return 0, errors.New("prompt token cost is not provided")
}
cost, ok := costMap[model]
if !ok {
return 0, errors.New("model is not present in the cost map provided")
}
tksInFloat := float64(tks)
return tksInFloat / 1000 * cost, nil
}
func (ce *CostEstimator) EstimateChatCompletionStreamCostWithTokenCounts(model string, content string) (int, float64, error) {
if len(model) == 0 {
return 0, 0, errors.New("model is not provided")
}
tks, err := Count(model, content)
if err != nil {
return 0, 0, err
}
cost, err := ce.EstimateCompletionCost(model, tks)
if err != nil {
return 0, 0, err
}
return tks, cost, nil
}
func (ce *CostEstimator) EstimateEmbeddingsCost(r *goopenai.EmbeddingRequest) (float64, error) {
model := "ada"
if inputs, ok := r.Input.([]interface{}); ok {
textStr := ""
for _, input := range inputs {
converted, ok := input.(string)
if !ok {
return 0, errors.New("input is not string")
}
textStr += converted
}
tks, err := Count(model, textStr)
if err != nil {
return 0, err
}
return ce.EstimateEmbeddingsInputCost(model, tks)
} else if input, ok := r.Input.(string); ok {
tks, err := Count(model, input)
if err != nil {
return 0, err
}
return ce.EstimateEmbeddingsInputCost(model, tks)
}
return 0, errors.New("input format is not recognized")
}