Do not use empty user messages in chat mode

This allows the bot to send messages by clicking on Generate with empty inputs.
This commit is contained in:
oobabooga 2023-03-24 17:22:22 -03:00 committed by GitHub
commit 507db0929d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 20 deletions

View File

@ -33,12 +33,14 @@ def generate_chat_prompt(user_input, max_new_tokens, name1, name2, context, chat
i = len(shared.history['internal'])-1 i = len(shared.history['internal'])-1
while i >= 0 and len(encode(''.join(rows), max_new_tokens)[0]) < max_length: while i >= 0 and len(encode(''.join(rows), max_new_tokens)[0]) < max_length:
rows.insert(1, f"{name2}: {shared.history['internal'][i][1].strip()}\n") rows.insert(1, f"{name2}: {shared.history['internal'][i][1].strip()}\n")
if not (shared.history['internal'][i][0] == '<|BEGIN-VISIBLE-CHAT|>'): prev_user_input = shared.history['internal'][i][0]
rows.insert(1, f"{name1}: {shared.history['internal'][i][0].strip()}\n") if len(prev_user_input) > 0 and prev_user_input != '<|BEGIN-VISIBLE-CHAT|>':
rows.insert(1, f"{name1}: {prev_user_input.strip()}\n")
i -= 1 i -= 1
if not impersonate: if not impersonate:
rows.append(f"{name1}: {user_input}\n") if len(user_input) > 0:
rows.append(f"{name1}: {user_input}\n")
rows.append(apply_extensions(f"{name2}:", "bot_prefix")) rows.append(apply_extensions(f"{name2}:", "bot_prefix"))
limit = 3 limit = 3
else: else:

View File

@ -119,13 +119,13 @@ def load_html_image(paths):
def generate_chat_html(history, name1, name2, character): def generate_chat_html(history, name1, name2, character):
output = f'<style>{cai_css}</style><div class="chat" id="chat">' output = f'<style>{cai_css}</style><div class="chat" id="chat">'
img_bot = load_html_image([f"characters/{character}.{ext}" for ext in ['png', 'jpg', 'jpeg']] + ["img_bot.png","img_bot.jpg","img_bot.jpeg"]) img_bot = load_html_image([f"characters/{character}.{ext}" for ext in ['png', 'jpg', 'jpeg']] + ["img_bot.png","img_bot.jpg","img_bot.jpeg"])
img_me = load_html_image(["img_me.png", "img_me.jpg", "img_me.jpeg"]) img_me = load_html_image(["img_me.png", "img_me.jpg", "img_me.jpeg"])
for i,_row in enumerate(history[::-1]): for i,_row in enumerate(history[::-1]):
row = [convert_to_markdown(entry) for entry in _row] row = [convert_to_markdown(entry) for entry in _row]
output += f""" output += f"""
<div class="message"> <div class="message">
<div class="circle-bot"> <div class="circle-bot">
@ -142,22 +142,24 @@ def generate_chat_html(history, name1, name2, character):
</div> </div>
""" """
if not (i == len(history)-1 and len(row[0]) == 0): if len(row[0]) == 0: # don't display empty user messages
output += f""" continue
<div class="message">
<div class="circle-you"> output += f"""
{img_me} <div class="message">
</div> <div class="circle-you">
<div class="text"> {img_me}
<div class="username"> </div>
{name1} <div class="text">
</div> <div class="username">
<div class="message-body"> {name1}
{row[0]}
</div>
</div>
</div> </div>
""" <div class="message-body">
{row[0]}
</div>
</div>
</div>
"""
output += "</div>" output += "</div>"
return output return output