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

🐛 fix: Anthropic Prompt Caching Edge Case #3690

Merged
merged 1 commit into from
Aug 18, 2024

Conversation

danny-avila
Copy link
Owner

@danny-avila danny-avila commented Aug 18, 2024

Summary

I addressed an edge case in the Anthropic prompt caching mechanism, specifically improving the handling of multi-modal messages and ensuring proper cache control for text content.

  • Refactored the addCacheControl function to handle complex message structures more effectively.
  • Modified the logic to only add cache control to text content within user messages.
  • Updated the function to process up to two user messages, focusing on the most recent ones.
  • Improved efficiency by breaking the inner loop once a text content is processed in array-type messages.
  • Enhanced test coverage to include edge cases with multiple content types and messages without text blocks.
  • Ensured that non-text content (like images) are not modified with cache control properties.

Anthropic only allows 4 items to have the cache_control property per payload. Our method now correctly stays under this limit with a maximum of 3 (last 2 user messages, and +1 if system prompt is included)

Testing

To test these changes:

  1. Run the updated unit tests in addCacheControl.spec.js.
  2. Manually test the chat functionality with Anthropic models, particularly with multi-modal messages (text + images).
  3. Verify that only text content in user messages receives the cache_control property.
  4. Check that the changes do not affect the behavior of other message types or roles.

Test Configuration:

  • Node.js environment
  • Jest for running unit tests
  • Anthropic API integration (for manual testing)

I've thoroughly tested these changes with both unit tests and manual verification. The updated implementation should now correctly handle various edge cases while maintaining the intended functionality of the caching mechanism.

@danny-avila danny-avila merged commit 8ca1e4f into main Aug 18, 2024
2 checks passed
@danny-avila danny-avila deleted the fix/anthropic-caching-edge-case branch August 18, 2024 23:03
kenshinsamue pushed a commit to intelequia/LibreChat that referenced this pull request Sep 17, 2024
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.

1 participant