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

Bump anthropic from 0.37.1 to 0.39.0 #355

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/log10/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def convert_claude_to_messages(prompt: str):

@staticmethod
def create_tokens_usage(prompt: str, completion: str):
client = anthropic.Anthropic()
prompt_tokens = client.count_tokens(prompt)
completion_tokens = client.count_tokens(completion)
# count_tokens is removed from the API in 0.39.0, consider to deprecate anthropic completion
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

anthropic 0.39.0 removed "legacy client.count_tokens() method". We only call create_tokens_usage for message using legacy completion model, like claude-2.1. Tried to use a new API beta.messages.count_tokens but got 400 error from the API.
https://github.com/anthropics/anthropic-sdk-python/releases

Consider the rare usage of legacy models, I set the token to 0 and we could discuss if we want to deprecate the support in future.

prompt_tokens = 0
completion_tokens = 0
total_tokens = prompt_tokens + completion_tokens

# Imitate OpenAI usage format.
Expand Down
5 changes: 3 additions & 2 deletions tests/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def test_chat_openai_messages(session, openai_model):
_LogAssertion(completion_id=session.last_completion_id(), message_content=content).assert_chat_response()


@pytest.mark.skip(reason="Anthropic API removed count_tokens in 0.39.0, langchain_community not updated")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

error in langchain-community:

cls = <class 'langchain_community.chat_models.anthropic.ChatAnthropic'>
values = {'AI_PROMPT': '\n\nAssistant:', 'HUMAN_PROMPT': '\n\nHuman:', 'anthropic_api_key': SecretStr('**********'), 'anthropic_api_url': 'https://api.anthropic.com', ...}

    @pre_init
    def validate_environment(cls, values: Dict) -> Dict:
        """Validate that api key and python package exists in environment."""
        values["anthropic_api_key"] = convert_to_secret_str(
            get_from_dict_or_env(values, "anthropic_api_key", "ANTHROPIC_API_KEY")
        )
        # Get custom api url from environment.
        values["anthropic_api_url"] = get_from_dict_or_env(
            values,
            "anthropic_api_url",
            "ANTHROPIC_API_URL",
            default="https://api.anthropic.com",
        )

        try:
            import anthropic

            check_package_version("anthropic", gte_version="0.3")
            values["client"] = anthropic.Anthropic(
                base_url=values["anthropic_api_url"],
                api_key=values["anthropic_api_key"].get_secret_value(),
                timeout=values["default_request_timeout"],
                max_retries=values["max_retries"],
            )
            values["async_client"] = anthropic.AsyncAnthropic(
                base_url=values["anthropic_api_url"],
                api_key=values["anthropic_api_key"].get_secret_value(),
                timeout=values["default_request_timeout"],
                max_retries=values["max_retries"],
            )
            values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT
            values["AI_PROMPT"] = anthropic.AI_PROMPT
>           values["count_tokens"] = values["client"].count_tokens
E           AttributeError: 'Anthropic' object has no attribute 'count_tokens'

venv/lib/python3.12/site-packages/langchain_community/llms/anthropic.py:108: AttributeError

Copy link
Collaborator

Choose a reason for hiding this comment

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

discussed offline that the test will be enabled with a follow up to upgrade the langchain anthropic 0.3.

@pytest.mark.chat
def test_chat_anthropic_messages(session, anthropic_legacy_model):
def test_chat_anthropic_messages(session, anthropic_model):
log10(anthropic)
llm = ChatAnthropic(model=anthropic_legacy_model, temperature=0.7)
llm = ChatAnthropic(model=anthropic_model, temperature=0.7)
messages = [SystemMessage(content="You are a ping pong machine"), HumanMessage(content="Ping?")]
completion = llm.predict_messages(messages)

Expand Down
Loading