Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save chat template #5901

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,22 +704,22 @@ def load_character(character, name1, name2):
return name1, name2, picture, greeting, context


def load_instruction_template(template):
def load_instruction_template(template, current_instruction_template=None, current_chat_template=None):
if template == 'None':
return ''
return '', current_chat_template

for filepath in [Path(f'instruction-templates/{template}.yaml'), Path('instruction-templates/Alpaca.yaml')]:
if filepath.exists():
break
else:
return ''
return '', current_chat_template

file_contents = open(filepath, 'r', encoding='utf-8').read()
data = yaml.safe_load(file_contents)
if 'instruction_template' in data:
return data['instruction_template']
return data['instruction_template'], data['chat_template'] if 'chat_template' in data else current_chat_template
else:
return jinja_template_from_old_format(data)
return jinja_template_from_old_format(data), current_chat_template


@functools.cache
Expand Down Expand Up @@ -821,9 +821,10 @@ def generate_character_yaml(name, greeting, context):
return yaml.dump(data, sort_keys=False, width=float("inf"))


def generate_instruction_template_yaml(instruction_template):
def generate_instruction_template_yaml(instruction_template, chat_template):
data = {
'instruction_template': instruction_template
'instruction_template': instruction_template,
'chat_template': chat_template
}

return my_yaml_output(data)
Expand Down
7 changes: 3 additions & 4 deletions modules/ui_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,12 @@ def create_event_handlers():
shared.gradio['delete_character'].click(lambda: gr.update(visible=True), None, gradio('character_deleter'))

shared.gradio['load_template'].click(
chat.load_instruction_template, gradio('instruction_template'), gradio('instruction_template_str')).then(
lambda: "Select template to load...", None, gradio('instruction_template'))
chat.load_instruction_template, gradio(['instruction_template', 'instruction_template_str', 'chat_template_str']), gradio(['instruction_template_str', 'chat_template_str']))

shared.gradio['save_template'].click(
lambda: 'My Template.yaml', None, gradio('save_filename')).then(
lambda x: x + '.yaml', gradio('instruction_template'), gradio('save_filename')).then(
lambda: 'instruction-templates/', None, gradio('save_root')).then(
chat.generate_instruction_template_yaml, gradio('instruction_template_str'), gradio('save_contents')).then(
chat.generate_instruction_template_yaml, gradio(['instruction_template_str', 'chat_template_str']), gradio('save_contents')).then(
lambda: gr.update(visible=True), None, gradio('file_saver'))

shared.gradio['delete_template'].click(
Expand Down