From 56a5969658c9e692bd209cd1a9379814dc6d2b64 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Sun, 7 May 2023 23:47:02 -0300 Subject: [PATCH] Improve the separation between instruct/chat modes (#1896) --- modules/chat.py | 18 ++++++++---------- modules/ui.py | 2 +- server.py | 22 ++++++++++++++-------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/modules/chat.py b/modules/chat.py index 2481128a..0657cfba 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -36,7 +36,7 @@ def generate_chat_prompt(user_input, state, **kwargs): _continue = kwargs['_continue'] if '_continue' in kwargs else False also_return_rows = kwargs['also_return_rows'] if 'also_return_rows' in kwargs else False is_instruct = state['mode'] == 'instruct' - rows = [state['context'] if is_instruct else f"{state['context'].strip()}\n"] + rows = [state['context_instruct'] if is_instruct else f"{state['context'].strip()}\n"] min_rows = 3 # Finding the maximum prompt size @@ -47,17 +47,17 @@ def generate_chat_prompt(user_input, state, **kwargs): max_length = min(get_max_prompt_length(state), chat_prompt_size) # Building the turn templates - if 'turn_template' not in state or state['turn_template'] == '': - if is_instruct: + if is_instruct: + if 'turn_template' not in state or state['turn_template'] == '': template = '<|user|>\n<|user-message|>\n<|bot|>\n<|bot-message|>\n' else: - template = '<|user|>: <|user-message|>\n<|bot|>: <|bot-message|>\n' + template = state['turn_template'].replace(r'\n', '\n') else: - template = state['turn_template'].replace(r'\n', '\n') + template = '<|user|>: <|user-message|>\n<|bot|>: <|bot-message|>\n' replacements = { - '<|user|>': state['name1'].strip(), - '<|bot|>': state['name2'].strip(), + '<|user|>': state['name1_instruct' if is_instruct else 'name1'].strip(), + '<|bot|>': state['name2_instruct' if is_instruct else 'name2'].strip(), } user_turn = replace_all(template.split('<|bot|>')[0], replacements) @@ -102,7 +102,7 @@ def generate_chat_prompt(user_input, state, **kwargs): def get_stopping_strings(state): if state['mode'] == 'instruct': - stopping_strings = [f"\n{state['name1']}", f"\n{state['name2']}"] + stopping_strings = [f"\n{state['name1_instruct']}", f"\n{state['name2_instruct']}"] else: stopping_strings = [f"\n{state['name1']}:", f"\n{state['name2']}:"] @@ -472,8 +472,6 @@ def load_character(character, name1, name2, mode): if k in data and data[k] != '': name1 = data[k] break - else: - name1 = shared.settings['name1'] for field in ['context', 'greeting', 'example_dialogue', 'char_persona', 'char_greeting', 'world_scenario']: if field in data: diff --git a/modules/ui.py b/modules/ui.py index 28ff2f53..d8932928 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -36,7 +36,7 @@ def list_model_elements(): def list_interface_input_elements(chat=False): elements = ['max_new_tokens', 'seed', 'temperature', 'top_p', 'top_k', 'typical_p', 'repetition_penalty', 'encoder_repetition_penalty', 'no_repeat_ngram_size', 'min_length', 'do_sample', 'penalty_alpha', 'num_beams', 'length_penalty', 'early_stopping', 'add_bos_token', 'ban_eos_token', 'truncation_length', 'custom_stopping_strings', 'skip_special_tokens', 'preset_menu', 'stream'] if chat: - elements += ['name1', 'name2', 'greeting', 'context', 'turn_template', 'chat_prompt_size', 'chat_generation_attempts', 'stop_at_newline', 'mode', 'instruction_template', 'character_menu'] + elements += ['name1', 'name2', 'greeting', 'context', 'chat_prompt_size', 'chat_generation_attempts', 'stop_at_newline', 'mode', 'instruction_template', 'character_menu', 'name1_instruct', 'name2_instruct', 'context_instruct', 'turn_template'] elements += list_model_elements() return elements diff --git a/server.py b/server.py index c667f498..dc2741c4 100644 --- a/server.py +++ b/server.py @@ -42,6 +42,7 @@ import psutil import torch import yaml from PIL import Image + import modules.extensions as extensions_module from modules import chat, shared, training, ui, utils from modules.html_generator import chat_html_wrapper @@ -476,6 +477,8 @@ def create_interface(): shared.input_elements = ui.list_interface_input_elements(chat=True) shared.gradio['interface_state'] = gr.State({k: None for k in shared.input_elements}) shared.gradio['Chat input'] = gr.State() + shared.gradio['dummy'] = gr.State() + is_instruct = shared.settings['mode'] == 'instruct' with gr.Tab('Text generation', elem_id='main'): shared.gradio['display'] = gr.HTML(value=chat_html_wrapper(shared.history['visible'], shared.settings['name1'], shared.settings['name2'], 'cai-chat')) @@ -502,16 +505,19 @@ def create_interface(): shared.gradio['Clear history-cancel'] = gr.Button('Cancel', visible=False) shared.gradio['mode'] = gr.Radio(choices=['cai-chat', 'chat', 'instruct'], value=shared.settings['mode'], label='Mode') - shared.gradio['instruction_template'] = gr.Dropdown(choices=utils.get_available_instruction_templates(), label='Instruction template', value='None', visible=shared.settings['mode'] == 'instruct', info='Change this according to the model/LoRA that you are using.') + shared.gradio['instruction_template'] = gr.Dropdown(choices=utils.get_available_instruction_templates(), label='Instruction template', value='None', visible=is_instruct, info='Change this according to the model/LoRA that you are using.') with gr.Tab('Character', elem_id='chat-settings'): with gr.Row(): with gr.Column(scale=8): - shared.gradio['name1'] = gr.Textbox(value=shared.settings['name1'], lines=1, label='Your name') - shared.gradio['name2'] = gr.Textbox(value=shared.settings['name2'], lines=1, label='Character\'s name') - shared.gradio['greeting'] = gr.Textbox(value=shared.settings['greeting'], lines=4, label='Greeting') - shared.gradio['context'] = gr.Textbox(value=shared.settings['context'], lines=4, label='Context') - shared.gradio['turn_template'] = gr.Textbox(value=shared.settings['turn_template'], lines=1, label='Turn template', info='Used to precisely define the placement of spaces and new line characters in instruction prompts.') + shared.gradio['name1'] = gr.Textbox(value=shared.settings['name1'], lines=1, label='Your name', visible=not is_instruct) + shared.gradio['name1_instruct'] = gr.Textbox(value=shared.settings['name1'], lines=1, label='Your name', visible=is_instruct) + shared.gradio['name2'] = gr.Textbox(value=shared.settings['name2'], lines=1, label='Character\'s name', visible=not is_instruct) + shared.gradio['name2_instruct'] = gr.Textbox(value=shared.settings['name2'], lines=1, label='Character\'s name', visible=is_instruct) + shared.gradio['greeting'] = gr.Textbox(value=shared.settings['greeting'], lines=4, label='Greeting', visible=not is_instruct) + shared.gradio['context'] = gr.Textbox(value=shared.settings['context'], lines=4, label='Context', visible=not is_instruct) + shared.gradio['context_instruct'] = gr.Textbox(value=shared.settings['context'], lines=4, label='Context', visible=is_instruct) + shared.gradio['turn_template'] = gr.Textbox(value=shared.settings['turn_template'], lines=1, label='Turn template', info='Used to precisely define the placement of spaces and new line characters in instruction prompts.', visible=is_instruct) with gr.Column(scale=1): shared.gradio['character_picture'] = gr.Image(label='Character picture', type='pil') @@ -731,12 +737,12 @@ def create_interface(): chat.redraw_html, reload_inputs, shared.gradio['display']) shared.gradio['mode'].change( - lambda x: gr.update(visible=x == 'instruct'), shared.gradio['mode'], shared.gradio['instruction_template']).then( + lambda x: [gr.update(visible=x == 'instruct')] * 5 + [gr.update(visible=x != 'instruct')] * 4, shared.gradio['mode'], [shared.gradio[k] for k in ['instruction_template', 'name1_instruct', 'name2_instruct', 'context_instruct', 'turn_template', 'name1', 'name2', 'context', 'greeting']]).then( lambda x: gr.update(interactive=x != 'instruct'), shared.gradio['mode'], shared.gradio['character_menu']).then( chat.redraw_html, reload_inputs, shared.gradio['display']) shared.gradio['instruction_template'].change( - chat.load_character, [shared.gradio[k] for k in ['instruction_template', 'name1', 'name2', 'mode']], [shared.gradio[k] for k in ['name1', 'name2', 'character_picture', 'greeting', 'context', 'turn_template', 'display']]).then( + chat.load_character, [shared.gradio[k] for k in ['instruction_template', 'name1_instruct', 'name2_instruct', 'mode']], [shared.gradio[k] for k in ['name1_instruct', 'name2_instruct', 'dummy', 'dummy', 'context_instruct', 'turn_template', 'display']]).then( chat.redraw_html, reload_inputs, shared.gradio['display']) shared.gradio['upload_chat_history'].upload(