Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.
You must have the pillow installed if you want to count image tokens if
you are specifying the image as a base64 string, and you must have both
pillow and httpx installed if you are specifying the image as a URL. If
these aren't installed image inputs will be ignored in token counting.
get_num_tokens_from_messages(
self,
messages: Sequence[BaseMessage],
tools: Sequence[dict[str, Any] | type | Callable | BaseTool] | None = None,
*,
allow_fetching_images: bool = True
) -> int| Name | Type | Description |
|---|---|---|
messages* | Sequence[BaseMessage] | The message inputs to tokenize. |
tools | Sequence[dict[str, Any] | type | Callable | BaseTool] | None | Default: NoneIf provided, sequence of |
allow_fetching_images | bool | Default: TrueWhether to allow fetching images for token counting. |