Improve the separation between instruct/chat modes (#1896)

This commit is contained in:
oobabooga 2023-05-07 23:47:02 -03:00 committed by GitHub
parent 9754d6a811
commit 56a5969658
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 19 deletions

View file

@ -36,7 +36,7 @@ def generate_chat_prompt(user_input, state, **kwargs):
_continue = kwargs['_continue'] if '_continue' in kwargs else False _continue = kwargs['_continue'] if '_continue' in kwargs else False
also_return_rows = kwargs['also_return_rows'] if 'also_return_rows' in kwargs else False also_return_rows = kwargs['also_return_rows'] if 'also_return_rows' in kwargs else False
is_instruct = state['mode'] == 'instruct' 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 min_rows = 3
# Finding the maximum prompt size # 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) max_length = min(get_max_prompt_length(state), chat_prompt_size)
# Building the turn templates # 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' template = '<|user|>\n<|user-message|>\n<|bot|>\n<|bot-message|>\n'
else:
template = '<|user|>: <|user-message|>\n<|bot|>: <|bot-message|>\n'
else: else:
template = state['turn_template'].replace(r'\n', '\n') template = state['turn_template'].replace(r'\n', '\n')
else:
template = '<|user|>: <|user-message|>\n<|bot|>: <|bot-message|>\n'
replacements = { replacements = {
'<|user|>': state['name1'].strip(), '<|user|>': state['name1_instruct' if is_instruct else 'name1'].strip(),
'<|bot|>': state['name2'].strip(), '<|bot|>': state['name2_instruct' if is_instruct else 'name2'].strip(),
} }
user_turn = replace_all(template.split('<|bot|>')[0], replacements) 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): def get_stopping_strings(state):
if state['mode'] == 'instruct': 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: else:
stopping_strings = [f"\n{state['name1']}:", f"\n{state['name2']}:"] 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] != '': if k in data and data[k] != '':
name1 = data[k] name1 = data[k]
break break
else:
name1 = shared.settings['name1']
for field in ['context', 'greeting', 'example_dialogue', 'char_persona', 'char_greeting', 'world_scenario']: for field in ['context', 'greeting', 'example_dialogue', 'char_persona', 'char_greeting', 'world_scenario']:
if field in data: if field in data:

View file

@ -36,7 +36,7 @@ def list_model_elements():
def list_interface_input_elements(chat=False): 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'] 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: 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() elements += list_model_elements()
return elements return elements

View file

@ -42,6 +42,7 @@ import psutil
import torch import torch
import yaml import yaml
from PIL import Image from PIL import Image
import modules.extensions as extensions_module import modules.extensions as extensions_module
from modules import chat, shared, training, ui, utils from modules import chat, shared, training, ui, utils
from modules.html_generator import chat_html_wrapper 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.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['interface_state'] = gr.State({k: None for k in shared.input_elements})
shared.gradio['Chat input'] = gr.State() 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'): 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')) 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['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['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.Tab('Character', elem_id='chat-settings'):
with gr.Row(): with gr.Row():
with gr.Column(scale=8): with gr.Column(scale=8):
shared.gradio['name1'] = gr.Textbox(value=shared.settings['name1'], lines=1, label='Your name') shared.gradio['name1'] = gr.Textbox(value=shared.settings['name1'], lines=1, label='Your name', visible=not is_instruct)
shared.gradio['name2'] = gr.Textbox(value=shared.settings['name2'], lines=1, label='Character\'s name') shared.gradio['name1_instruct'] = gr.Textbox(value=shared.settings['name1'], lines=1, label='Your name', visible=is_instruct)
shared.gradio['greeting'] = gr.Textbox(value=shared.settings['greeting'], lines=4, label='Greeting') shared.gradio['name2'] = gr.Textbox(value=shared.settings['name2'], lines=1, label='Character\'s name', visible=not is_instruct)
shared.gradio['context'] = gr.Textbox(value=shared.settings['context'], lines=4, label='Context') shared.gradio['name2_instruct'] = gr.Textbox(value=shared.settings['name2'], lines=1, label='Character\'s name', 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.') 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): with gr.Column(scale=1):
shared.gradio['character_picture'] = gr.Image(label='Character picture', type='pil') 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']) chat.redraw_html, reload_inputs, shared.gradio['display'])
shared.gradio['mode'].change( 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( lambda x: gr.update(interactive=x != 'instruct'), shared.gradio['mode'], shared.gradio['character_menu']).then(
chat.redraw_html, reload_inputs, shared.gradio['display']) chat.redraw_html, reload_inputs, shared.gradio['display'])
shared.gradio['instruction_template'].change( 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']) chat.redraw_html, reload_inputs, shared.gradio['display'])
shared.gradio['upload_chat_history'].upload( shared.gradio['upload_chat_history'].upload(