Skip to content

Commit 2c33f7e

Browse files
authored
Fallback to gpt-4o for tiktoken if openai model unrecognized (#1262)
* Fallback to gpt-4o for tiktoken if openai model unrecognized * Add estimated model limit for o3-mini * Bump version to 0.0.96
1 parent a4cfca0 commit 2c33f7e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

patchwork/common/client/llm/openai_.py

Lines changed: 8 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
@@ -36,6 +37,7 @@ class OpenAiLlmClient(LlmClient):
3637
"o1-mini": 128_000,
3738
"gpt-4o-mini": 128_000,
3839
"gpt-4o": 128_000,
40+
"o3-mini": 128_000,
3941
}
4042

4143
def __init__(self, api_key: str, base_url=None, **kwargs):
@@ -87,7 +89,12 @@ def is_prompt_supported(
8789

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "patchwork-cli"
3-
version = "0.0.95"
3+
version = "0.0.96"
44
description = ""
55
authors = ["patched.codes"]
66
license = "AGPL"

0 commit comments

Comments
 (0)