update anthropic

This commit is contained in:
Chester Curme 2024-11-12 10:16:47 -05:00
parent 76971094ab
commit 7de02c5997
2 changed files with 22 additions and 6 deletions

View File

@ -1123,6 +1123,11 @@ class ChatAnthropic(BaseChatModel):
) -> int: ) -> int:
"""Count tokens in a sequence of input messages. """Count tokens in a sequence of input messages.
Args:
messages: The message inputs to tokenize.
tools: If provided, sequence of dict, BaseModel, function, or BaseTools
to be converted to tool schemas.
.. versionchanged:: 0.2.5 .. versionchanged:: 0.2.5
Uses Anthropic's token counting API to count tokens in messages. See: Uses Anthropic's token counting API to count tokens in messages. See:

View File

@ -336,6 +336,7 @@ def test_anthropic_multimodal() -> None:
assert isinstance(response.content, str) assert isinstance(response.content, str)
num_tokens = chat.get_num_tokens_from_messages(messages) num_tokens = chat.get_num_tokens_from_messages(messages)
assert num_tokens > 0 assert num_tokens > 0
import pdb; pdb.set_trace()
def test_streaming() -> None: def test_streaming() -> None:
@ -508,24 +509,34 @@ def test_with_structured_output() -> None:
def test_get_num_tokens_from_messages() -> None: def test_get_num_tokens_from_messages() -> None:
llm = ChatAnthropic(model="claude-3-5-haiku-20241022") # type: ignore[call-arg] llm = ChatAnthropic(model="claude-3-5-sonnet-20241022") # type: ignore[call-arg]
# Test simple case # Test simple case
messages = [ messages = [
SystemMessage(content="You are an assistant."), SystemMessage(content="You are a scientist"),
HumanMessage(content="What is the weather in SF?"), HumanMessage(content="Hello, Claude"),
] ]
num_tokens = llm.get_num_tokens_from_messages(messages) num_tokens = llm.get_num_tokens_from_messages(messages)
assert num_tokens > 0 assert num_tokens > 0
# Test tool use # Test tool use
@tool @tool(parse_docstring=True)
def get_weather(location: str) -> str: def get_weather(location: str) -> str:
"""Get weather report for a city""" """Get the current weather in a given location
Args:
location: The city and state, e.g. San Francisco, CA
"""
return "Sunny" return "Sunny"
messages = [ messages = [
HumanMessage(content="What is the weather in SF?"), HumanMessage(content="What's the weather like in San Francisco?"),
]
num_tokens = llm.get_num_tokens_from_messages(messages, tools=[get_weather])
assert num_tokens > 0
messages = [
HumanMessage(content="What's the weather like in San Francisco?"),
AIMessage( AIMessage(
content=[ content=[
{"text": "Let's see.", "type": "text"}, {"text": "Let's see.", "type": "text"},