添加对话上下文窗口管理

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-06-17 11:49:47 +08:00
co-authored by Claude
parent 54041e691b
commit 0b48dc0d7d
6 changed files with 365 additions and 15 deletions
+44
View File
@@ -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 == "" {