generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Bedrock token count and IDs for Anthropic models (#341)
Fixes #314 The `get_num_tokens()` and `get_token_ids()` methods of `BedrockLLM` and `ChatBedrock` currently fail when used with Anthropic models and `anthropic>=0.39.0` installed. In such scenarios, this PR fixes `get_num_tokens()` and `get_token_ids()` by falling back to the base class implementations [in `BaseLanguageModel`](https://python.langchain.com/api_reference/core/language_models/langchain_core.language_models.base.BaseLanguageModel.html#langchain_core.language_models.base.BaseLanguageModel.get_num_tokens) and [in `BaseChatModel`](https://python.langchain.com/api_reference/core/language_models/langchain_core.language_models.chat_models.BaseChatModel.html#langchain_core.language_models.chat_models.BaseChatModel.get_num_tokens), which use the HuggingFace [GPT2 Tokenizer](https://huggingface.co/docs/transformers/en/model_doc/gpt2#transformers.GPT2TokenizerFast). If `anthropic<=0.38.0` (and other requirements) are present instead, the Anthropic SDK token methods will continue to be used as normal. **Note about tokenizer accuracy:** The GPT2 and Anthropic SDK tokenizers (see [here](https://github.com/anthropics/anthropic-sdk-python/pull/726/files#diff-9595fafc42ceb1044adb6f4f2a93774f58704ee22b6f9d40ad9d0a336c118d39L540-L542)) both may produce inaccurate token estimates for Claude 3 and 3.5 models. To obtain a more accurate estimate, Anthropic recommends using the new [Count Message Tokens API](https://docs.anthropic.com/en/api/messages-count-tokens). For example: ``` import os import anthropic os.environ["ANTHROPIC_API_KEY"] = "<your-api-key>" anthropic.Anthropic().messages.count_tokens( model="claude-3-5-sonnet-20241022", messages=[ {"role": "user", "content": "Hello, world"} ] ) ``` As another alternative, you can implement your own token counter method, and pass this using [`custom_get_token_ids`](https://python.langchain.com/api_reference/core/language_models/langchain_core.language_models.base.BaseLanguageModel.html#langchain_core.language_models.base.BaseLanguageModel.custom_get_token_ids) when initializing the model. This will override both the GPT2 and Anthropic tokenizers. --------- Co-authored-by: Piyush Jain <[email protected]>
- Loading branch information
1 parent
c5ec714
commit 6355b0f
Showing
4 changed files
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters