Source code for langchain_community.utilities.anthropic
fromtypingimportAny,Listdef_get_anthropic_client()->Any:try:importanthropicexceptImportError:raiseImportError("Could not import anthropic python package. ""This is needed in order to accurately tokenize the text ""for anthropic models. Please install it with `pip install anthropic`.")returnanthropic.Anthropic()
[docs]defget_num_tokens_anthropic(text:str)->int:"""Get the number of tokens in a string of text."""client=_get_anthropic_client()returnclient.count_tokens(text=text)
[docs]defget_token_ids_anthropic(text:str)->List[int]:"""Get the token ids for a string of text."""client=_get_anthropic_client()tokenizer=client.get_tokenizer()encoded_text=tokenizer.encode(text)returnencoded_text.ids