Spaces:
Paused
Paused
:zap: [Enhance] MessageComposer: Handle messages without assistant
Browse files
conversations/message_composer.py
CHANGED
@@ -9,25 +9,43 @@ class MessageComposer:
|
|
9 |
def merge(self, messages, suffix=True) -> str:
|
10 |
self.messages = messages
|
11 |
self.merged_str = ""
|
|
|
12 |
self.system_prompt = ""
|
13 |
-
|
14 |
-
role = message["role"]
|
15 |
-
content = message["content"]
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
|
32 |
return self.merged_str
|
33 |
|
@@ -36,22 +54,32 @@ class MessageComposer:
|
|
36 |
self.merged_str = merged_str
|
37 |
pattern = r"`(?P<role>me|you)`:\n(?P<content>.*)\n+"
|
38 |
matches = re.finditer(pattern, self.merged_str, re.MULTILINE)
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
if role == "me":
|
43 |
-
role_str = "user"
|
44 |
-
elif role == "you":
|
45 |
-
role_str = "assistant"
|
46 |
-
else:
|
47 |
-
role_str = "unknown"
|
48 |
-
|
49 |
-
self.messages.append(
|
50 |
{
|
51 |
-
"role":
|
52 |
-
"content":
|
53 |
}
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
return self.messages
|
56 |
|
57 |
|
|
|
9 |
def merge(self, messages, suffix=True) -> str:
|
10 |
self.messages = messages
|
11 |
self.merged_str = ""
|
12 |
+
self.merged_str_list = []
|
13 |
self.system_prompt = ""
|
14 |
+
self.system_prompt_list = []
|
|
|
|
|
15 |
|
16 |
+
assistant_messages = [
|
17 |
+
message for message in messages if message["role"] not in ["user", "system"]
|
18 |
+
]
|
19 |
+
if len(assistant_messages) <= 0:
|
20 |
+
for message in messages:
|
21 |
+
role = message["role"]
|
22 |
+
content = message["content"]
|
23 |
+
if role.lower() in ["system"]:
|
24 |
+
self.system_prompt += content
|
25 |
+
if role.lower() in ["user"]:
|
26 |
+
self.merged_str_list.append(content)
|
27 |
+
else:
|
28 |
+
for message in messages:
|
29 |
+
role = message["role"]
|
30 |
+
content = message["content"]
|
31 |
|
32 |
+
if role.lower() in ["system"]:
|
33 |
+
self.system_prompt_list.append(content)
|
34 |
+
continue
|
35 |
+
if role.lower() in ["user"]:
|
36 |
+
role_str = "me"
|
37 |
+
elif role.lower() in ["assistant", "bot"]:
|
38 |
+
role_str = "you"
|
39 |
+
else:
|
40 |
+
role_str = "unknown"
|
41 |
+
# self.merged_str += f"`{role_str}`:\n{content}\n\n"
|
42 |
+
self.merged_str_list.append(f"`{role_str}`:\n{content}")
|
43 |
|
44 |
+
if suffix:
|
45 |
+
self.merged_str_list.append("`you`:")
|
46 |
+
|
47 |
+
self.merged_str = "\n\n".join(self.merged_str_list)
|
48 |
+
self.system_prompt = "\n\n".join(self.system_prompt_list)
|
49 |
|
50 |
return self.merged_str
|
51 |
|
|
|
54 |
self.merged_str = merged_str
|
55 |
pattern = r"`(?P<role>me|you)`:\n(?P<content>.*)\n+"
|
56 |
matches = re.finditer(pattern, self.merged_str, re.MULTILINE)
|
57 |
+
matches_list = list(matches)
|
58 |
+
if len(matches_list) <= 0:
|
59 |
+
self.messages = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
{
|
61 |
+
"role": "user",
|
62 |
+
"content": self.merged_str,
|
63 |
}
|
64 |
+
]
|
65 |
+
else:
|
66 |
+
for match in matches_list:
|
67 |
+
role = match.group("role")
|
68 |
+
|
69 |
+
if role == "me":
|
70 |
+
role_str = "user"
|
71 |
+
elif role == "you":
|
72 |
+
role_str = "assistant"
|
73 |
+
else:
|
74 |
+
role_str = "unknown"
|
75 |
+
|
76 |
+
self.messages.append(
|
77 |
+
{
|
78 |
+
"role": role_str,
|
79 |
+
"content": match.group("content"),
|
80 |
+
}
|
81 |
+
)
|
82 |
+
|
83 |
return self.messages
|
84 |
|
85 |
|