From 62a5f993f2860459d493bd80e354f1754e745ed5 Mon Sep 17 00:00:00 2001 From: Chester Curme Date: Tue, 12 Nov 2024 10:29:51 -0500 Subject: [PATCH] openai --- .../langchain_openai/chat_models/base.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 8a1a63bbcdbfb..ca73ed21b8ac4 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -886,8 +886,13 @@ def get_token_ids(self, text: str) -> List[int]: _, encoding_model = self._get_encoding_model() return encoding_model.encode(text) - # TODO: Count bound tools as part of input. - def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: + def get_num_tokens_from_messages( + self, + messages: List[BaseMessage], + tools: Optional[ + Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]] + ] = None, + ) -> int: """Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package. **Requirements**: You must have the ``pillow`` installed if you want to count @@ -897,7 +902,16 @@ def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int: counting. OpenAI reference: https://github.com/openai/openai-cookbook/blob/ - main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb""" + main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb + + Args: + messages: The message inputs to tokenize. + tools: If provided, sequence of dict, BaseModel, function, or BaseTools + to be converted to tool schemas. + """ + # TODO: Count bound tools as part of input. + if tools is not None: + warnings.warn("Counting tokens in tool schemas is not yet supported.") if sys.version_info[1] <= 7: return super().get_num_tokens_from_messages(messages) model, encoding = self._get_encoding_model()