Files
ragflow/internal/ingestion/component/knowledge_compiler/common/tokenize.go
Zhichang Yu 90f46b0b4d Go port: doc-level metadata extraction and knowledge compiler (#17536)
Ports doc-level auto-metadata extraction to Go and adds the
knowledge_compiler component with scheduler/routing. Fixes Extractor
metadata injection type assertion and enable_metadata default-on.
2026-07-29 21:06:48 +08:00

21 lines
557 B
Go

package common
// EstimateTokens is a lightweight, dependency-free token estimate used when no
// real Tokenizer is injected (≈4 chars/token, matching common LLM tokenizers'
// order of magnitude). Production wiring should inject a real Tokenizer.
func EstimateTokens(s string) int {
n := (len(s) + 3) / 4
if n == 0 {
return 0
}
return n
}
// numTokens returns the token count of s using tok when non-nil, else EstimateTokens.
func numTokens(tok Tokenizer, s string) int {
if tok != nil {
return tok.NumTokens(s)
}
return EstimateTokens(s)
}