@@ -8,6 +8,8 @@ import (
|
||||
"unicode"
|
||||
|
||||
"aichat/message"
|
||||
|
||||
"github.com/volcengine/volcengine-go-sdk/service/arkruntime/model"
|
||||
)
|
||||
|
||||
type Stats struct {
|
||||
@@ -97,6 +99,48 @@ func EstimateChatMessagesTokens(messages []message.ChatMessage) int {
|
||||
return total
|
||||
}
|
||||
|
||||
func EstimateArkMessagesTokens(messages []*model.ChatCompletionMessage) int {
|
||||
total := 0
|
||||
for _, msg := range messages {
|
||||
if msg == nil {
|
||||
continue
|
||||
}
|
||||
total += EstimateTokenCount(string(msg.Role)) + estimateArkContentTokens(msg.Content) + 4
|
||||
if msg.ToolCallID != "" {
|
||||
total += EstimateTokenCount(msg.ToolCallID) + 2
|
||||
}
|
||||
for _, call := range msg.ToolCalls {
|
||||
if call == nil {
|
||||
continue
|
||||
}
|
||||
total += EstimateTokenCount(call.ID) + EstimateTokenCount(call.Function.Name) + EstimateTokenCount(call.Function.Arguments) + 8
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func estimateArkContentTokens(content *model.ChatCompletionMessageContent) int {
|
||||
if content == nil {
|
||||
return 0
|
||||
}
|
||||
if content.StringValue != nil {
|
||||
return EstimateTokenCount(*content.StringValue)
|
||||
}
|
||||
total := 0
|
||||
for _, part := range content.ListValue {
|
||||
if part == nil {
|
||||
continue
|
||||
}
|
||||
if part.Text != "" {
|
||||
total += EstimateTokenCount(part.Text)
|
||||
}
|
||||
if part.ImageURL != nil {
|
||||
total += 85
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func EstimateTokenCount(text string) int {
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
|
||||
Reference in New Issue
Block a user