-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vertexai: Add ChatAnthropicVertex Text Caching Support (#672)
- Loading branch information
1 parent
1755705
commit 1f6d2d7
Showing
5 changed files
with
478 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
libs/vertexai/tests/integration_tests/test_anthropic_cache.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
"""Integration tests for Anthropic cache control functionality.""" | ||
import os | ||
from typing import Dict, List, Union | ||
|
||
import pytest | ||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage | ||
from langchain_core.prompts import ChatPromptTemplate | ||
|
||
from langchain_google_vertexai.model_garden import ChatAnthropicVertex | ||
|
||
|
||
@pytest.mark.extended | ||
def test_anthropic_system_cache() -> None: | ||
"""Test chat with system message having cache control.""" | ||
project = os.environ["PROJECT_ID"] | ||
location = "us-central1" | ||
model = ChatAnthropicVertex( | ||
project=project, | ||
location=location, | ||
) | ||
|
||
context = SystemMessage( | ||
content="You are my personal assistant. Be helpful and concise.", | ||
additional_kwargs={"cache_control": {"type": "ephemeral"}}, | ||
) | ||
message = HumanMessage(content="Hello! What can you do for me?") | ||
|
||
response = model.invoke( | ||
[context, message], model_name="claude-3-5-sonnet-v2@20241022" | ||
) | ||
assert isinstance(response, AIMessage) | ||
assert isinstance(response.content, str) | ||
assert "usage_metadata" in response.additional_kwargs | ||
assert "cache_creation_input_tokens" in response.additional_kwargs["usage_metadata"] | ||
|
||
|
||
@pytest.mark.extended | ||
def test_anthropic_mixed_cache() -> None: | ||
"""Test chat with different cache control types.""" | ||
project = os.environ["PROJECT_ID"] | ||
location = "us-central1" | ||
model = ChatAnthropicVertex( | ||
project=project, | ||
location=location, | ||
) | ||
|
||
context = SystemMessage( | ||
content=[ | ||
{ | ||
"type": "text", | ||
"text": "You are my personal assistant.", | ||
"cache_control": {"type": "ephemeral"}, | ||
} | ||
] | ||
) | ||
message = HumanMessage( | ||
content=[ | ||
{ | ||
"type": "text", | ||
"text": "What's your name and what can you help me with?", | ||
"cache_control": {"type": "ephemeral"}, | ||
} | ||
] | ||
) | ||
|
||
response = model.invoke( | ||
[context, message], model_name="claude-3-5-sonnet-v2@20241022" | ||
) | ||
assert isinstance(response, AIMessage) | ||
assert isinstance(response.content, str) | ||
assert "usage_metadata" in response.additional_kwargs | ||
|
||
|
||
@pytest.mark.extended | ||
def test_anthropic_conversation_cache() -> None: | ||
"""Test chat conversation with cache control.""" | ||
project = os.environ["PROJECT_ID"] | ||
location = "us-central1" | ||
model = ChatAnthropicVertex( | ||
project=project, | ||
location=location, | ||
) | ||
|
||
context = SystemMessage( | ||
content="You are my personal assistant. My name is Peter.", | ||
additional_kwargs={"cache_control": {"type": "ephemeral"}}, | ||
) | ||
messages = [ | ||
context, | ||
HumanMessage( | ||
content=[ | ||
{ | ||
"type": "text", | ||
"text": "What's my name?", | ||
"cache_control": {"type": "ephemeral"}, | ||
} | ||
] | ||
), | ||
AIMessage(content="Your name is Peter."), | ||
HumanMessage( | ||
content=[ | ||
{ | ||
"type": "text", | ||
"text": "Can you repeat my name?", | ||
"cache_control": {"type": "ephemeral"}, | ||
} | ||
] | ||
), | ||
] | ||
|
||
response = model.invoke(messages, model_name="claude-3-5-sonnet-v2@20241022") | ||
assert isinstance(response, AIMessage) | ||
assert isinstance(response.content, str) | ||
assert "peter" in response.content.lower() # Should remember the name | ||
|
||
|
||
@pytest.mark.extended | ||
def test_anthropic_chat_template_cache() -> None: | ||
"""Test chat template with structured content and cache control.""" | ||
project = os.environ["PROJECT_ID"] | ||
location = "us-central1" | ||
model = ChatAnthropicVertex( | ||
project=project, | ||
location=location, | ||
) | ||
|
||
content: List[Union[Dict[str, Union[str, Dict[str, str]]], str]] = [ | ||
{ | ||
"text": "You are a helpful assistant. Be concise and clear.", | ||
"type": "text", | ||
"cache_control": {"type": "ephemeral"}, | ||
} | ||
] | ||
|
||
prompt = ChatPromptTemplate.from_messages( | ||
[SystemMessage(content=content), ("human", "{input}")] | ||
) | ||
|
||
chain = prompt | model | ||
|
||
response = chain.invoke( | ||
{"input": "What's the capital of France?"}, | ||
) | ||
|
||
assert isinstance(response, AIMessage) | ||
assert isinstance(response.content, str) | ||
assert "Paris" in response.content |
Oops, something went wrong.