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

vertexai: Add ChatAnthropicVertex Text Caching Support #672

Merged
merged 6 commits into from
Jan 7, 2025

Conversation

lgesuellip
Copy link
Contributor

PR Description

This PR adds support for cache control in VertexAI ChatAnthropic text messages.

Relevant issues

#651

Type

🆕 New Feature
🧹 Refactoring
✅ Test

Changes

  • Implemented cache control handling for System and Human/AI messages
  • Added support for both message-level and content block-level cache control
  • Added CacheUsageMetadata class extending UsageMetadata to track cache-specific token usage

Testing

  • Added Integration and Unit Tests covering:
    • System and User/AI message cache control
    • Chat template with structured content and cache control
    • Mixed cache control types

content: List[Dict[str, Any]] = []

if isinstance(message.content, str):
if not message.content.strip():
return None
content.append({"type": "text", "text": message.content})
content.append(
_format_text_content(message.content, _get_cache_control(message))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nits: maybe it would be more readable if _format_text_content expects a message only, and handles cache control itself, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lkuligin
Hey, thank you all for the review!
That’s a good observation.
To avoid duplicating logic and keep the code more readable, I keep the code in the same if statement.

@lkuligin lkuligin merged commit 1f6d2d7 into langchain-ai:main Jan 7, 2025
14 of 15 checks passed
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.

2 participants