From ff2ef48b35e739eb4743a25cdb505b32d635e973 Mon Sep 17 00:00:00 2001 From: Chester Curme Date: Tue, 5 Nov 2024 11:08:05 -0500 Subject: [PATCH] drop support for Anthropic.get_num_tokens --- libs/partners/anthropic/langchain_anthropic/llms.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libs/partners/anthropic/langchain_anthropic/llms.py b/libs/partners/anthropic/langchain_anthropic/llms.py index 5b53663d7e2..508c71da9c4 100644 --- a/libs/partners/anthropic/langchain_anthropic/llms.py +++ b/libs/partners/anthropic/langchain_anthropic/llms.py @@ -109,7 +109,6 @@ class _AnthropicCommon(BaseLanguageModel): ) self.HUMAN_PROMPT = anthropic.HUMAN_PROMPT self.AI_PROMPT = anthropic.AI_PROMPT - self.count_tokens = self.client.count_tokens return self @property @@ -375,9 +374,11 @@ class AnthropicLLM(LLM, _AnthropicCommon): def get_num_tokens(self, text: str) -> int: """Calculate number of tokens.""" - if not self.count_tokens: - raise NameError("Please ensure the anthropic package is loaded") - return self.count_tokens(text) + raise NotImplementedError( + "Anthropic's legacy count_tokens method was removed in anthropic 0.39.0 " + "and langchain-anthropic 0.2.5. Please use " + "ChatAnthropic.get_num_tokens_from_messages instead." + ) @deprecated(since="0.1.0", removal="0.3.0", alternative="AnthropicLLM")