drop support for Anthropic.get_num_tokens

This commit is contained in:
Chester Curme 2024-11-05 11:08:05 -05:00
parent b2e8df4cea
commit ff2ef48b35

View File

@ -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")