mem0ai / mem0

The Memory layer for your AI apps
https://mem0.ai
Apache License 2.0
22.41k stars 2.06k forks source link

JSONDecodeError: AI sometimes produces JSON with a code block format #1854

Open FoliageOwO opened 1 month ago

FoliageOwO commented 1 month ago

🐛 Describe the bug

I was creating a memory, following the official document.

But I did get confused at this error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

which is occurred at the line

new_memories_with_actions = json.loads(new_memories_with_actions)

After checking what on earth are the new_memories_with_actions, I found that the AI did respond a JSON content, but with triple backticks (the code block format), just like this (I'm using diff because the triple backticks will not show at the time):


+ ```json
+ {
+     "memory": [
+         {
+             "id": "(UUID)",
+             "text": "I'm visiting Paris",
+             "event": "ADD"
+         }
+     ]
+ }
+ ```

which surely will get json.loads() crashed.

Is anything wrong?

EDIT: BTW I think it was the model that caused the issue of generating bad content, so I need to declare that I was using Qwen (the model by Alibaba Cloud), not gpt-4 or gpt-4o or something that was mentioned in the official document.

FoliageOwO commented 1 month ago

I solved this problem by adding these lines in mem0/memory/main.py:

https://github.com/mem0ai/mem0/blob/f40a2e7603125be21953656efc9314e8029af4f6/mem0/memory/main.py#L168-L173

  ...
+ import re
  ...
          function_calling_prompt = get_update_memory_messages(retrieved_old_memory, new_retrieved_facts)
          new_memories_with_actions = self.llm.generate_response(
              messages=[{"role": "user", "content": function_calling_prompt}],
              response_format={"type": "json_object"},
          )

+         search_result = re.search("(```json)((.*\n)+)(```)", new_memories_with_actions)
+         if search_result:
+             new_memories_with_actions = search_result.group(2).strip()

          new_memories_with_actions = json.loads(new_memories_with_actions)

EDIT: The issue will not occur only at the json.loads() line I mentioned before, but also here:

https://github.com/mem0ai/mem0/blob/f40a2e7603125be21953656efc9314e8029af4f6/mem0/memory/main.py#L142-L145

prateekchhikara commented 1 month ago

@FoliageOwO can you please raise a PR for this?

FoliageOwO commented 1 month ago

@FoliageOwO can you please raise a PR for this?

@prateekchhikara Hi, I just created a new PR #1860, please review it when you get a second.

hhhhhge commented 1 month ago

@FoliageOwO can you please raise a PR for this?

@prateekchhikara Hi, I just created a new PR #1860, please review it when you get a second.

When will this bug be fixed o(╥﹏╥)o

FoliageOwO commented 1 month ago

@hhhhhge 等 PR 被合并并发布到新的 Release 版本后,所有人就都可以用上了。如果你目前要紧急解决这个问题的话,可以手动克隆我的 fork 仓库到 pip 安装的包的目录,也可以根据我的 commit 手动修改本地包源码。目前的 PR 并能代表最终 Release,我的方案可能不是最好的,也许可能会被其他贡献者改进,但是可以暂时用来应急。

For those who can't wait for the PR approval and releasing process: you can clone my fork repository to your pip packages folder, or manually edit local mem0 source codes according to my commit.