Skip to content

Commit 296ab01

Browse files
authored
feat: support gpt-4o mini (songquanpeng#1665)
* feat: support gpt-4o mini * feat: fix gpt-4o mini image price
1 parent 5f03c85 commit 296ab01

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

relay/adaptor/openai/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var ModelList = []string{
88
"gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-0613",
99
"gpt-4-turbo-preview", "gpt-4-turbo", "gpt-4-turbo-2024-04-09",
1010
"gpt-4o", "gpt-4o-2024-05-13",
11+
"gpt-4o-mini", "gpt-4o-mini-2024-07-18",
1112
"gpt-4-vision-preview",
1213
"text-embedding-ada-002", "text-embedding-3-small", "text-embedding-3-large",
1314
"text-curie-001", "text-babbage-001", "text-ada-001", "text-davinci-002", "text-davinci-003",

relay/adaptor/openai/token.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func CountTokenMessages(messages []model.Message, model string) int {
110110
if imageUrl["detail"] != nil {
111111
detail = imageUrl["detail"].(string)
112112
}
113-
imageTokens, err := countImageTokens(url, detail)
113+
imageTokens, err := countImageTokens(url, detail, model)
114114
if err != nil {
115115
logger.SysError("error counting image tokens: " + err.Error())
116116
} else {
@@ -134,11 +134,15 @@ const (
134134
lowDetailCost = 85
135135
highDetailCostPerTile = 170
136136
additionalCost = 85
137+
// gpt-4o-mini cost higher than other model
138+
gpt4oMiniLowDetailCost = 2833
139+
gpt4oMiniHighDetailCost = 5667
140+
gpt4oMiniAdditionalCost = 2833
137141
)
138142

139143
// https://platform.openai.com/docs/guides/vision/calculating-costs
140144
// https://github.com/openai/openai-cookbook/blob/05e3f9be4c7a2ae7ecf029a7c32065b024730ebe/examples/How_to_count_tokens_with_tiktoken.ipynb
141-
func countImageTokens(url string, detail string) (_ int, err error) {
145+
func countImageTokens(url string, detail string, model string) (_ int, err error) {
142146
var fetchSize = true
143147
var width, height int
144148
// Reference: https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding
@@ -172,6 +176,9 @@ func countImageTokens(url string, detail string) (_ int, err error) {
172176
}
173177
switch detail {
174178
case "low":
179+
if strings.HasPrefix(model, "gpt-4o-mini") {
180+
return gpt4oMiniLowDetailCost, nil
181+
}
175182
return lowDetailCost, nil
176183
case "high":
177184
if fetchSize {
@@ -191,6 +198,9 @@ func countImageTokens(url string, detail string) (_ int, err error) {
191198
height = int(float64(height) * ratio)
192199
}
193200
numSquares := int(math.Ceil(float64(width)/512) * math.Ceil(float64(height)/512))
201+
if strings.HasPrefix(model, "gpt-4o-mini") {
202+
return numSquares*gpt4oMiniHighDetailCost + gpt4oMiniAdditionalCost, nil
203+
}
194204
result := numSquares*highDetailCostPerTile + additionalCost
195205
return result, nil
196206
default:

relay/billing/ratio/model.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ var ModelRatio = map[string]float64{
2828
"gpt-4-32k": 30,
2929
"gpt-4-32k-0314": 30,
3030
"gpt-4-32k-0613": 30,
31-
"gpt-4-1106-preview": 5, // $0.01 / 1K tokens
32-
"gpt-4-0125-preview": 5, // $0.01 / 1K tokens
33-
"gpt-4-turbo-preview": 5, // $0.01 / 1K tokens
34-
"gpt-4-turbo": 5, // $0.01 / 1K tokens
35-
"gpt-4-turbo-2024-04-09": 5, // $0.01 / 1K tokens
36-
"gpt-4o": 2.5, // $0.005 / 1K tokens
37-
"gpt-4o-2024-05-13": 2.5, // $0.005 / 1K tokens
38-
"gpt-4-vision-preview": 5, // $0.01 / 1K tokens
39-
"gpt-3.5-turbo": 0.25, // $0.0005 / 1K tokens
31+
"gpt-4-1106-preview": 5, // $0.01 / 1K tokens
32+
"gpt-4-0125-preview": 5, // $0.01 / 1K tokens
33+
"gpt-4-turbo-preview": 5, // $0.01 / 1K tokens
34+
"gpt-4-turbo": 5, // $0.01 / 1K tokens
35+
"gpt-4-turbo-2024-04-09": 5, // $0.01 / 1K tokens
36+
"gpt-4o": 2.5, // $0.005 / 1K tokens
37+
"gpt-4o-2024-05-13": 2.5, // $0.005 / 1K tokens
38+
"gpt-4o-mini": 0.075, // $0.00015 / 1K tokens
39+
"gpt-4o-mini-2024-07-18": 0.075, // $0.00015 / 1K tokens
40+
"gpt-4-vision-preview": 5, // $0.01 / 1K tokens
41+
"gpt-3.5-turbo": 0.25, // $0.0005 / 1K tokens
4042
"gpt-3.5-turbo-0301": 0.75,
4143
"gpt-3.5-turbo-0613": 0.75,
4244
"gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
@@ -308,6 +310,9 @@ func GetCompletionRatio(name string, channelType int) float64 {
308310
return 4.0 / 3.0
309311
}
310312
if strings.HasPrefix(name, "gpt-4") {
313+
if strings.HasPrefix(name, "gpt-4o-mini") {
314+
return 4
315+
}
311316
if strings.HasPrefix(name, "gpt-4-turbo") ||
312317
strings.HasPrefix(name, "gpt-4o") ||
313318
strings.HasSuffix(name, "preview") {

0 commit comments

Comments
 (0)