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

Fixed json.loads() decode error when LLM produces code block #1860

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mem0/memory/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mem0.memory.setup import setup_config
from mem0.memory.storage import SQLiteManager
from mem0.memory.telemetry import capture_event
from mem0.memory.utils import get_fact_retrieval_messages, parse_messages
from mem0.memory.utils import get_fact_retrieval_messages, parse_json_response, parse_messages
from mem0.utils.factory import LlmFactory, EmbedderFactory, VectorStoreFactory
from mem0.configs.base import MemoryItem, MemoryConfig

Expand Down Expand Up @@ -144,6 +144,8 @@ def _add_to_vector_store(self, messages, metadata, filters):
response_format={"type": "json_object"},
)

response = parse_json_response(response)

try:
new_retrieved_facts = json.loads(response)[
"facts"
Expand All @@ -170,6 +172,7 @@ def _add_to_vector_store(self, messages, metadata, filters):
messages=[{"role": "user", "content": function_calling_prompt}],
response_format={"type": "json_object"},
)
new_memories_with_actions = parse_json_response(new_memories_with_actions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we should still do "json.loads"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prateekchhikara Hi. Thanks for reviewing.

I think it may be the name of the function that confused you.

The function parse_json_response only extracts the JSON content from the potential code block from the given origin response string, it doesn't call json.loads(), which means it doesn't return a dictionary but a string.

So we HAVE to call json.loads() after the parsing progress.

new_memories_with_actions = json.loads(new_memories_with_actions)

returned_memories = []
Expand Down
8 changes: 8 additions & 0 deletions mem0/memory/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from mem0.configs.prompts import FACT_RETRIEVAL_PROMPT


Expand All @@ -14,3 +16,9 @@ def parse_messages(messages):
if msg["role"] == "assistant":
response += f"assistant: {msg['content']}\n"
return response

def parse_json_response(response):
search_result = re.search("(```json)((.*\n)+)(```)", response)
if search_result:
response = search_result.group(2).strip()
return response
Loading