:gem: [Feature] New model enabled: nous-mixtral-8-7b
Browse files- apis/chat_api.py +7 -0
- messagers/message_composer.py +51 -8
- networks/message_streamer.py +3 -0
apis/chat_api.py
CHANGED
@@ -44,6 +44,13 @@ class ChatAPIApp:
|
|
44 |
"created": 1700000000,
|
45 |
"owned_by": "mistralai",
|
46 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
],
|
48 |
}
|
49 |
return self.available_models
|
|
|
44 |
"created": 1700000000,
|
45 |
"owned_by": "mistralai",
|
46 |
},
|
47 |
+
{
|
48 |
+
"id": "nous-mixtral-8x7b",
|
49 |
+
"description": "[NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO]: https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
|
50 |
+
"object": "model",
|
51 |
+
"created": 1700000000,
|
52 |
+
"owned_by": "NousResearch",
|
53 |
+
},
|
54 |
],
|
55 |
}
|
56 |
return self.available_models
|
messagers/message_composer.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import re
|
2 |
from pprint import pprint
|
|
|
3 |
|
4 |
|
5 |
class MessageComposer:
|
@@ -8,6 +9,7 @@ class MessageComposer:
|
|
8 |
"mixtral-8x7b",
|
9 |
"mistral-7b",
|
10 |
"openchat-3.5",
|
|
|
11 |
]
|
12 |
|
13 |
def __init__(self, model: str = None):
|
@@ -15,8 +17,10 @@ class MessageComposer:
|
|
15 |
self.model = model
|
16 |
else:
|
17 |
self.model = "mixtral-8x7b"
|
|
|
18 |
self.inst_roles = ["user", "system", "inst"]
|
19 |
self.answer_roles = ["assistant", "bot", "answer"]
|
|
|
20 |
|
21 |
def concat_messages_by_role(self, messages):
|
22 |
def is_same_role(role1, role2):
|
@@ -48,12 +52,22 @@ class MessageComposer:
|
|
48 |
def merge(self, messages) -> str:
|
49 |
# Mistral and Mixtral:
|
50 |
# <s> [INST] Instruction [/INST] Model answer </s> [INST] Follow-up instruction [/INST]
|
|
|
51 |
# OpenChat:
|
52 |
# 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:
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
self.merged_str = ""
|
56 |
|
|
|
57 |
if self.model in ["mixtral-8x7b", "mistral-7b"]:
|
58 |
self.cached_str = ""
|
59 |
for message in self.messages:
|
@@ -68,6 +82,19 @@ class MessageComposer:
|
|
68 |
self.cached_str = f"[INST] {content} [/INST]"
|
69 |
if self.cached_str:
|
70 |
self.merged_str += f"{self.cached_str}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
elif self.model in ["openchat-3.5"]:
|
72 |
self.merged_str_list = []
|
73 |
self.end_of_turn = "<|end_of_turn|>"
|
@@ -150,10 +177,21 @@ class MessageComposer:
|
|
150 |
self.append_last_instruction_to_messages(
|
151 |
inst_matches_list, pair_matches_list
|
152 |
)
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
elif self.model in ["openchat-3.5"]:
|
155 |
pair_pattern = r"GPT4 Correct User:(?P<inst>[\s\S]*?)<\|end_of_turn\|>\s*GPT4 Correct Assistant:(?P<answer>[\s\S]*?)<\|end_of_turn\|>"
|
156 |
-
# ignore case
|
157 |
pair_matches = re.finditer(
|
158 |
pair_pattern, self.merged_str, flags=re.MULTILINE | re.IGNORECASE
|
159 |
)
|
@@ -179,11 +217,13 @@ class MessageComposer:
|
|
179 |
|
180 |
|
181 |
if __name__ == "__main__":
|
182 |
-
|
|
|
|
|
183 |
messages = [
|
184 |
{
|
185 |
"role": "system",
|
186 |
-
"content": "You are a LLM developed by OpenAI
|
187 |
},
|
188 |
{"role": "user", "content": "Hello, who are you?"},
|
189 |
{"role": "assistant", "content": "I am a bot."},
|
@@ -196,8 +236,11 @@ if __name__ == "__main__":
|
|
196 |
# "content": "How many questions have I asked? Please list them.",
|
197 |
# },
|
198 |
]
|
199 |
-
|
200 |
merged_str = composer.merge(messages)
|
201 |
-
|
|
|
|
|
202 |
pprint(composer.split(merged_str))
|
203 |
-
#
|
|
|
|
1 |
import re
|
2 |
from pprint import pprint
|
3 |
+
from utils.logger import logger
|
4 |
|
5 |
|
6 |
class MessageComposer:
|
|
|
9 |
"mixtral-8x7b",
|
10 |
"mistral-7b",
|
11 |
"openchat-3.5",
|
12 |
+
"nous-mixtral-8x7b",
|
13 |
]
|
14 |
|
15 |
def __init__(self, model: str = None):
|
|
|
17 |
self.model = model
|
18 |
else:
|
19 |
self.model = "mixtral-8x7b"
|
20 |
+
self.system_roles = ["system"]
|
21 |
self.inst_roles = ["user", "system", "inst"]
|
22 |
self.answer_roles = ["assistant", "bot", "answer"]
|
23 |
+
self.default_role = "user"
|
24 |
|
25 |
def concat_messages_by_role(self, messages):
|
26 |
def is_same_role(role1, role2):
|
|
|
52 |
def merge(self, messages) -> str:
|
53 |
# Mistral and Mixtral:
|
54 |
# <s> [INST] Instruction [/INST] Model answer </s> [INST] Follow-up instruction [/INST]
|
55 |
+
|
56 |
# OpenChat:
|
57 |
# 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:
|
58 |
|
59 |
+
# Nous Mixtral:
|
60 |
+
# <|im_start|>system
|
61 |
+
# You are "Hermes 2".<|im_end|>
|
62 |
+
# <|im_start|>user
|
63 |
+
# Hello, who are you?<|im_end|>
|
64 |
+
# <|im_start|>assistant
|
65 |
+
|
66 |
+
# self.messages = self.concat_messages_by_role(messages)
|
67 |
+
self.messages = messages
|
68 |
self.merged_str = ""
|
69 |
|
70 |
+
# https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1#instruction-format
|
71 |
if self.model in ["mixtral-8x7b", "mistral-7b"]:
|
72 |
self.cached_str = ""
|
73 |
for message in self.messages:
|
|
|
82 |
self.cached_str = f"[INST] {content} [/INST]"
|
83 |
if self.cached_str:
|
84 |
self.merged_str += f"{self.cached_str}"
|
85 |
+
# https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO#prompt-format
|
86 |
+
elif self.model in ["nous-mixtral-8x7b"]:
|
87 |
+
self.merged_str_list = []
|
88 |
+
for message in self.messages:
|
89 |
+
role = message["role"]
|
90 |
+
content = message["content"]
|
91 |
+
if role not in ["system", "user", "assistant"]:
|
92 |
+
role = self.default_role
|
93 |
+
message_line = f"<|im_start|>{role}\n{content}<|im_end|>"
|
94 |
+
self.merged_str_list.append(message_line)
|
95 |
+
self.merged_str_list.append("<|im_start|>assistant")
|
96 |
+
self.merged_str = "\n".join(self.merged_str_list)
|
97 |
+
# https://huggingface.co/openchat/openchat-3.5-0106
|
98 |
elif self.model in ["openchat-3.5"]:
|
99 |
self.merged_str_list = []
|
100 |
self.end_of_turn = "<|end_of_turn|>"
|
|
|
177 |
self.append_last_instruction_to_messages(
|
178 |
inst_matches_list, pair_matches_list
|
179 |
)
|
180 |
+
elif self.model in ["nous-mixtral-8x7b"]:
|
181 |
+
# https://huggingface.co/NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO#prompt-format
|
182 |
+
# message_pattern = r"<\|im_start\|>(?P<role>system|user|assistant)[\s\n]*(?P<content>[\s\S]*?)<\|im_end\|>"
|
183 |
+
message_pattern = r"<\|im_start\|>(?P<role>system|user|assistant)[\s\n]*(?P<content>[\s\S]*?)<\|im_end\|>"
|
184 |
+
message_matches = re.finditer(
|
185 |
+
message_pattern, self.merged_str, flags=re.MULTILINE | re.IGNORECASE
|
186 |
+
)
|
187 |
+
message_matches_list = list(message_matches)
|
188 |
+
logger.note(f"message_matches_list: {message_matches_list}")
|
189 |
+
for match in message_matches_list:
|
190 |
+
role = match.group("role")
|
191 |
+
content = match.group("content")
|
192 |
+
self.messages.append({"role": role, "content": content.strip()})
|
193 |
elif self.model in ["openchat-3.5"]:
|
194 |
pair_pattern = r"GPT4 Correct User:(?P<inst>[\s\S]*?)<\|end_of_turn\|>\s*GPT4 Correct Assistant:(?P<answer>[\s\S]*?)<\|end_of_turn\|>"
|
|
|
195 |
pair_matches = re.finditer(
|
196 |
pair_pattern, self.merged_str, flags=re.MULTILINE | re.IGNORECASE
|
197 |
)
|
|
|
217 |
|
218 |
|
219 |
if __name__ == "__main__":
|
220 |
+
# model = "mixtral-8x7b"
|
221 |
+
model = "nous-mixtral-8x7b"
|
222 |
+
composer = MessageComposer(model)
|
223 |
messages = [
|
224 |
{
|
225 |
"role": "system",
|
226 |
+
"content": "You are a LLM developed by OpenAI.\nYour name is GPT-4.",
|
227 |
},
|
228 |
{"role": "user", "content": "Hello, who are you?"},
|
229 |
{"role": "assistant", "content": "I am a bot."},
|
|
|
236 |
# "content": "How many questions have I asked? Please list them.",
|
237 |
# },
|
238 |
]
|
239 |
+
logger.note(f"model: {composer.model}")
|
240 |
merged_str = composer.merge(messages)
|
241 |
+
logger.note("merged_str:")
|
242 |
+
logger.mesg(merged_str)
|
243 |
+
logger.note("splitted messages:")
|
244 |
pprint(composer.split(merged_str))
|
245 |
+
# logger.note("merged merged_str:")
|
246 |
+
# logger.mesg(composer.merge(composer.split(merged_str)))
|
networks/message_streamer.py
CHANGED
@@ -11,6 +11,7 @@ class MessageStreamer:
|
|
11 |
MODEL_MAP = {
|
12 |
"mixtral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1", # 72.62, fast [Recommended]
|
13 |
"mistral-7b": "mistralai/Mistral-7B-Instruct-v0.2", # 65.71, fast
|
|
|
14 |
# "openchat-3.5": "openchat/openchat-3.5-1210", # 68.89, fast
|
15 |
# "zephyr-7b-beta": "HuggingFaceH4/zephyr-7b-beta", # ❌ Too Slow
|
16 |
# "llama-70b": "meta-llama/Llama-2-70b-chat-hf", # ❌ Require Pro User
|
@@ -21,11 +22,13 @@ class MessageStreamer:
|
|
21 |
STOP_SEQUENCES_MAP = {
|
22 |
"mixtral-8x7b": "</s>",
|
23 |
"mistral-7b": "</s>",
|
|
|
24 |
"openchat-3.5": "<|end_of_turn|>",
|
25 |
}
|
26 |
TOKEN_LIMIT_MAP = {
|
27 |
"mixtral-8x7b": 32768,
|
28 |
"mistral-7b": 32768,
|
|
|
29 |
"openchat-3.5": 8192,
|
30 |
}
|
31 |
TOKEN_RESERVED = 100
|
|
|
11 |
MODEL_MAP = {
|
12 |
"mixtral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1", # 72.62, fast [Recommended]
|
13 |
"mistral-7b": "mistralai/Mistral-7B-Instruct-v0.2", # 65.71, fast
|
14 |
+
"nous-mixtral-8x7b": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
|
15 |
# "openchat-3.5": "openchat/openchat-3.5-1210", # 68.89, fast
|
16 |
# "zephyr-7b-beta": "HuggingFaceH4/zephyr-7b-beta", # ❌ Too Slow
|
17 |
# "llama-70b": "meta-llama/Llama-2-70b-chat-hf", # ❌ Require Pro User
|
|
|
22 |
STOP_SEQUENCES_MAP = {
|
23 |
"mixtral-8x7b": "</s>",
|
24 |
"mistral-7b": "</s>",
|
25 |
+
"nous-mixtral-8x7b": "<|im_end|>",
|
26 |
"openchat-3.5": "<|end_of_turn|>",
|
27 |
}
|
28 |
TOKEN_LIMIT_MAP = {
|
29 |
"mixtral-8x7b": 32768,
|
30 |
"mistral-7b": 32768,
|
31 |
+
"nous-mixtral-8x7b": 32768,
|
32 |
"openchat-3.5": 8192,
|
33 |
}
|
34 |
TOKEN_RESERVED = 100
|