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
22aaa610
authored
Apr 23, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 重构计费代码
parent
86ab64ec
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
18 deletions
+15
-18
service/token_counter.go
+15
-18
No files found.
service/token_counter.go
View file @
22aaa610
...
...
@@ -128,7 +128,7 @@ func CountTokenChatRequest(request dto.GeneralOpenAIRequest, model string, check
var
openaiTools
[]
dto
.
OpenAITools
err
:=
json
.
Unmarshal
(
toolsData
,
&
openaiTools
)
if
err
!=
nil
{
return
0
,
errors
.
New
(
fmt
.
Sprintf
(
"count
tools token
fail: %s"
,
err
.
Error
())),
false
return
0
,
errors
.
New
(
fmt
.
Sprintf
(
"count
_tools_token_
fail: %s"
,
err
.
Error
())),
false
}
countStr
:=
""
for
_
,
tool
:=
range
openaiTools
{
...
...
@@ -173,26 +173,23 @@ func CountTokenMessages(messages []dto.Message, model string, checkSensitive boo
tokenNum
+=
tokensPerMessage
tokenNum
+=
getTokenNum
(
tokenEncoder
,
message
.
Role
)
if
len
(
message
.
Content
)
>
0
{
var
arrayContent
[]
dto
.
MediaMessage
if
err
:=
json
.
Unmarshal
(
message
.
Content
,
&
arrayContent
);
err
!=
nil
{
var
stringContent
string
if
err
:=
json
.
Unmarshal
(
message
.
Content
,
&
stringContent
);
err
!=
nil
{
return
0
,
err
,
false
}
else
{
if
checkSensitive
{
contains
,
words
:=
SensitiveWordContains
(
stringContent
)
if
contains
{
err
:=
fmt
.
Errorf
(
"message contains sensitive words: [%s]"
,
strings
.
Join
(
words
,
", "
))
return
0
,
err
,
true
}
}
tokenNum
+=
getTokenNum
(
tokenEncoder
,
stringContent
)
if
message
.
Name
!=
nil
{
tokenNum
+=
tokensPerName
tokenNum
+=
getTokenNum
(
tokenEncoder
,
*
message
.
Name
)
if
message
.
IsStringContent
()
{
stringContent
:=
message
.
StringContent
()
if
checkSensitive
{
contains
,
words
:=
SensitiveWordContains
(
stringContent
)
if
contains
{
err
:=
fmt
.
Errorf
(
"message contains sensitive words: [%s]"
,
strings
.
Join
(
words
,
", "
))
return
0
,
err
,
true
}
}
tokenNum
+=
getTokenNum
(
tokenEncoder
,
stringContent
)
if
message
.
Name
!=
nil
{
tokenNum
+=
tokensPerName
tokenNum
+=
getTokenNum
(
tokenEncoder
,
*
message
.
Name
)
}
}
else
{
var
err
error
arrayContent
:=
message
.
ParseContent
()
for
_
,
m
:=
range
arrayContent
{
if
m
.
Type
==
"image_url"
{
var
imageTokenNum
int
...
...
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