mlabonne commited on
Commit
6b9fdb2
ยท
verified ยท
1 Parent(s): 3f2b118

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -22,17 +22,23 @@ DESCRIPTION = os.environ.get("DESCRIPTION")
22
  @spaces.GPU()
23
  def predict(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
24
  # Format history with a given chat template
25
- if CHAT_TEMPLATE == "ChatML":
 
 
 
 
 
 
26
  stop_tokens = ["<|endoftext|>", "<|im_end|>"]
27
  instruction = '<|im_start|>system\n' + system_prompt + '\n<|im_end|>\n'
28
- for human, assistant in history:
29
- instruction += '<|im_start|>user\n' + human + '\n<|im_end|>\n<|im_start|>assistant\n' + assistant
30
  instruction += '\n<|im_start|>user\n' + message + '\n<|im_end|>\n<|im_start|>assistant\n'
31
  elif CHAT_TEMPLATE == "Mistral Instruct":
32
  stop_tokens = ["</s>", "[INST]", "[INST] ", "<s>", "[/INST]", "[/INST] "]
33
  instruction = '<s>[INST] ' + system_prompt
34
- for human, assistant in history:
35
- instruction += human + ' [/INST] ' + assistant + '</s>[INST]'
36
  instruction += ' ' + message + ' [/INST]'
37
  else:
38
  raise Exception("Incorrect chat template, select 'ChatML' or 'Mistral Instruct'")
 
22
  @spaces.GPU()
23
  def predict(message, history, system_prompt, temperature, max_new_tokens, top_k, repetition_penalty, top_p):
24
  # Format history with a given chat template
25
+ if CHAT_TEMPLATE == "Auto":
26
+ stop_tokens = [tokenizer.eos_token_id]
27
+ instruction = []
28
+ for user, assistant in history:
29
+ instruction.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
30
+ instruction.append({"role": "user", "content": message})
31
+ elif CHAT_TEMPLATE == "ChatML":
32
  stop_tokens = ["<|endoftext|>", "<|im_end|>"]
33
  instruction = '<|im_start|>system\n' + system_prompt + '\n<|im_end|>\n'
34
+ for user, assistant in history:
35
+ instruction += '<|im_start|>user\n' + user + '\n<|im_end|>\n<|im_start|>assistant\n' + assistant
36
  instruction += '\n<|im_start|>user\n' + message + '\n<|im_end|>\n<|im_start|>assistant\n'
37
  elif CHAT_TEMPLATE == "Mistral Instruct":
38
  stop_tokens = ["</s>", "[INST]", "[INST] ", "<s>", "[/INST]", "[/INST] "]
39
  instruction = '<s>[INST] ' + system_prompt
40
+ for user, assistant in history:
41
+ instruction += user + ' [/INST] ' + assistant + '</s>[INST]'
42
  instruction += ' ' + message + ' [/INST]'
43
  else:
44
  raise Exception("Incorrect chat template, select 'ChatML' or 'Mistral Instruct'")