Spaces:
Build error
Build error
XThomasBU
commited on
Commit
·
d4cb771
1
Parent(s):
6ad79fd
fix for conversation pair
Browse files- modules/chat/helpers.py +46 -18
modules/chat/helpers.py
CHANGED
|
@@ -137,31 +137,59 @@ def get_history_chat_resume(steps, k, SYSTEM, LLM):
|
|
| 137 |
|
| 138 |
def get_history_setup_llm(memory_list):
|
| 139 |
conversation_list = []
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
-
# Check
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
if isinstance(
|
| 154 |
-
else getattr(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
)
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
conversation_list.append(
|
| 161 |
-
{"type": "
|
| 162 |
)
|
|
|
|
| 163 |
else:
|
| 164 |
-
|
| 165 |
|
| 166 |
return conversation_list
|
| 167 |
|
|
|
|
| 137 |
|
| 138 |
def get_history_setup_llm(memory_list):
|
| 139 |
conversation_list = []
|
| 140 |
+
i = 0
|
| 141 |
+
while i < len(memory_list) - 1:
|
| 142 |
+
# Process the current and next message
|
| 143 |
+
current_message = memory_list[i]
|
| 144 |
+
next_message = memory_list[i + 1]
|
| 145 |
+
|
| 146 |
+
# Convert messages to dictionary if necessary
|
| 147 |
+
current_message_dict = (
|
| 148 |
+
current_message.to_dict()
|
| 149 |
+
if hasattr(current_message, "to_dict")
|
| 150 |
+
else current_message
|
| 151 |
+
)
|
| 152 |
+
next_message_dict = (
|
| 153 |
+
next_message.to_dict() if hasattr(next_message, "to_dict") else next_message
|
| 154 |
)
|
| 155 |
|
| 156 |
+
# Check message type and content for current and next message
|
| 157 |
+
current_message_type = (
|
| 158 |
+
current_message_dict.get("type", None)
|
| 159 |
+
if isinstance(current_message_dict, dict)
|
| 160 |
+
else getattr(current_message, "type", None)
|
| 161 |
+
)
|
| 162 |
+
current_message_content = (
|
| 163 |
+
current_message_dict.get("content", None)
|
| 164 |
+
if isinstance(current_message_dict, dict)
|
| 165 |
+
else getattr(current_message, "content", None)
|
| 166 |
)
|
| 167 |
|
| 168 |
+
next_message_type = (
|
| 169 |
+
next_message_dict.get("type", None)
|
| 170 |
+
if isinstance(next_message_dict, dict)
|
| 171 |
+
else getattr(next_message, "type", None)
|
| 172 |
+
)
|
| 173 |
+
next_message_content = (
|
| 174 |
+
next_message_dict.get("content", None)
|
| 175 |
+
if isinstance(next_message_dict, dict)
|
| 176 |
+
else getattr(next_message, "content", None)
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Check if the current message is user message and the next one is AI message
|
| 180 |
+
if current_message_type in ["human", "user_message"] and next_message_type in [
|
| 181 |
+
"ai",
|
| 182 |
+
"ai_message",
|
| 183 |
+
]:
|
| 184 |
+
conversation_list.append(
|
| 185 |
+
{"type": "user_message", "content": current_message_content}
|
| 186 |
+
)
|
| 187 |
conversation_list.append(
|
| 188 |
+
{"type": "ai_message", "content": next_message_content}
|
| 189 |
)
|
| 190 |
+
i += 2 # Skip the next message since it has been paired
|
| 191 |
else:
|
| 192 |
+
i += 1 # Move to the next message if not a valid pair (example user message, followed by the cooldown system message)
|
| 193 |
|
| 194 |
return conversation_list
|
| 195 |
|