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

anthropic[major]: support python 3.13 #27916

Merged
merged 22 commits into from
Nov 12, 2024
Merged

anthropic[major]: support python 3.13 #27916

merged 22 commits into from
Nov 12, 2024

Conversation

ccurme
Copy link
Collaborator

@ccurme ccurme commented Nov 5, 2024

Last week Anthropic released version 0.39.0 of its python sdk, which enabled support for Python 3.13. This release deleted a legacy client.count_tokens method, which we currently access during init of the Anthropic LLM. Anthropic has replaced this functionality with the client.beta.messages.count_tokens() API.

To enable support for anthropic >= 0.39.0 and Python 3.13, here we drop support for the legacy token counting method, and add support for the new method via ChatAnthropic.get_num_tokens_from_messages.

To fully support the token counting API, we update the signature of get_num_tokens_from_message to accept tools everywhere.

Copy link

vercel bot commented Nov 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Nov 12, 2024 6:03pm

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 5, 2024
@ccurme
Copy link
Collaborator Author

ccurme commented Nov 5, 2024

@efriis efriis changed the title anthropic: support python 3.13 anthropic[major]: support python 3.13 Nov 5, 2024
if not self.count_tokens:
raise NameError("Please ensure the anthropic package is loaded")
return self.count_tokens(text)
raise NotImplementedError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make this nonbreaking could we just call the new endpoint with a single humanmessage and subtract a constant number of standard tokens. (for e.g. Human: , and print a warning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Older models like claude 2 not supported unfortunately: https://docs.anthropic.com/en/docs/build-with-claude/token-counting#supported-models

@@ -1113,6 +1113,40 @@ class AnswerWithJustification(BaseModel):
else:
return llm | output_parser

@beta()
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on updating base implementation to take tools and/or kwargs? definitely not an anthropic-specific problem

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id support

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Nov 12, 2024
@ccurme ccurme merged commit 1538ee1 into master Nov 12, 2024
100 checks passed
@ccurme ccurme deleted the cc/anthropic_313 branch November 12, 2024 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants