diff --git a/backend/danswer/prompts/prompt_utils.py b/backend/danswer/prompts/prompt_utils.py index cd59e97061f..5e1a3e298fb 100644 --- a/backend/danswer/prompts/prompt_utils.py +++ b/backend/danswer/prompts/prompt_utils.py @@ -13,6 +13,10 @@ from danswer.prompts.chat_prompts import CITATION_REMINDER from danswer.prompts.constants import CODE_BLOCK_PAT from danswer.search.models import InferenceChunk +from danswer.utils.logger import setup_logger + + +logger = setup_logger() MOST_BASIC_PROMPT = "You are a helpful AI assistant." @@ -136,14 +140,23 @@ def find_last_index(lst: list[int], max_prompt_tokens: int) -> int: before the list exceeds the maximum""" running_sum = 0 + if not lst: + logger.warning("Empty message history passed to find_last_index") + return 0 + last_ind = 0 for i in range(len(lst) - 1, -1, -1): running_sum += lst[i] + _PER_MESSAGE_TOKEN_BUFFER if running_sum > max_prompt_tokens: last_ind = i + 1 break + if last_ind >= len(lst): + logger.error( + f"Last message alone is too large! max_prompt_tokens: {max_prompt_tokens}, message_token_counts: {lst}" + ) raise ValueError("Last message alone is too large!") + return last_ind