Unverified Commit 994027ff authored by liukuikun's avatar liukuikun Committed by GitHub
Browse files

[Enchance] internlm message to prompt (#499)

parent 823ad849
...@@ -173,15 +173,16 @@ class InternLMChat7B(BaseModel): ...@@ -173,15 +173,16 @@ class InternLMChat7B(BaseModel):
def __init__( def __init__(
self, self,
system='<|System|>', system='<|System|>:',
meta_instruction="""You are an AI assistant whose name is InternLM (书生·浦语). meta_instruction="""You are an AI assistant whose name is InternLM (书生·浦语).
- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless. - InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文. - InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.
""", # noqa: E501 """, # noqa: E501
user='<|User|>', user='<|User|>:',
eoh='', eoh='\n',
eoa='<eoa>', eoa='<eoa>\n',
assistant='<|Bot|>', eosys='\n',
assistant='<|Bot|>:',
stop_words=['<eoa>'], stop_words=['<eoa>'],
**kwargs): **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
...@@ -190,6 +191,7 @@ class InternLMChat7B(BaseModel): ...@@ -190,6 +191,7 @@ class InternLMChat7B(BaseModel):
self.user = user self.user = user
self.eoh = eoh self.eoh = eoh
self.eoa = eoa self.eoa = eoa
self.eosys = eosys
self.assistant = assistant self.assistant = assistant
self.stop_words = stop_words self.stop_words = stop_words
...@@ -207,12 +209,12 @@ class InternLMChat7B(BaseModel): ...@@ -207,12 +209,12 @@ class InternLMChat7B(BaseModel):
assert self.capability == 'chat', \ assert self.capability == 'chat', \
f'{type(self).__name__} has no capability of {self.capability}' f'{type(self).__name__} has no capability of {self.capability}'
if sequence_start: if sequence_start:
return f'<BOS>{self.system}:{self.meta_instruction}\n' \ return f'<BOS>{self.system}{self.meta_instruction}{self.eosys}' \
f'{self.user}:{prompt}{self.eoh}\n' \ f'{self.user}{prompt}{self.eoh}' \
f'{self.assistant}:' f'{self.assistant}'
else: else:
return f'\n{self.user}:{prompt}{self.eoh}\n' \ return f'\n{self.user}{prompt}{self.eoh}' \
f'{self.assistant}:' f'{self.assistant}'
def messages2prompt(self, messages, sequence_start=True): def messages2prompt(self, messages, sequence_start=True):
"""Return the prompt that is concatenated with other elements in the """Return the prompt that is concatenated with other elements in the
...@@ -223,17 +225,19 @@ class InternLMChat7B(BaseModel): ...@@ -223,17 +225,19 @@ class InternLMChat7B(BaseModel):
Returns: Returns:
str: the concatenated prompt str: the concatenated prompt
""" """
if isinstance(messages, str): if isinstance(messages, str):
return self.get_prompt(messages, sequence_start) return self.get_prompt(messages, sequence_start)
system, users, assistants = self._translate_messages(messages) eox_map = dict(user=self.eoh, assistant=self.eoa, system=self.eosys)
system = self.meta_instruction if not system else system ret = '<BOS>'
ret = f'<BOS>{self.system}:{system}\n' if self.meta_instruction:
for user, assistant in zip(users, assistants): ret += f'{self.system}:{self.meta_instruction}{self.eosys}'
if assistant:
ret += f'{self.user}:{user}{self.eoh}\n{self.assistant}:' \ for message in messages:
f'{assistant}{self.eoa}\n' role = message['role']
else: content = message['content']
ret += f'{self.user}:{user}{self.eoh}\n{self.assistant}:' ret += f'{eval(f"self.{role}")}{content}{eox_map[role]}'
ret += f'{self.assistant}:'
return ret return ret
...@@ -368,15 +372,16 @@ class Puyu(BaseModel): ...@@ -368,15 +372,16 @@ class Puyu(BaseModel):
""" """
if isinstance(messages, str): if isinstance(messages, str):
return self.get_prompt(messages, sequence_start) return self.get_prompt(messages, sequence_start)
system, users, assistants = self._translate_messages(messages) eox_map = dict(user=self.eoh, assistant=self.eoa, system=self.eosys)
system = self.system if not system else system ret = '<BOS>'
ret = f'<BOS>{system}{self.meta_instruction}{self.eosys}' if self.meta_instruction:
for user, assistant in zip(users, assistants): ret += f'{self.system}{self.meta_instruction}{self.eosys}'
if assistant:
ret += f'{self.user}{user}{self.eoh}{self.assistant}' \ for message in messages:
f'{assistant}{self.eoa}' role = message['role']
else: content = message['content']
ret += f'{self.user}{user}{self.eoh}{self.assistant}' ret += f'{eval(f"self.{role}")}{content}{eox_map[role]}'
ret += f'{self.assistant}'
return ret return ret
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment