Skip to content

Commit

Permalink
OpenAI: Lazy initialize tiktoken to avoid http at import time (#3287)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-sentry authored Jul 12, 2024
1 parent ae034ab commit 301c4b8
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
try:
import tiktoken # type: ignore

enc = tiktoken.get_encoding("cl100k_base")
enc = None # lazy initialize

def count_tokens(s):
# type: (str) -> int
global enc
if enc is None:
enc = tiktoken.get_encoding("cl100k_base")
return len(enc.encode_ordinary(s))

logger.debug("[OpenAI] using tiktoken to count tokens")
Expand Down

0 comments on commit 301c4b8

Please sign in to comment.