演示 token 估算
()
| 1008 | |
| 1009 | |
| 1010 | def demo_token_estimation(): |
| 1011 | """演示 token 估算""" |
| 1012 | print("\n" + "=" * 60) |
| 1013 | print("Token 估算策略") |
| 1014 | print("=" * 60) |
| 1015 | |
| 1016 | test_cases = [ |
| 1017 | ("hello", 1 + 1), # 5 chars / 4 + 1 = 2 |
| 1018 | ("a" * 100, 100 // 4 + 1), # 100 / 4 + 1 = 26 |
| 1019 | ("", 0 + 1), # 空字符串 = 1 |
| 1020 | ("This is a typical sentence.", 27 // 4 + 1), |
| 1021 | ] |
| 1022 | |
| 1023 | print("\n 公式: len(text) // 4 + 1") |
| 1024 | print(f" {'文本':<35} {'字符数':>6} {'估算tokens':>10}") |
| 1025 | print(f" {'-'*35} {'-'*6} {'-'*10}") |
| 1026 | for text, expected in test_cases: |
| 1027 | display = text[:30] + "..." if len(text) > 30 else text |
| 1028 | print(f" {display:<35} {len(text):>6} {expected:>10}") |
| 1029 | |
| 1030 | |
| 1031 | # ============================================================ |
no outgoing calls
no test coverage detected