Right-truncate long chat completion prompts instead of left-truncating

Instructions are usually at the beginning of the prompt.
This commit is contained in:
oobabooga 2024-03-07 08:50:24 -08:00
parent 104573f7d4
commit bae14c8f13

View File

@ -197,16 +197,16 @@ def generate_chat_prompt(user_input, state, **kwargs):
while right - left > 1: while right - left > 1:
mid = (left + right) // 2 mid = (left + right) // 2
messages[-1]['content'] = user_message[mid:] messages[-1]['content'] = user_message[:mid]
prompt = make_prompt(messages) prompt = make_prompt(messages)
encoded_length = get_encoded_length(prompt) encoded_length = get_encoded_length(prompt)
if encoded_length <= max_length: if encoded_length <= max_length:
right = mid
else:
left = mid left = mid
else:
right = mid
messages[-1]['content'] = user_message[right:] messages[-1]['content'] = user_message[:left]
prompt = make_prompt(messages) prompt = make_prompt(messages)
encoded_length = get_encoded_length(prompt) encoded_length = get_encoded_length(prompt)
if encoded_length > max_length: if encoded_length > max_length: