Skip to content

Commit add4237

Browse files
committed
feat: add stream for further question
1 parent 9ee4639 commit add4237

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/memos/mem_os/product.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -641,13 +641,17 @@ def user_register(
641641

642642
def _get_further_suggestion(self, message: MessageList | None = None) -> list[str]:
643643
"""Get further suggestion prompt."""
644-
dialogue_info = "\n".join([f"{msg['role']}: {msg['content']}" for msg in message[-2:]])
645-
further_suggestion_prompt = FURTHER_SUGGESTION_PROMPT.format(dialogue=dialogue_info)
646-
message_list = [{"role": "system", "content": further_suggestion_prompt}]
647-
response = self.chat_llm.generate(message_list)
648-
clean_response = clean_json_response(response)
649-
response_json = json.loads(clean_response)
650-
return response_json["query"]
644+
try:
645+
dialogue_info = "\n".join([f"{msg['role']}: {msg['content']}" for msg in message[-2:]])
646+
further_suggestion_prompt = FURTHER_SUGGESTION_PROMPT.format(dialogue=dialogue_info)
647+
message_list = [{"role": "system", "content": further_suggestion_prompt}]
648+
response = self.chat_llm.generate(message_list)
649+
clean_response = clean_json_response(response)
650+
response_json = json.loads(clean_response)
651+
return response_json["query"]
652+
except Exception as e:
653+
logger.error(f"Error getting further suggestion: {e}", exc_info=True)
654+
return []
651655

652656
def get_suggestion_query(
653657
self, user_id: str, language: str = "zh", message: MessageList | None = None
@@ -830,6 +834,11 @@ def chat_with_references(
830834
total_time = round(float(time_end - time_start), 1)
831835

832836
yield f"data: {json.dumps({'type': 'time', 'data': {'total_time': total_time, 'speed_improvement': f'{speed_improvement}%'}})}\n\n"
837+
# get further suggestion
838+
current_messages.append({"role": "assistant", "content": full_response})
839+
further_suggestion = self._get_further_suggestion(current_messages)
840+
logger.info(f"further_suggestion: {further_suggestion}")
841+
yield f"data: {json.dumps({'type': 'suggestion', 'data': further_suggestion})}\n\n"
833842
yield f"data: {json.dumps({'type': 'end'})}\n\n"
834843

835844
logger.info(f"user_id: {user_id}, cube_id: {cube_id}, current_messages: {current_messages}")

0 commit comments

Comments
 (0)