Spaces:
Sleeping
Sleeping
:gem: [Feature] New MessageComposer: Merge and split messages in mixtral-instruct format
Browse files- messagers/__init__.py +0 -0
- messagers/message_composer.py +128 -0
messagers/__init__.py
ADDED
|
File without changes
|
messagers/message_composer.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from pprint import pprint
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class MessageComposer:
|
| 6 |
+
"""
|
| 7 |
+
models:
|
| 8 |
+
- mixtral-8x7b (mistralai/Mixtral-8x7B-Instruct-v0.1)
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
def __init__(self, model: str = None):
|
| 12 |
+
self.inst_roles = ["user", "system", "inst"]
|
| 13 |
+
self.answer_roles = ["assistant", "bot", "answer"]
|
| 14 |
+
|
| 15 |
+
def concat_messages_by_role(self, messages):
|
| 16 |
+
def is_same_role(role1, role2):
|
| 17 |
+
if (
|
| 18 |
+
(role1 == role2)
|
| 19 |
+
or (role1 in self.inst_roles and role2 in self.inst_roles)
|
| 20 |
+
or (role1 in self.answer_roles and role2 in self.answer_roles)
|
| 21 |
+
):
|
| 22 |
+
return True
|
| 23 |
+
else:
|
| 24 |
+
return False
|
| 25 |
+
|
| 26 |
+
concat_messages = []
|
| 27 |
+
for message in messages:
|
| 28 |
+
role = message["role"]
|
| 29 |
+
content = message["content"]
|
| 30 |
+
if concat_messages and is_same_role(role, concat_messages[-1]["role"]):
|
| 31 |
+
concat_messages[-1]["content"] += "\n" + content
|
| 32 |
+
else:
|
| 33 |
+
if role in self.inst_roles:
|
| 34 |
+
message["role"] = "inst"
|
| 35 |
+
elif role in self.answer_roles:
|
| 36 |
+
message["role"] = "answer"
|
| 37 |
+
else:
|
| 38 |
+
message["role"] = "inst"
|
| 39 |
+
concat_messages.append(message)
|
| 40 |
+
return concat_messages
|
| 41 |
+
|
| 42 |
+
def merge(self, messages) -> str:
|
| 43 |
+
# <s> [INST] Instruction [/INST] Model answer </s> [INST] Follow-up instruction [/INST]
|
| 44 |
+
|
| 45 |
+
self.messages = self.concat_messages_by_role(messages)
|
| 46 |
+
self.merged_str = ""
|
| 47 |
+
self.cached_str = ""
|
| 48 |
+
for message in self.messages:
|
| 49 |
+
role = message["role"]
|
| 50 |
+
content = message["content"]
|
| 51 |
+
if role in self.inst_roles:
|
| 52 |
+
self.cached_str = f"[INST] {content} [/INST]"
|
| 53 |
+
elif role in self.answer_roles:
|
| 54 |
+
self.merged_str += f"<s> {self.cached_str} {content} </s>\n"
|
| 55 |
+
self.cached_str = ""
|
| 56 |
+
else:
|
| 57 |
+
self.cached_str = f"[INST] {content} [/INST]"
|
| 58 |
+
if self.cached_str:
|
| 59 |
+
self.merged_str += f"{self.cached_str}"
|
| 60 |
+
|
| 61 |
+
return self.merged_str
|
| 62 |
+
|
| 63 |
+
def split(self, merged_str) -> list:
|
| 64 |
+
self.messages = []
|
| 65 |
+
self.merged_str = merged_str
|
| 66 |
+
pair_pattern = (
|
| 67 |
+
r"<s>\s*\[INST\](?P<inst>[\s\S]*?)\[/INST\](?P<answer>[\s\S]*?)</s>"
|
| 68 |
+
)
|
| 69 |
+
pair_matches = re.finditer(pair_pattern, self.merged_str, re.MULTILINE)
|
| 70 |
+
pair_matches_list = list(pair_matches)
|
| 71 |
+
|
| 72 |
+
if len(pair_matches_list) <= 0:
|
| 73 |
+
self.messages = [
|
| 74 |
+
{
|
| 75 |
+
"role": "user",
|
| 76 |
+
"content": self.merged_str,
|
| 77 |
+
}
|
| 78 |
+
]
|
| 79 |
+
else:
|
| 80 |
+
for match in pair_matches_list:
|
| 81 |
+
inst = match.group("inst")
|
| 82 |
+
answer = match.group("answer")
|
| 83 |
+
self.messages.extend(
|
| 84 |
+
[
|
| 85 |
+
{"role": "user", "content": inst.strip()},
|
| 86 |
+
{"role": "assistant", "content": answer.strip()},
|
| 87 |
+
]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
inst_pattern = r"\[INST\](?P<inst>[\s\S]*?)\[/INST\]"
|
| 91 |
+
inst_matches = re.finditer(inst_pattern, self.merged_str, re.MULTILINE)
|
| 92 |
+
inst_matches_list = list(inst_matches)
|
| 93 |
+
|
| 94 |
+
if len(inst_matches_list) > len(pair_matches_list):
|
| 95 |
+
self.messages.extend(
|
| 96 |
+
[
|
| 97 |
+
{
|
| 98 |
+
"role": "user",
|
| 99 |
+
"content": inst_matches_list[-1].group("inst").strip(),
|
| 100 |
+
}
|
| 101 |
+
]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
return self.messages
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
composer = MessageComposer()
|
| 109 |
+
messages = [
|
| 110 |
+
{
|
| 111 |
+
"role": "system",
|
| 112 |
+
"content": "You are a LLM developed by OpenAI. Your name is GPT-4.",
|
| 113 |
+
},
|
| 114 |
+
{"role": "user", "content": "Hello, who are you?"},
|
| 115 |
+
{"role": "assistant", "content": "I am a bot."},
|
| 116 |
+
# {"role": "user", "content": "What is your name?"},
|
| 117 |
+
{"role": "assistant", "content": "My name is Bing."},
|
| 118 |
+
# {"role": "user", "content": "Tell me a joke."},
|
| 119 |
+
# {"role": "assistant", "content": "What is a robot's favorite type of music?"},
|
| 120 |
+
# {
|
| 121 |
+
# "role": "user",
|
| 122 |
+
# "content": "How many questions have I asked? Please list them.",
|
| 123 |
+
# },
|
| 124 |
+
]
|
| 125 |
+
merged_str = composer.merge(messages)
|
| 126 |
+
print(merged_str)
|
| 127 |
+
pprint(composer.split(merged_str))
|
| 128 |
+
# print(composer.merge(composer.split(merged_str)))
|