Arturo Jiménez de los Galanes Reguillos commited on
Commit
0580074
1 Parent(s): 06e297b

use chat template

Browse files
Files changed (1) hide show
  1. app.py +5 -14
app.py CHANGED
@@ -5,6 +5,7 @@ from threading import Thread
5
  import torch
6
 
7
  MODEL = "m-a-p/OpenCodeInterpreter-DS-33B"
 
8
 
9
  system_message = "You are a computer programmer that can translate python code to C++ in order to improve performance"
10
 
@@ -20,19 +21,6 @@ def messages_for(python):
20
  {"role": "user", "content": user_prompt_for(python)}
21
  ]
22
 
23
- def apply_chat_template(messages):
24
- result = ""
25
- for message in messages:
26
- if message['role'] == 'system':
27
- result += f"{message['content']}\n"
28
- elif message['role'] == 'user':
29
- result += f"### Instruction:\n{message['content']}\n"
30
- else:
31
- result += f"### Response:\n{message['content']}\n<|EOT|>\n"
32
-
33
- return result
34
-
35
-
36
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
37
  model = AutoModelForCausalLM.from_pretrained(MODEL, torch_dtype=torch.bfloat16, device_map="auto")
38
  model.eval()
@@ -42,7 +30,10 @@ streamer = TextIteratorStreamer(tokenizer, decode_kwargs=decode_kwargs)
42
 
43
  cplusplus = None
44
  def translate(python):
45
- inputs = tokenizer(apply_chat_template(messages_for(python)), return_tensors="pt").to(model.device)
 
 
 
46
  generation_kwargs = dict(
47
  inputs,
48
  streamer=streamer,
 
5
  import torch
6
 
7
  MODEL = "m-a-p/OpenCodeInterpreter-DS-33B"
8
+ CHAT_TEMPLATE = "{%- set found_item = false -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'system' -%}\n {%- set found_item = true -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if not found_item -%}\n{{'You are an AI programming assistant, utilizing the Deepseek Coder model, developed by Deepseek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer.\\n'}}\n{%- endif %}\n{%- for message in messages %}\n {%- if message['role'] == 'system' %}\n{{ message['content'] }}\n {%- else %}\n {%- if message['role'] == 'user' %}\n{{'### Instruction:\\n' + message['content'] + '\\n'}}\n {%- else %}\n{{'### Response:\\n' + message['content'] + '\\n<|EOT|>\\n'}}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{{'### Response:\\n'}}\n"
9
 
10
  system_message = "You are a computer programmer that can translate python code to C++ in order to improve performance"
11
 
 
21
  {"role": "user", "content": user_prompt_for(python)}
22
  ]
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
25
  model = AutoModelForCausalLM.from_pretrained(MODEL, torch_dtype=torch.bfloat16, device_map="auto")
26
  model.eval()
 
30
 
31
  cplusplus = None
32
  def translate(python):
33
+ inputs = tokenizer.apply_chat_template(
34
+ messages_for(python),
35
+ chat_template=CHAT_TEMPLATE,
36
+ return_tensors="pt").to(model.device)
37
  generation_kwargs = dict(
38
  inputs,
39
  streamer=streamer,