Refactor chat functions (#2003)
This commit is contained in:
parent
4e9da22c58
commit
638c6a65a2
8 changed files with 138 additions and 157 deletions
|
@ -101,10 +101,10 @@ def fix_galactica(s):
|
|||
return s
|
||||
|
||||
|
||||
def get_reply_from_output_ids(output_ids, input_ids, original_question, state):
|
||||
def get_reply_from_output_ids(output_ids, input_ids, original_question, state, is_chat=False):
|
||||
if shared.model_type == 'HF_seq2seq':
|
||||
reply = decode(output_ids, state['skip_special_tokens'])
|
||||
if not shared.is_chat():
|
||||
if not is_chat:
|
||||
reply = apply_extensions('output', reply)
|
||||
else:
|
||||
new_tokens = len(output_ids) - len(input_ids[0])
|
||||
|
@ -114,24 +114,21 @@ def get_reply_from_output_ids(output_ids, input_ids, original_question, state):
|
|||
if len(original_question) > 0 and original_question[-1] not in [' ', '\n']:
|
||||
reply = ' ' + reply
|
||||
|
||||
if not shared.is_chat():
|
||||
if not is_chat:
|
||||
reply = original_question + apply_extensions('output', reply)
|
||||
|
||||
return reply
|
||||
|
||||
|
||||
def formatted_outputs(reply, model_name):
|
||||
if not shared.is_chat():
|
||||
if shared.model_type == 'galactica':
|
||||
reply = fix_galactica(reply)
|
||||
return reply, reply, generate_basic_html(reply)
|
||||
elif shared.model_type == 'gpt4chan':
|
||||
reply = fix_gpt4chan(reply)
|
||||
return reply, 'Only applicable for GALACTICA models.', generate_4chan_html(reply)
|
||||
else:
|
||||
return reply, 'Only applicable for GALACTICA models.', generate_basic_html(reply)
|
||||
if shared.model_type == 'galactica':
|
||||
reply = fix_galactica(reply)
|
||||
return reply, reply, generate_basic_html(reply)
|
||||
elif shared.model_type == 'gpt4chan':
|
||||
reply = fix_gpt4chan(reply)
|
||||
return reply, 'Only applicable for GALACTICA models.', generate_4chan_html(reply)
|
||||
else:
|
||||
return reply
|
||||
return reply, 'Only applicable for GALACTICA models.', generate_basic_html(reply)
|
||||
|
||||
|
||||
def set_manual_seed(seed):
|
||||
|
@ -150,13 +147,18 @@ def stop_everything_event():
|
|||
shared.stop_everything = True
|
||||
|
||||
|
||||
def generate_reply(question, state, eos_token=None, stopping_strings=None):
|
||||
def generate_reply_wrapper(question, state, eos_token=None, stopping_strings=None):
|
||||
for reply in generate_reply(question, state, eos_token, stopping_strings, is_chat=False):
|
||||
yield formatted_outputs(reply, shared.model_name)
|
||||
|
||||
|
||||
def generate_reply(question, state, eos_token=None, stopping_strings=None, is_chat=False):
|
||||
state = apply_extensions('state', state)
|
||||
generate_func = apply_extensions('custom_generate_reply')
|
||||
if generate_func is None:
|
||||
if shared.model_name == 'None' or shared.model is None:
|
||||
logging.error("No model is loaded! Select one in the Model tab.")
|
||||
yield formatted_outputs(question, shared.model_name)
|
||||
yield question
|
||||
return
|
||||
|
||||
if shared.model_type in ['rwkv', 'llamacpp']:
|
||||
|
@ -168,7 +170,7 @@ def generate_reply(question, state, eos_token=None, stopping_strings=None):
|
|||
|
||||
# Preparing the input
|
||||
original_question = question
|
||||
if not shared.is_chat():
|
||||
if not is_chat:
|
||||
question = apply_extensions('input', question)
|
||||
|
||||
if shared.args.verbose:
|
||||
|
@ -177,11 +179,11 @@ def generate_reply(question, state, eos_token=None, stopping_strings=None):
|
|||
shared.stop_everything = False
|
||||
clear_torch_cache()
|
||||
seed = set_manual_seed(state['seed'])
|
||||
for reply in generate_func(question, original_question, seed, state, eos_token, stopping_strings):
|
||||
yield formatted_outputs(reply, shared.model_name)
|
||||
for reply in generate_func(question, original_question, seed, state, eos_token, stopping_strings, is_chat=is_chat):
|
||||
yield reply
|
||||
|
||||
|
||||
def generate_reply_HF(question, original_question, seed, state, eos_token=None, stopping_strings=None):
|
||||
def generate_reply_HF(question, original_question, seed, state, eos_token=None, stopping_strings=None, is_chat=False):
|
||||
generate_params = {}
|
||||
for k in ['max_new_tokens', 'do_sample', 'temperature', 'top_p', 'typical_p', 'repetition_penalty', 'encoder_repetition_penalty', 'top_k', 'min_length', 'no_repeat_ngram_size', 'num_beams', 'penalty_alpha', 'length_penalty', 'early_stopping']:
|
||||
generate_params[k] = state[k]
|
||||
|
@ -233,7 +235,7 @@ def generate_reply_HF(question, original_question, seed, state, eos_token=None,
|
|||
|
||||
t0 = time.time()
|
||||
try:
|
||||
if not shared.is_chat() and shared.model_type != 'HF_seq2seq':
|
||||
if not is_chat and shared.model_type != 'HF_seq2seq':
|
||||
yield original_question
|
||||
|
||||
# Generate the entire reply at once.
|
||||
|
@ -246,7 +248,7 @@ def generate_reply_HF(question, original_question, seed, state, eos_token=None,
|
|||
if shared.soft_prompt:
|
||||
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
||||
|
||||
yield get_reply_from_output_ids(output, input_ids, original_question, state)
|
||||
yield get_reply_from_output_ids(output, input_ids, original_question, state, is_chat=is_chat)
|
||||
|
||||
# Stream the reply 1 token at a time.
|
||||
# This is based on the trick of using 'stopping_criteria' to create an iterator.
|
||||
|
@ -266,7 +268,7 @@ def generate_reply_HF(question, original_question, seed, state, eos_token=None,
|
|||
if shared.soft_prompt:
|
||||
output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:]))
|
||||
|
||||
yield get_reply_from_output_ids(output, input_ids, original_question, state)
|
||||
yield get_reply_from_output_ids(output, input_ids, original_question, state, is_chat=is_chat)
|
||||
if output[-1] in eos_token_ids:
|
||||
break
|
||||
|
||||
|
@ -280,7 +282,7 @@ def generate_reply_HF(question, original_question, seed, state, eos_token=None,
|
|||
return
|
||||
|
||||
|
||||
def generate_reply_custom(question, original_question, seed, state, eos_token=None, stopping_strings=None):
|
||||
def generate_reply_custom(question, original_question, seed, state, eos_token=None, stopping_strings=None, is_chat=False):
|
||||
seed = set_manual_seed(state['seed'])
|
||||
generate_params = {'token_count': state['max_new_tokens']}
|
||||
for k in ['temperature', 'top_p', 'top_k', 'repetition_penalty']:
|
||||
|
@ -288,13 +290,13 @@ def generate_reply_custom(question, original_question, seed, state, eos_token=No
|
|||
|
||||
t0 = time.time()
|
||||
try:
|
||||
if not shared.is_chat():
|
||||
if not is_chat:
|
||||
yield question
|
||||
|
||||
if not state['stream']:
|
||||
reply = shared.model.generate(context=question, **generate_params)
|
||||
output = original_question + reply
|
||||
if not shared.is_chat():
|
||||
if not is_chat:
|
||||
reply = original_question + apply_extensions('output', reply)
|
||||
|
||||
yield reply
|
||||
|
@ -302,7 +304,7 @@ def generate_reply_custom(question, original_question, seed, state, eos_token=No
|
|||
|
||||
for reply in shared.model.generate_with_streaming(context=question, **generate_params):
|
||||
output = original_question + reply
|
||||
if not shared.is_chat():
|
||||
if not is_chat:
|
||||
reply = original_question + apply_extensions('output', reply)
|
||||
|
||||
yield reply
|
||||
|
@ -317,7 +319,7 @@ def generate_reply_custom(question, original_question, seed, state, eos_token=No
|
|||
return
|
||||
|
||||
|
||||
def generate_reply_flexgen(question, original_question, seed, state, eos_token=None, stopping_strings=None):
|
||||
def generate_reply_flexgen(question, original_question, seed, state, eos_token=None, stopping_strings=None, is_chat=False):
|
||||
generate_params = {}
|
||||
for k in ['max_new_tokens', 'do_sample', 'temperature']:
|
||||
generate_params[k] = state[k]
|
||||
|
@ -346,7 +348,7 @@ def generate_reply_flexgen(question, original_question, seed, state, eos_token=N
|
|||
|
||||
t0 = time.time()
|
||||
try:
|
||||
if not shared.is_chat():
|
||||
if not is_chat:
|
||||
yield question
|
||||
|
||||
# Generate the entire reply at once.
|
||||
|
@ -354,7 +356,7 @@ def generate_reply_flexgen(question, original_question, seed, state, eos_token=N
|
|||
with torch.no_grad():
|
||||
output = shared.model.generate(**generate_params)[0]
|
||||
|
||||
yield get_reply_from_output_ids(output, input_ids, original_question, state)
|
||||
yield get_reply_from_output_ids(output, input_ids, original_question, state, is_chat=is_chat)
|
||||
|
||||
# Stream the output naively for FlexGen since it doesn't support 'stopping_criteria'
|
||||
else:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue