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

🧮 feat: Improve structured token spending and testing; fix: Anthropic Cache Spend #3766

Merged
merged 11 commits into from
Aug 24, 2024

Conversation

danny-avila
Copy link
Owner

Summary

I implemented improvements in the structured token spending functionality and added comprehensive unit tests. The changes include:

  • Updated the calculateStructuredTokenValue method in the Transaction model to handle negative token values and ensure correct rate calculations.
  • Improved the spendStructuredTokens function to correctly handle negative token counts for prompt tokens.
  • Added extensive unit tests for both regular and structured token spending scenarios.
  • Updated the logging in spendTokens and spendStructuredTokens to provide more detailed information about transactions.
  • Refactored the spendTokens function to always record transactions, even when token counts are zero.
  • Fixed an edge case where the balance could incorrectly increase for certain Anthropic model transactions.
  • Updated the Jest and mongodb-memory-server dependencies to their latest versions for improved testing capabilities.

Testing

To test these changes:

  1. Run the unit tests using npm test in the api directory.
  2. Manually test token spending scenarios in the application, particularly with Anthropic models and structured token spending.
  3. Verify that the balance updates correctly for various token usage scenarios.
  4. Check the logs to ensure that transaction details are being recorded accurately.

Test Configuration:

  • Node.js environment
  • MongoDB instance (using mongodb-memory-server for tests)
  • Updated Jest version 29.7.0
  • Various token spending scenarios, including edge cases with zero or negative token counts

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • I have made pertinent documentation changes
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes

@danny-avila danny-avila merged commit d54458b into main Aug 24, 2024
2 checks passed
@danny-avila danny-avila deleted the fix/anthropic-caching-spend branch August 24, 2024 08:36
kenshinsamue pushed a commit to intelequia/LibreChat that referenced this pull request Sep 17, 2024
… Cache Spend (danny-avila#3766)

* chore: update jest and mongodb-memory-server dependencies

* fix: Anthropic edge case of increasing balance

* refactor: Update token calculations in Transaction model/spec

* refactor: `spendTokens` always record transactions, add Tx related tests

* feat: Add error handling for CHECK_BALANCE environment variable

* feat: Update import path for Balance model in Balance controller

* chore: remove logging

* refactor: Improve structured token spend logging in spendTokens.js

* ci: add unit tests for spend token

* ci: Improve structured token spend unit testing

* chore: improve logging phrase for no tx spent from balance
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