XThomasBU commited on
Commit
d4cb771
1 Parent(s): 6ad79fd

fix for conversation pair

Browse files
Files changed (1) hide show
  1. 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
- for message in memory_list:
141
- message_dict = message.to_dict() if hasattr(message, "to_dict") else message
142
-
143
- # Check if the type attribute is present as a key or attribute
144
- message_type = (
145
- message_dict.get("type", None)
146
- if isinstance(message_dict, dict)
147
- else getattr(message, "type", None)
 
 
 
 
 
 
148
  )
149
 
150
- # Check if content is present as a key or attribute
151
- message_content = (
152
- message_dict.get("content", None)
153
- if isinstance(message_dict, dict)
154
- else getattr(message, "content", None)
 
 
 
 
 
155
  )
156
 
157
- if message_type in ["ai", "ai_message"]:
158
- conversation_list.append({"type": "ai_message", "content": message_content})
159
- elif message_type in ["human", "user_message"]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  conversation_list.append(
161
- {"type": "user_message", "content": message_content}
162
  )
 
163
  else:
164
- raise ValueError("Invalid message type")
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