Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added version ceiling for anthropic token count. #281

Closed

Conversation

3coins
Copy link
Collaborator

@3coins 3coins commented Nov 18, 2024

Description

Current get_num_tokens method in ChatBedrock utilizes a legacy Anthropic API count_tokens, support for which has been dropped by Anthropic in their latest release v0.39.0. This PR adds a version ceiling for the anthropic client, only supporting token count for anthropic client versions 0.38.0 and lower. Users who have a need to use token counting in their applications should follow these steps.

  1. Stop using the get_num_tokens from the ChatBedrock class.
  2. Start using get_num_tokens_from_messages from the ChatAnthropic class.

Here is some sample code that follows this strategy.

from langchain_core.messages import HumanMessage
from langchain_aws import ChatBedrock
from langchain_anthropic import ChatAnthropic

def main():
    messages = [
        HumanMessage("What is the capital of France?")
    ]

    llm = ChatBedrock(
        model="anthropic.claude-3-5-sonnet-20241022-v2:0"
    )

    token_counter_model = ChatAnthropic(
        model="claude-3-5-sonnet-20241022"
    )

    token_count = token_counter_model.get_num_tokens_from_messages(messages)
    print(f"token count is {token_count}")

    if token_count < 100:
        print(llm.invoke(messages))
    else:
        print(
            "Warning: Trim to smaller prompt to reduce token count."
        )

if __name__ == "__main__":
    main()

References

Anthropic Documentation for count_tokens API
https://docs.anthropic.com/en/api/messages-count-tokens

Dropped support for count_tokens in AnthropicLLM
https://github.com/langchain-ai/langchain/blob/master/libs/partners/anthropic/langchain_anthropic/llms.py#L375

New implementation in ChatAnthropic
https://github.com/langchain-ai/langchain/blob/master/libs/partners/anthropic/langchain_anthropic/chat_models.py#L1117

@3coins 3coins marked this pull request as ready for review November 18, 2024 23:45
@3coins 3coins closed this Dec 4, 2024
@3coins 3coins deleted the update-anthropic-token-count branch December 4, 2024 03:37
@3coins 3coins restored the update-anthropic-token-count branch December 4, 2024 03:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant