Spaces:
Paused
Paused
:gem: [Feature] MessageComposer: Support openchat-3.5, and generalize preprocessing stages
Browse files2 unmodified lines3456789101112131425 unmodified lines404142434445464748495051525354555657585960616263646566676869707172737475763 unmodified lines80818283848586878889909192939495964 unmodified lines1011021031041051061071081091101111 unmodified line1131141151161171181191201 unmodified line1221231241251261272 unmodified linesclass MessageComposer:"""models:- mixtral-8x7b (mistralai/Mixtral-8x7B-Instruct-v0.1)"""def __init__(self, model: str = None):self.inst_roles = ["user", "system", "inst"]self.answer_roles = ["assistant", "bot", "answer"]25 unmodified linesreturn concat_messagesdef merge(self, messages) -> str:# <s> [INST] Instruction [/INST] Model answer </s> [INST] Follow-up instruction [/INST]self.messages = self.concat_messages_by_role(messages)self.merged_str = ""self.cached_str = ""for message in self.messages:role = message["role"]content = message["content"]if role in self.inst_roles:self.cached_str = f"[INST] {content} [/INST]"elif role in self.answer_roles:self.merged_str += f"<s> {self.cached_str} {content} </s>\n"self.cached_str = ""else:self.cached_str = f"[INST] {content} [/INST]"if self.cached_str:self.merged_str += f"{self.cached_str}"return self.merged_strdef split(self, merged_str) -> list:self.messages = []self.merged_str = merged_strpair_pattern = (r"<s>\s*\[INST\](?P<inst>[\s\S]*?)\[/INST\](?P<answer>[\s\S]*?)</s>")pair_matches = re.finditer(pair_pattern, self.merged_str, re.MULTILINE)pair_matches_list = list(pair_matches)if len(pair_matches_list) <= 0:self.messages = [{"role": "user","content": self.merged_str,3 unmodified linesfor match in pair_matches_list:inst = match.group("inst")answer = match.group("answer")self.messages.extend([{"role": "user", "content": inst.strip()},{"role": "assistant", "content": answer.strip()},])inst_pattern = r"\[INST\](?P<inst>[\s\S]*?)\[/INST\]"inst_matches = re.finditer(inst_pattern, self.merged_str, re.MULTILINE)inst_matches_list = list(inst_matches)if len(inst_matches_list) > len(pair_matches_list):self.messages.extend([4 unmodified lines])return self.messagesif __name__ == "__main__":composer = MessageComposer()messages = [{"role": "system",1 unmodified line},{"role": "user", "content": "Hello, who are you?"},{"role": "assistant", "content": "I am a bot."},# {"role": "user", "content": "What is your name?"},{"role": "assistant", "content": "My name is Bing."},# {"role": "user", "content": "Tell me a joke."},# {"role": "assistant", "content": "What is a robot's favorite type of music?"},# {1 unmodified line# "content": "How many questions have I asked? Please list them.",# },]merged_str = composer.merge(messages)print(merged_str)pprint(composer.split(merged_str))2 unmodified lines3456789101112131415161718192025 unmodified lines4647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061073 unmodified lines1111121131141151161171181191201211221231241254 unmodified lines1301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851 unmodified line1871881891901911921931941 unmodified line1961971981992002012022 unmodified linesclass MessageComposer:# LINK - apis/chat_api.py#available-modelsAVALAIBLE_MODELS = ["mixtral-8x7b","mistral-7b","openchat-3.5",]def __init__(self, model: str = None):if model in self.AVALAIBLE_MODELS:self.model = modelelse:self.model = "mixtral-8x7b"self.inst_roles = ["user", "system", "inst"]self.answer_roles = ["assistant", "bot", "answer"]25 unmodified linesreturn concat_messagesdef merge(self, messages) -> str:# Mistral and Mixtral:# <s> [INST] Instruction [/INST] Model answer </s> [INST] Follow-up instruction [/INST]# OpenChat:# GPT4 Correct User: Hello<|end_of_turn|>GPT4 Correct Assistant: Hi<|end_of_turn|>GPT4 Correct User: How are you today?<|end_of_turn|>GPT4 Correct Assistant:self.messages = self.concat_messages_by_role(messages)self.merged_str = ""if self.model in ["mixtral-8x7b", "mistral-7b"]:self.cached_str = ""for message in self.messages:role = message["role"]content = message["content"]if role in self.inst_roles:self.cached_str = f"[INST] {content} [/INST]"elif role in self.answer_roles:self.merged_str += f"<s> {self.cached_str} {content} </s>\n"self.cached_str = ""else:self.cached_str = f"[INST] {content} [/INST]"if self.cached_str:self.merged_str += f"{self.cached_str}"elif self.model in ["openchat-3.5"]:self.merged_str_list = []self.end_of_turn = "<|end_of_turn|>"for message in self.messages:role = message["role"]content = message["content"]if role in self.inst_roles:self.merged_str_list.append(f"GPT4 Correct User:\n{content}{self.end_of_turn}")elif role in self.answer_roles:self.merged_str_list.append(f"GPT4 Correct Assistant:\n{content}{self.end_of_turn}")else:self.merged_str_list.append(f"GPT4 Correct User: {content}{self.end_of_turn}")self.merged_str_list.append(f"GPT4 Correct Assistant:\n")self.merged_str = "\n".join(self.merged_str_list)else:self.merged_str = "\n".join([f'`{message["role"]}`:\n{message["content"]}\n'for message in self.messages])return self.merged_strdef convert_pair_matches_to_messages(self, pair_matches_list):messages = []if len(pair_matches_list) <= 0:messages = [{"role": "user","content": self.merged_str,3 unmodified linesfor match in pair_matches_list:inst = match.group("inst")answer = match.group("answer")messages.extend([{"role": "user", "content": inst.strip()},{"role": "assistant", "content": answer.strip()},])return messagesdef append_last_instruction_to_messages(self, inst_matches_list, pair_matches_list):if len(inst_matches_list) > len(pair_matches_list):self.messages.extend([4 unmodified lines])def split(self, merged_str) -> list:self.merged_str = merged_strself.messages = []if self.model in ["mixtral-8x7b", "mistral-7b"]:pair_pattern = (r"<s>\s*\[INST\](?P<inst>[\s\S]*?)\[/INST\](?P<answer>[\s\S]*?)</s>")pair_matches = re.finditer(pair_pattern, self.merged_str, re.MULTILINE)pair_matches_list = list(pair_matches)self.messages = self.convert_pair_matches_to_messages(pair_matches_list)inst_pattern = r"\[INST\](?P<inst>[\s\S]*?)\[/INST\]"inst_matches = re.finditer(inst_pattern, self.merged_str, re.MULTILINE)inst_matches_list = list(inst_matches)self.append_last_instruction_to_messages(inst_matches_list, pair_matches_list)elif self.model in ["openchat-3.5"]:pair_pattern = r"GPT4 Correct User:(?P<inst>[\s\S]*?)<\|end_of_turn\|>\s*GPT4 Correct Assistant:(?P<answer>[\s\S]*?)<\|end_of_turn\|>"# ignore casepair_matches = re.finditer(pair_pattern, self.merged_str, flags=re.MULTILINE | re.IGNORECASE)pair_matches_list = list(pair_matches)self.messages = self.convert_pair_matches_to_messages(pair_matches_list)inst_pattern = r"GPT4 Correct User:(?P<inst>[\s\S]*?)<\|end_of_turn\|>"inst_matches = re.finditer(inst_pattern, self.merged_str, flags=re.MULTILINE | re.IGNORECASE)inst_matches_list = list(inst_matches)self.append_last_instruction_to_messages(inst_matches_list, pair_matches_list)else:self.messages = [{"role": "user","content": self.merged_str,}]return self.messagesif __name__ == "__main__":composer = MessageComposer(model="openchat-3.5")messages = [{"role": "system",1 unmodified line},{"role": "user", "content": "Hello, who are you?"},{"role": "assistant", "content": "I am a bot."},{"role": "user", "content": "What is your name?"},# {"role": "assistant", "content": "My name is Bing."},# {"role": "user", "content": "Tell me a joke."},# {"role": "assistant", "content": "What is a robot's favorite type of music?"},# {1 unmodified line# "content": "How many questions have I asked? Please list them.",# },]print("model:", composer.model)merged_str = composer.merge(messages)print(merged_str)pprint(composer.split(merged_str))
messagers/message_composer.py+111-36