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

Improve message trimming #787

Merged
merged 3 commits into from
Nov 11, 2023
Merged

Conversation

thiel-ph
Copy link

@thiel-ph thiel-ph commented Nov 9, 2023

Changes:

  • Added the token counter code from OpenAI cookbook to make sure we have the right token count.
  • Fixed a small bug in process_system_message.
  • Improved the logic in process_messages.
  • Ensured that the original list is not affected after trimming.

These changes should resolve issue #742, along with some other edge cases as in the test file.

Copy link

vercel bot commented Nov 9, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 9, 2023 6:48pm

Copy link
Contributor

@ishaan-jaff ishaan-jaff left a comment

Choose a reason for hiding this comment

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

looks great ! thanks for the contribution

if key == "name":
num_tokens += tokens_per_name
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
Copy link
Contributor

Choose a reason for hiding this comment

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

can we just have one token counter ? Can you add these changes to token_counter ?

Copy link
Contributor

Choose a reason for hiding this comment

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

just requesting this one change ^

unless there's a strong reason to have two token counters ? @duc-phamh

Copy link
Author

@thiel-ph thiel-ph Nov 10, 2023

Choose a reason for hiding this comment

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

The openai_token_counter recursively calls itself. Of course we can refactor it into one token counter, but leaving the OpenAI code separate like this would make it easier to apply future changes in case OpenAI update this function. Therefore, I believe it's better to leave it this way.

Please let me know if you think otherwise.

@thiel-ph thiel-ph requested a review from ishaan-jaff November 10, 2023 06:56
Copy link
Contributor

@ishaan-jaff ishaan-jaff left a comment

Choose a reason for hiding this comment

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

looks good to me

@ishaan-jaff
Copy link
Contributor

@duc-phamh going to wait until we can get a new release of our existing code base and then merge this

@thiel-ph
Copy link
Author

Thank you, we really look forward to it. Our production relies on this, and occasionally we'd have error with message generation without the fix here.

@ishaan-jaff ishaan-jaff merged commit fd6064b into BerriAI:main Nov 11, 2023
2 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