diff --git a/libs/community/langchain_community/chat_models/anyscale.py b/libs/community/langchain_community/chat_models/anyscale.py index bb1adff0ca3b6..9eae0a3bc7577 100644 --- a/libs/community/langchain_community/chat_models/anyscale.py +++ b/libs/community/langchain_community/chat_models/anyscale.py @@ -5,10 +5,22 @@ import logging import os import sys -from typing import TYPE_CHECKING, Any, Dict, Optional, Set +import warnings +from typing import ( + TYPE_CHECKING, + Any, + Callable, + Dict, + Optional, + Sequence, + Set, + Type, + Union, +) import requests from langchain_core.messages import BaseMessage +from langchain_core.tools import BaseTool from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env from pydantic import Field, SecretStr, model_validator @@ -197,10 +209,18 @@ def _get_encoding_model(self) -> tuple[str, tiktoken.Encoding]: encoding = tiktoken_.get_encoding(model) return model, encoding - 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 with tiktoken package. Official documentation: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb """ + 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() diff --git a/libs/community/langchain_community/chat_models/everlyai.py b/libs/community/langchain_community/chat_models/everlyai.py index f7de8d3a34f87..ea739f904a0ea 100644 --- a/libs/community/langchain_community/chat_models/everlyai.py +++ b/libs/community/langchain_community/chat_models/everlyai.py @@ -4,9 +4,21 @@ import logging import sys -from typing import TYPE_CHECKING, Any, Dict, Optional, Set +import warnings +from typing import ( + TYPE_CHECKING, + Any, + Callable, + Dict, + Optional, + Sequence, + Set, + Type, + Union, +) from langchain_core.messages import BaseMessage +from langchain_core.tools import BaseTool from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env from pydantic import Field, model_validator @@ -138,11 +150,19 @@ def _get_encoding_model(self) -> tuple[str, tiktoken.Encoding]: encoding = tiktoken_.get_encoding(model) return model, encoding - 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 with tiktoken package. Official documentation: https://github.com/openai/openai-cookbook/blob/ main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb""" + 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() diff --git a/libs/community/langchain_community/chat_models/openai.py b/libs/community/langchain_community/chat_models/openai.py index a2b3a057295b2..475e509b553c4 100644 --- a/libs/community/langchain_community/chat_models/openai.py +++ b/libs/community/langchain_community/chat_models/openai.py @@ -46,6 +46,7 @@ ) from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult from langchain_core.runnables import Runnable +from langchain_core.tools import BaseTool from langchain_core.utils import ( get_from_dict_or_env, get_pydantic_field_names, @@ -644,11 +645,19 @@ def get_token_ids(self, text: str) -> List[int]: _, encoding_model = self._get_encoding_model() return encoding_model.encode(text) - 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. Official documentation: https://github.com/openai/openai-cookbook/blob/ main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb""" + 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()