Skip to content

Commit 62a5f99

Browse files
committed
openai
1 parent db7b518 commit 62a5f99

File tree

1 file changed

+17
-3
lines changed
  • libs/partners/openai/langchain_openai/chat_models

1 file changed

+17
-3
lines changed

libs/partners/openai/langchain_openai/chat_models/base.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,13 @@ def get_token_ids(self, text: str) -> List[int]:
886886
_, encoding_model = self._get_encoding_model()
887887
return encoding_model.encode(text)
888888

889-
# TODO: Count bound tools as part of input.
890-
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
889+
def get_num_tokens_from_messages(
890+
self,
891+
messages: List[BaseMessage],
892+
tools: Optional[
893+
Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]
894+
] = None,
895+
) -> int:
891896
"""Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.
892897
893898
**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:
897902
counting.
898903
899904
OpenAI reference: https://github.com/openai/openai-cookbook/blob/
900-
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
905+
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
906+
907+
Args:
908+
messages: The message inputs to tokenize.
909+
tools: If provided, sequence of dict, BaseModel, function, or BaseTools
910+
to be converted to tool schemas.
911+
"""
912+
# TODO: Count bound tools as part of input.
913+
if tools is not None:
914+
warnings.warn("Counting tokens in tool schemas is not yet supported.")
901915
if sys.version_info[1] <= 7:
902916
return super().get_num_tokens_from_messages(messages)
903917
model, encoding = self._get_encoding_model()

0 commit comments

Comments
 (0)