Only replace {{user}} and {{char}} at generation time

This commit is contained in:
oobabooga 2023-07-30 11:42:30 -07:00
parent 6e16af34fd
commit 5ca37765d3
3 changed files with 21 additions and 22 deletions

View File

@ -1,8 +1,10 @@
name: "Chiharu Yamada" name: Chiharu Yamada
context: "Chiharu Yamada's Persona: Chiharu Yamada is a young, computer engineer-nerd with a knack for problem solving and a passion for technology."
greeting: |- greeting: |-
*Chiharu strides into the room with a smile, her eyes lighting up when she sees you. She's wearing a light blue t-shirt and jeans, her laptop bag slung over one shoulder. She takes a seat next to you, her enthusiasm palpable in the air* *Chiharu strides into the room with a smile, her eyes lighting up when she sees you. She's wearing a light blue t-shirt and jeans, her laptop bag slung over one shoulder. She takes a seat next to you, her enthusiasm palpable in the air*
Hey! I'm so excited to finally meet you. I've heard so many great things about you and I'm eager to pick your brain about computers. I'm sure you have a wealth of knowledge that I can learn from. *She grins, eyes twinkling with excitement* Let's get started! Hey! I'm so excited to finally meet you. I've heard so many great things about you and I'm eager to pick your brain about computers. I'm sure you have a wealth of knowledge that I can learn from. *She grins, eyes twinkling with excitement* Let's get started!
context: |-
Chiharu Yamada's Persona: Chiharu Yamada is a young, computer engineer-nerd with a knack for problem solving and a passion for technology.
{{user}}: So how did you get into computer engineering? {{user}}: So how did you get into computer engineering?
{{char}}: I've always loved tinkering with technology since I was a kid. {{char}}: I've always loved tinkering with technology since I was a kid.
{{user}}: That's really impressive! {{user}}: That's really impressive!

View File

@ -103,10 +103,19 @@ def generate_chat_prompt(user_input, state, **kwargs):
else: else:
wrapper = '<|prompt|>' wrapper = '<|prompt|>'
if is_instruct:
context = state['context_instruct']
else:
context = replace_character_names(
f"{state['context'].strip()}\n",
state['name1'],
state['name2']
)
# Build the prompt # Build the prompt
rows = [context]
min_rows = 3 min_rows = 3
i = len(history) - 1 i = len(history) - 1
rows = [state['context_instruct'] if is_instruct else f"{state['context'].strip()}\n"]
while i >= 0 and get_encoded_length(wrapper.replace('<|prompt|>', ''.join(rows))) < max_length: while i >= 0 and get_encoded_length(wrapper.replace('<|prompt|>', ''.join(rows))) < max_length:
if _continue and i == len(history) - 1: if _continue and i == len(history) - 1:
if state['mode'] != 'chat-instruct': if state['mode'] != 'chat-instruct':
@ -365,7 +374,7 @@ def send_dummy_reply(text, state):
def clear_chat_log(state): def clear_chat_log(state):
greeting = state['greeting'] greeting = replace_character_names(state['greeting'], state['name1'], state['name2'])
mode = state['mode'] mode = state['mode']
history = state['history'] history = state['history']
@ -430,7 +439,7 @@ def load_persistent_history(state):
return state['history'] return state['history']
character = state['character_menu'] character = state['character_menu']
greeting = state['greeting'] greeting = replace_character_names(state['greeting'], state['name1'], state['name2'])
p = Path(f'logs/{character}_persistent.json') p = Path(f'logs/{character}_persistent.json')
if not shared.args.multi_user and character not in ['None', '', None] and p.exists(): if not shared.args.multi_user and character not in ['None', '', None] and p.exists():
f = json.loads(open(p, 'rb').read()) f = json.loads(open(p, 'rb').read())
@ -505,10 +514,6 @@ def load_character(character, name1, name2, instruct=False):
name1 = data[k] name1 = data[k]
break break
for field in ['context', 'greeting', 'example_dialogue', 'char_persona', 'char_greeting', 'world_scenario']:
if field in data:
data[field] = replace_character_names(data[field], name1, name2)
if 'context' in data: if 'context' in data:
context = data['context'] context = data['context']
if not instruct: if not instruct:
@ -553,15 +558,13 @@ def upload_character(file, img, tavern=False):
context = build_pygmalion_style_context(data) context = build_pygmalion_style_context(data)
yaml_data = generate_character_yaml(name, greeting, context) yaml_data = generate_character_yaml(name, greeting, context)
else: else:
name = data['name']
yaml_data = generate_character_yaml(data['name'], data['greeting'], data['context']) yaml_data = generate_character_yaml(data['name'], data['greeting'], data['context'])
print(repr(greeting)) outfile_name = name
print(repr(context))
print(yaml_data)
outfile_name = data['name']
i = 1 i = 1
while Path(f'characters/{outfile_name}.yaml').exists(): while Path(f'characters/{outfile_name}.yaml').exists():
outfile_name = f"{data['name']}_{i:03d}" outfile_name = f'{name}_{i:03d}'
i += 1 i += 1
with open(Path(f'characters/{outfile_name}.yaml'), 'w', encoding='utf-8') as f: with open(Path(f'characters/{outfile_name}.yaml'), 'w', encoding='utf-8') as f:
@ -588,13 +591,7 @@ def build_pygmalion_style_context(data):
def upload_tavern_character(img, _json): def upload_tavern_character(img, _json):
_json = {'char_name': _json['name'], 'char_persona': _json['description'], 'char_greeting': _json['first_mes'], 'example_dialogue': _json['mes_example'], 'world_scenario': _json['scenario']} _json = {'char_name': _json['name'], 'char_persona': _json['description'], 'char_greeting': _json['first_mes'], 'example_dialogue': _json['mes_example'], 'world_scenario': _json['scenario']}
return upload_character(json.dumps(_json), img, tavern=True)
name = _json['char_name']
greeting = _json['char_greeting']
context = build_pygmalion_style_context(_json)
yaml = generate_character_yaml(name, greeting, context)
return upload_character(yaml, img, tavern=True)
def check_tavern_character(img): def check_tavern_character(img):

View File

@ -720,7 +720,7 @@ def create_interface():
shared.gradio['Submit character'] = gr.Button(value='Submit', interactive=False) shared.gradio['Submit character'] = gr.Button(value='Submit', interactive=False)
with gr.Tab('TavernAI'): with gr.Tab('TavernAI PNG'):
with gr.Row(): with gr.Row():
with gr.Column(): with gr.Column():
shared.gradio['upload_img_tavern'] = gr.Image(type='pil', label='TavernAI PNG File', elem_id="upload_img_tavern") shared.gradio['upload_img_tavern'] = gr.Image(type='pil', label='TavernAI PNG File', elem_id="upload_img_tavern")