Skip to content

Commit d368576

Browse files
committed
Fallback to gpt-4o for tiktoken if openai model unrecognized
1 parent a4cfca0 commit d368576

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

patchwork/common/client/llm/openai_.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing_extensions import Dict, Iterable, List, Optional, Union
1515

1616
from patchwork.common.client.llm.protocol import NOT_GIVEN, LlmClient, NotGiven
17+
from patchwork.logger import logger
1718

1819

1920
@functools.lru_cache
@@ -87,7 +88,12 @@ def is_prompt_supported(
8788

8889
model_limit = self.__get_model_limits(model)
8990
token_count = 0
90-
encoding = tiktoken.encoding_for_model(model)
91+
encoding = None
92+
try:
93+
encoding = tiktoken.encoding_for_model(model)
94+
except Exception as e:
95+
logger.error(f"Error getting encoding for model {model}: {e}, using gpt-4o as fallback")
96+
encoding = tiktoken.encoding_for_model("gpt-4o")
9197
for message in messages:
9298
message_token_count = len(encoding.encode(message.get("content")))
9399
token_count = token_count + message_token_count

0 commit comments

Comments
 (0)