Skip to content

Commit

Permalink
k
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhongsun96 committed Nov 3, 2024
1 parent 51b79f6 commit 34ebc0f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions backend/danswer/prompts/prompt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 34ebc0f

Please sign in to comment.