Load the gpt-4o encoding lazily. tiktoken may download the encoding from a remote host on first use; in environments without egress that raises, so a failure is cached and callers fall back to a character-based approximation rather than crashing.
()
| 12 | |
| 13 | |
| 14 | def _get_encoding() -> tiktoken.Encoding | None: |
| 15 | """Load the gpt-4o encoding lazily. tiktoken may download the encoding from a |
| 16 | remote host on first use; in environments without egress that raises, so a |
| 17 | failure is cached and callers fall back to a character-based approximation |
| 18 | rather than crashing.""" |
| 19 | global _encoding, _encoding_load_failed |
| 20 | if _encoding is not None or _encoding_load_failed: |
| 21 | return _encoding |
| 22 | try: |
| 23 | _encoding = tiktoken.encoding_for_model("gpt-4o") |
| 24 | except Exception: |
| 25 | _encoding_load_failed = True |
| 26 | LOG.warning("Failed to load tiktoken encoding; falling back to approximate token counting", exc_info=True) |
| 27 | return _encoding |
| 28 | |
| 29 | |
| 30 | def count_tokens(text: str) -> int: |
no test coverage detected