Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
09401526
authored
Jul 19, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support gpt-4o-mini image tokens
parent
6ff4370d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
2 deletions
+9
-2
service/token_counter.go
+9
-2
No files found.
service/token_counter.go
View file @
09401526
...
@@ -82,11 +82,12 @@ func getTokenNum(tokenEncoder *tiktoken.Tiktoken, text string) int {
...
@@ -82,11 +82,12 @@ func getTokenNum(tokenEncoder *tiktoken.Tiktoken, text string) int {
}
}
func
getImageToken
(
imageUrl
*
dto
.
MessageImageUrl
,
model
string
,
stream
bool
)
(
int
,
error
)
{
func
getImageToken
(
imageUrl
*
dto
.
MessageImageUrl
,
model
string
,
stream
bool
)
(
int
,
error
)
{
baseTokens
:=
85
if
model
==
"glm-4v"
{
if
model
==
"glm-4v"
{
return
1047
,
nil
return
1047
,
nil
}
}
if
imageUrl
.
Detail
==
"low"
{
if
imageUrl
.
Detail
==
"low"
{
return
85
,
nil
return
baseTokens
,
nil
}
}
// TODO: 非流模式下不计算图片token数量
// TODO: 非流模式下不计算图片token数量
if
!
constant
.
GetMediaTokenNotStream
&&
!
stream
{
if
!
constant
.
GetMediaTokenNotStream
&&
!
stream
{
...
@@ -100,6 +101,12 @@ func getImageToken(imageUrl *dto.MessageImageUrl, model string, stream bool) (in
...
@@ -100,6 +101,12 @@ func getImageToken(imageUrl *dto.MessageImageUrl, model string, stream bool) (in
if
imageUrl
.
Detail
==
"auto"
||
imageUrl
.
Detail
==
""
{
if
imageUrl
.
Detail
==
"auto"
||
imageUrl
.
Detail
==
""
{
imageUrl
.
Detail
=
"high"
imageUrl
.
Detail
=
"high"
}
}
tileTokens
:=
170
if
strings
.
HasPrefix
(
model
,
"gpt-4o-mini"
)
{
tileTokens
=
5667
baseTokens
=
2833
}
var
config
image
.
Config
var
config
image
.
Config
var
err
error
var
err
error
var
format
string
var
format
string
...
@@ -146,7 +153,7 @@ func getImageToken(imageUrl *dto.MessageImageUrl, model string, stream bool) (in
...
@@ -146,7 +153,7 @@ func getImageToken(imageUrl *dto.MessageImageUrl, model string, stream bool) (in
// 计算图片的token数量(边的长度除以512,向上取整)
// 计算图片的token数量(边的长度除以512,向上取整)
tiles
:=
(
shortSide
+
511
)
/
512
*
((
otherSide
+
511
)
/
512
)
tiles
:=
(
shortSide
+
511
)
/
512
*
((
otherSide
+
511
)
/
512
)
log
.
Printf
(
"tiles: %d"
,
tiles
)
log
.
Printf
(
"tiles: %d"
,
tiles
)
return
tiles
*
170
+
85
,
nil
return
tiles
*
tileTokens
+
baseTokens
,
nil
}
}
func
CountTokenChatRequest
(
request
dto
.
GeneralOpenAIRequest
,
model
string
)
(
int
,
error
)
{
func
CountTokenChatRequest
(
request
dto
.
GeneralOpenAIRequest
,
model
string
)
(
int
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment