From fac2b100a10890cbfcdcdcdbb18f07fd22944ed4 Mon Sep 17 00:00:00 2001 From: Yuhong Sun Date: Sun, 3 Nov 2024 11:24:04 -0800 Subject: [PATCH] Last Message Too Large Logging (#3039) --- backend/danswer/prompts/prompt_utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/danswer/prompts/prompt_utils.py b/backend/danswer/prompts/prompt_utils.py index cd59e97061..5e1a3e298f 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 ADDITIONAL_INFO 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