Skip to content

Commit 5b51bec

Browse files
committed
add: add json parse for en
1 parent 81c87ca commit 5b51bec

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/memos/mem_os/product.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from memos.mem_cube.general import GeneralMemCube
1717
from memos.mem_os.core import MOSCore
1818
from memos.mem_os.utils.format_utils import (
19+
clean_json_response,
1920
convert_graph_to_tree_forworkmem,
2021
ensure_unique_tree_ids,
2122
filter_nodes_by_tree_ids,
@@ -657,7 +658,7 @@ def get_suggestion_query(self, user_id: str, language: str = "zh") -> list[str]:
657658
you should generate some suggestion query, the query should be user what to query,
658659
user recently memories is:
659660
{memories}
660-
please generate 3 suggestion query in English,
661+
if the user recently memories is empty, please generate 3 suggestion query in English,
661662
output should be a json format, the key is "query", the value is a list of suggestion query.
662663
663664
example:
@@ -674,8 +675,8 @@ def get_suggestion_query(self, user_id: str, language: str = "zh") -> list[str]:
674675
memories = ""
675676
message_list = [{"role": "system", "content": suggestion_prompt.format(memories=memories)}]
676677
response = self.chat_llm.generate(message_list)
677-
response_json = json.loads(response)
678-
678+
clean_response = clean_json_response(response)
679+
response_json = json.loads(clean_response)
679680
return response_json["query"]
680681

681682
def chat(

src/memos/mem_os/utils/format_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,3 +1340,16 @@ def ensure_unique_tree_ids(tree_result: dict[str, Any]) -> dict[str, Any]:
13401340
print(f" - {error}")
13411341

13421342
return fixed_tree
1343+
1344+
1345+
def clean_json_response(response: str) -> str:
1346+
"""
1347+
Remove markdown JSON code block formatting from LLM response.
1348+
1349+
Args:
1350+
response: Raw response string that may contain ```json and ```
1351+
1352+
Returns:
1353+
str: Clean JSON string without markdown formatting
1354+
"""
1355+
return response.replace("```json", "").replace("```", "").strip()

0 commit comments

Comments
 (0)