Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FoliageOwO opened this issue Sep 11, 2024 · 5 comments · May be fixed by #1860
Open

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

FoliageOwO opened this issue Sep 11, 2024 · 5 comments · May be fixed by #1860
Assignees
Labels
bug Something isn't working

Comments

@FoliageOwO
Copy link
Contributor

FoliageOwO commented Sep 11, 2024

🐛 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 FoliageOwO changed the title Memory search with the prompt sometimes produces invalid JSON format JSONDecodeError: AI sometimes produces JSON with a code block format Sep 11, 2024
@prateekchhikara prateekchhikara self-assigned this Sep 11, 2024
@FoliageOwO
Copy link
Contributor Author

FoliageOwO commented Sep 12, 2024

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

mem0/mem0/memory/main.py

Lines 168 to 173 in f40a2e7

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"},
)
new_memories_with_actions = json.loads(new_memories_with_actions)

  ...
+ 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:

mem0/mem0/memory/main.py

Lines 142 to 145 in f40a2e7

response = self.llm.generate_response(
messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}],
response_format={"type": "json_object"},
)

@prateekchhikara
Copy link
Collaborator

@FoliageOwO can you please raise a PR for this?

@FoliageOwO FoliageOwO linked a pull request Sep 13, 2024 that will close this issue
12 tasks
@FoliageOwO
Copy link
Contributor Author

@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
Copy link

hhhhhge commented Sep 13, 2024

@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
Copy link
Contributor Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants