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: use reduced message to avoid maxing out tokens #21

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

alangsto
Copy link
Member

MST-2134

To avoid going over the maximum amount of tokens allowed, we should reduce the user message list and send only the most recent messages that do not go over the number of allotted tokens.

return int((len(message) - message.count(' ')) / chars_per_token) + json_padding


def get_reduced_message_list(system_list, message_list):
Copy link
Member Author

Choose a reason for hiding this comment

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

This was largely taken from https://github.com/2uinc/ai-chatbot-backend/blob/74faef96cc3732e1369d3ad5adeae9cdd79d0bc8/xpert/pipelines/utils.py#L173. I modified a few things within the while loop (like moving a break and adding it to the outer conditional) and changing the append + reverse to an insert for the new_message_list

"""
If messages are larger than allotted token amount, return a smaller list of messages.
"""
total_system_tokens = sum(_estimated_message_tokens(system_message['content']) for system_message in system_list)
Copy link
Member Author

Choose a reason for hiding this comment

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

This doesn't currently account for if the total_system_tokens is greater than the number of allowed tokens, but I couldn't find anything in https://github.com/2uinc/ai-chatbot-backend on the same codepath as this function that checks for that. I think it's fine as is but probably worth noting in the discovery for https://2u-internal.atlassian.net/browse/MST-2122.

@alangsto alangsto marked this pull request as ready for review September 11, 2023 15:31
@alangsto alangsto merged commit 2ee7e74 into main Sep 11, 2023
8 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