File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ def _estimate_string_tokens (content : str | Sequence [UserContent ]) -> int :
2+ if not content :
3+ return 0
4+ if isinstance (content , str ):
5+ return len (_TOKEN_SPLIT_RE .split (content .strip ()))
6+ tokens = 0
7+ for part in content :
8+ if isinstance (part , str ):
9+ tokens += len (_TOKEN_SPLIT_RE .split (part .strip ()))
10+ elif isinstance (part , BinaryContent ):
11+ tokens += len (part .data )
12+ return tokens
13+
14+
15+ _TOKEN_SPLIT_RE = re .compile (r'[\s",.:]+' )
Original file line number Diff line number Diff line change 1+ def _estimate_string_tokens (content : str | Sequence [UserContent ]) -> int :
2+ if not content :
3+ return 0
4+ if isinstance (content , str ):
5+ return len (re .split (r'[\s",.:]+' , content .strip ()))
6+ tokens = 0
7+ for part in content :
8+ if isinstance (part , str ):
9+ tokens += len (re .split (r'[\s",.:]+' , part .strip ()))
10+ if isinstance (part , (AudioUrl , ImageUrl )):
11+ tokens += 0
12+ elif isinstance (part , BinaryContent ):
13+ tokens += len (part .data )
14+ else :
15+ tokens += 0
16+ return tokens
You can’t perform that action at this time.
0 commit comments