ZwwWayne
commited on
Commit
·
28a9aa9
1
Parent(s):
ec21cec
fix: fix meta instruction
Browse files- modeling_internlm.py +11 -6
modeling_internlm.py
CHANGED
|
@@ -777,13 +777,15 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
| 777 |
reordered_past += (tuple(past_state.index_select(0, beam_idx) for past_state in layer_past),)
|
| 778 |
return reordered_past
|
| 779 |
|
| 780 |
-
def build_inputs(self, tokenizer, query: str, history: List[Tuple[str, str]] = []):
|
| 781 |
prompt = ""
|
| 782 |
-
|
| 783 |
-
prompt += f"""<s><|
|
| 784 |
-
|
| 785 |
prompt += "<s>"
|
| 786 |
-
|
|
|
|
|
|
|
| 787 |
return tokenizer([prompt], return_tensors="pt")
|
| 788 |
|
| 789 |
@torch.no_grad()
|
|
@@ -797,9 +799,12 @@ class InternLMForCausalLM(InternLMPreTrainedModel):
|
|
| 797 |
do_sample: bool = True,
|
| 798 |
temperature: float = 0.8,
|
| 799 |
top_p: float = 0.8,
|
|
|
|
|
|
|
|
|
|
| 800 |
**kwargs,
|
| 801 |
):
|
| 802 |
-
inputs = self.build_inputs(tokenizer, query, history)
|
| 803 |
inputs = {k: v.to(self.device) for k, v in inputs.items() if torch.is_tensor(v)}
|
| 804 |
outputs = self.generate(
|
| 805 |
**inputs,
|
|
|
|
| 777 |
reordered_past += (tuple(past_state.index_select(0, beam_idx) for past_state in layer_past),)
|
| 778 |
return reordered_past
|
| 779 |
|
| 780 |
+
def build_inputs(self, tokenizer, query: str, history: List[Tuple[str, str]] = [], meta_instruction=""):
|
| 781 |
prompt = ""
|
| 782 |
+
if meta_instruction:
|
| 783 |
+
prompt += f"""<s><|System|>:{meta_instruction}\n"""
|
| 784 |
+
else:
|
| 785 |
prompt += "<s>"
|
| 786 |
+
for record in history:
|
| 787 |
+
prompt += f"""<|User|>:{record[0]}\n<|Bot|>:{record[1]}<eoa>\n"""
|
| 788 |
+
prompt += f"""<|User|>:{query}\n<|Bot|>:"""
|
| 789 |
return tokenizer([prompt], return_tensors="pt")
|
| 790 |
|
| 791 |
@torch.no_grad()
|
|
|
|
| 799 |
do_sample: bool = True,
|
| 800 |
temperature: float = 0.8,
|
| 801 |
top_p: float = 0.8,
|
| 802 |
+
meta_instruction: str = "You are an AI assistant whose name is InternLM (书生·浦语).\n"
|
| 803 |
+
"- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.\n"
|
| 804 |
+
"- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.",
|
| 805 |
**kwargs,
|
| 806 |
):
|
| 807 |
+
inputs = self.build_inputs(tokenizer, query, history, meta_instruction)
|
| 808 |
inputs = {k: v.to(self.device) for k, v in inputs.items() if torch.is_tensor(v)}
|
| 809 |
outputs = self.generate(
|
| 810 |
**inputs,
|