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

Add Support for Prompt Caching in SpiceMessages Class #107

Closed
wants to merge 3 commits into from

Conversation

mentatai[bot]
Copy link
Contributor

@mentatai mentatai bot commented Aug 25, 2024

This update introduces support for Anthropic's prompt caching feature to the SpiceMessages class in the spice library. The changes will enable faster and more cost-efficient API calls by reusing cached prompt prefixes. Additionally, cache performance metrics are tracked to verify cache hits and the number of input tokens cached.

Key Changes:

  1. Update SpiceMessages Class:

    • Added a cache argument to the message creation methods.
    • Set the cache_control parameter based on the cache argument.
  2. Modify Message Creation Functions:

    • Updated the message creation functions to handle the cache argument.
  3. Track Cache Performance Metrics:

    • Updated the get_response method in the Spice class to handle the new API response fields related to caching.
    • Logged the number of input tokens cached and verified cache hits using the client.extract_text_and_tokens method.

Example Usage:

from spice import Spice
from spice.spice_message import SpiceMessages

client = Spice()

messages = SpiceMessages(client)
messages.add_system_message("You are an AI assistant tasked with analyzing literary works.", cache=True)
messages.add_user_message("Analyze the major themes in 'Pride and Prejudice'.", cache=True)

response = await client.get_response(messages=messages, model="claude-3-5-sonnet-20240620")
print(response.text)

Acceptance Criteria:

  • The SpiceMessages class should support the cache argument.
  • The get_response method should log cache performance metrics.
  • The implementation should be backward compatible and ignore the cache argument for non-Anthropic clients.

Closes #106

Thanks for using MentatBot. Give comments a 👍 or 👎 to help me improve!

mentatai bot added 3 commits August 25, 2024 15:30
This update introduces support for Anthropic's prompt caching feature to the `SpiceMessages` class in the `spice` library. The changes will enable faster and more cost-efficient API calls by reusing cached prompt prefixes. Additionally, cache performance metrics are tracked to verify cache hits and the number of input tokens cached.

### Key Changes:
1. **Update `SpiceMessages` Class:**
   - Added a `cache` argument to the message creation methods.
   - Set the `cache_control` parameter based on the `cache` argument.

2. **Modify Message Creation Functions:**
   - Updated the message creation functions to handle the `cache` argument.

3. **Track Cache Performance Metrics:**
   - Updated the `get_response` method in the `Spice` class to handle the new API response fields related to caching.
   - Logged the number of input tokens cached and verified cache hits using the `client.extract_text_and_tokens` method.

### Example Usage:
```python
from spice import Spice
from spice.spice_message import SpiceMessages

client = Spice()

messages = SpiceMessages(client)
messages.add_system_message("You are an AI assistant tasked with analyzing literary works.", cache=True)
messages.add_user_message("Analyze the major themes in 'Pride and Prejudice'.", cache=True)

response = await client.get_response(messages=messages, model="claude-3-5-sonnet-20240620")
print(response.text)
```

### Acceptance Criteria:
- The `SpiceMessages` class should support the `cache` argument.
- The `get_response` method should log cache performance metrics.
- The implementation should be backward compatible and ignore the `cache` argument for non-Anthropic clients.
This commit addresses the CI failure encountered in the previous pull request by reformatting the `spice_message.py` file to comply with the Ruff formatting standards. The changes ensure that the code adheres to the required style guidelines, allowing the CI pipeline to pass successfully.

### Key Changes:
1. **Reformatted Function Definitions:**
   - Adjusted the formatting of function definitions to ensure proper line breaks and indentation.

2. **Reformatted Error Messages:**
   - Updated the formatting of error messages to improve readability and comply with style guidelines.

3. **Reformatted Class Methods:**
   - Reformatted class methods to ensure consistent indentation and line breaks.

These changes do not alter the functionality of the code but ensure that it meets the required formatting standards.
This commit addresses the CI failure encountered in the previous pull request by reformatting the `spice_message.py` file to comply with the Ruff formatting standards. The reformatting ensures that all code adheres to the required style guidelines, allowing the CI pipeline to pass successfully.
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.

Add Support for Prompt Caching in SpiceMessages Class
1 participant