From d368576eac299950c19017c098a77a5eb3915df1 Mon Sep 17 00:00:00 2001 From: Arpit Roopchandani <17565234+whoisarpit@users.noreply.github.com> Date: Wed, 5 Feb 2025 10:43:37 +0800 Subject: [PATCH] Fallback to gpt-4o for tiktoken if openai model unrecognized --- patchwork/common/client/llm/openai_.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/patchwork/common/client/llm/openai_.py b/patchwork/common/client/llm/openai_.py index 6573ee24e..6d6beb910 100644 --- a/patchwork/common/client/llm/openai_.py +++ b/patchwork/common/client/llm/openai_.py @@ -14,6 +14,7 @@ from typing_extensions import Dict, Iterable, List, Optional, Union from patchwork.common.client.llm.protocol import NOT_GIVEN, LlmClient, NotGiven +from patchwork.logger import logger @functools.lru_cache @@ -87,7 +88,12 @@ def is_prompt_supported( model_limit = self.__get_model_limits(model) token_count = 0 - encoding = tiktoken.encoding_for_model(model) + encoding = None + try: + encoding = tiktoken.encoding_for_model(model) + except Exception as e: + logger.error(f"Error getting encoding for model {model}: {e}, using gpt-4o as fallback") + encoding = tiktoken.encoding_for_model("gpt-4o") for message in messages: message_token_count = len(encoding.encode(message.get("content"))) token_count = token_count + message_token_count