[OpenAI]: Encoding Model (#31402)

- **Description:** Small Fix for when getting the encoder in case of
KeyError and using the correct encoder for newer models
- **Issue:** #31390
This commit is contained in:
Mohammad Mohtashim 2025-06-11 01:00:00 +05:00 committed by GitHub
parent 40bd71caa5
commit 42eb356a44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1259,8 +1259,12 @@ class BaseChatOpenAI(BaseChatModel):
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
model = "cl100k_base"
encoding = tiktoken.get_encoding(model)
encoder = "cl100k_base"
if self.model_name.startswith("gpt-4o") or self.model_name.startswith(
"gpt-4.1"
):
encoder = "o200k_base"
encoding = tiktoken.get_encoding(encoder)
return model, encoding
def get_token_ids(self, text: str) -> list[int]: