Hansimov commited on
Commit
0b6e0c9
1 Parent(s): e4d11b8

:gem: [Feature] MessageCompoer: decompose to system and input prompt

Browse files
Files changed (1) hide show
  1. messagers/message_composer.py +43 -4
messagers/message_composer.py CHANGED
@@ -163,6 +163,37 @@ class MessageComposer:
163
 
164
  return self.merged_str
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
  if __name__ == "__main__":
168
  # model = "mixtral-8x7b"
@@ -187,9 +218,17 @@ if __name__ == "__main__":
187
  # "content": "How many questions have I asked? Please list them.",
188
  # },
189
  ]
190
- logger.note(f"model: {composer.model}")
191
- merged_str = composer.merge(messages)
192
- logger.note("merged_str:")
193
- logger.mesg(merged_str)
 
 
 
 
 
 
 
 
194
 
195
  # python -m messagers.message_composer
 
163
 
164
  return self.merged_str
165
 
166
+ def decompose_to_system_and_input_prompt(
167
+ self, messages: list[dict], append_assistant=True
168
+ ):
169
+ system_prompt_list = []
170
+ user_and_assistant_messages = []
171
+ for message in messages:
172
+ role = message["role"]
173
+ content = message["content"]
174
+ if role in self.system_roles:
175
+ system_prompt_list.append(content)
176
+ else:
177
+ user_and_assistant_messages.append(message)
178
+ system_prompt = "\n".join(system_prompt_list)
179
+
180
+ input_prompt_list = []
181
+ input_messages = self.concat_messages_by_role(user_and_assistant_messages)
182
+ for message in input_messages:
183
+ role = message["role"]
184
+ content = message["content"]
185
+ if role in self.answer_roles:
186
+ role_content_str = f"`assistant`:\n{content}"
187
+ else:
188
+ role_content_str = f"`user`:\n{content}"
189
+ input_prompt_list.append(role_content_str)
190
+ input_prompt = "\n\n".join(input_prompt_list)
191
+
192
+ if append_assistant:
193
+ input_prompt += "\n\n`assistant`:"
194
+
195
+ return system_prompt, input_prompt
196
+
197
 
198
  if __name__ == "__main__":
199
  # model = "mixtral-8x7b"
 
218
  # "content": "How many questions have I asked? Please list them.",
219
  # },
220
  ]
221
+ # logger.note(f"model: {composer.model}")
222
+ # merged_str = composer.merge(messages)
223
+ # logger.note("merged_str:")
224
+ # logger.mesg(merged_str)
225
+
226
+ system_prompt, input_prompt = composer.decompose_to_system_and_input_prompt(
227
+ messages
228
+ )
229
+ logger.note("system_prompt:")
230
+ logger.mesg(system_prompt)
231
+ logger.note("input_prompt:")
232
+ logger.mesg(input_prompt)
233
 
234
  # python -m messagers.message_composer