@@ -886,8 +886,13 @@ def get_token_ids(self, text: str) -> List[int]:
886
886
_ , encoding_model = self ._get_encoding_model ()
887
887
return encoding_model .encode (text )
888
888
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 :
891
896
"""Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.
892
897
893
898
**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:
897
902
counting.
898
903
899
904
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." )
901
915
if sys .version_info [1 ] <= 7 :
902
916
return super ().get_num_tokens_from_messages (messages )
903
917
model , encoding = self ._get_encoding_model ()
0 commit comments