You've already forked agentic-coding-workflow
31 lines
774 B
Go
31 lines
774 B
Go
package llm
|
|
|
|
import "unicode/utf8"
|
|
|
|
// EstimateTokens 是兜底估算:按 rune 数 / 4 向上取整。
|
|
// 真正的 tiktoken/远程精确计数由各 provider 的 CountTokens 实现。
|
|
func EstimateTokens(s string) int {
|
|
if s == "" {
|
|
return 0
|
|
}
|
|
runes := utf8.RuneCountInString(s)
|
|
if runes == 0 {
|
|
return 0
|
|
}
|
|
return (runes + 3) / 4
|
|
}
|
|
|
|
// EstimateRequestTokens 累加 system prompt + 所有 messages 的 text block。
|
|
// image/document block 暂按 0 计(多模态 token 估算各家差异大,热路径不依赖)。
|
|
func EstimateRequestTokens(req Request) int {
|
|
total := EstimateTokens(req.SystemPrompt)
|
|
for _, m := range req.Messages {
|
|
for _, b := range m.Blocks {
|
|
if b.Type == "text" {
|
|
total += EstimateTokens(b.Text)
|
|
}
|
|
}
|
|
}
|
|
return total
|
|
}
|