Get the number of tokens in the messages.
Useful for checking if an input fits in a model's context window.
This should be overridden by model-specific implementations to provide accurate token counts via model-specific tokenizers.
get_num_tokens_from_messages ignores tool
schemas.get_num_tokens_from_messages adds additional
prefixes to messages in represent user roles, which will add to the
overall token count. Model-specific implementations may choose to
handle this differently.get_num_tokens_from_messages(
self,
messages: list[BaseMessage],
tools: Sequence | None = None
) -> int| Name | Type | Description |
|---|---|---|
messages* | list[BaseMessage] | The message inputs to tokenize. |
tools | Sequence | None | Default: NoneIf provided, sequence of dict, |