Skip to content

Commit 2bb0653

Browse files
authored
Add: Json Parsing to solve Hallucination Errors (mem0ai#3013)
1 parent eb24b92 commit 2bb0653

File tree

15 files changed

+44
-17
lines changed

15 files changed

+44
-17
lines changed

evaluation/metrics/llm_judge.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import numpy as np
66
from openai import OpenAI
7+
from mem0.memory.utils import extract_json
78

89
client = OpenAI()
910

@@ -22,7 +23,7 @@
2223
2324
For time related questions, the gold answer will be a specific date, month, year, etc. The generated answer might be much longer or use relative time references (like "last Tuesday" or "next month"), but you should be generous with your grading - as long as it refers to the same date or time period as the gold answer, it should be counted as CORRECT. Even if the format differs (e.g., "May 7th" vs "7 May"), consider it CORRECT if it's the same date.
2425
25-
Now its time for the real question:
26+
Now it's time for the real question:
2627
Question: {question}
2728
Gold answer: {gold_answer}
2829
Generated answer: {generated_answer}
@@ -49,7 +50,7 @@ def evaluate_llm_judge(question, gold_answer, generated_answer):
4950
response_format={"type": "json_object"},
5051
temperature=0.0,
5152
)
52-
label = json.loads(response.choices[0].message.content)["label"]
53+
label = json.loads(extract_json(response.choices[0].message.content))["label"]
5354
return 1 if label == "CORRECT" else 0
5455

5556

mem0/embeddings/aws_bedrock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from mem0.configs.embeddings.base import BaseEmbedderConfig
1313
from mem0.embeddings.base import EmbeddingBase
14+
from mem0.memory.utils import extract_json
1415

1516

1617
class AWSBedrockEmbedding(EmbeddingBase):
@@ -74,7 +75,7 @@ def _get_embedding(self, text):
7475
contentType="application/json",
7576
)
7677

77-
response_body = json.loads(response.get("body").read())
78+
response_body = json.loads(extract_json(response.get("body").read()))
7879

7980
if provider == "cohere":
8081
embeddings = response_body.get("embeddings")[0]

mem0/llms/aws_bedrock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from mem0.configs.llms.base import BaseLlmConfig
1212
from mem0.llms.base import LLMBase
13+
from mem0.memory.utils import extract_json
1314

1415
PROVIDERS = ["ai21", "amazon", "anthropic", "cohere", "meta", "mistral", "stability", "writer"]
1516

@@ -101,7 +102,7 @@ def _parse_response(self, response, tools) -> str:
101102
return processed_response
102103

103104
response_body = response.get("body").read().decode()
104-
response_json = json.loads(response_body)
105+
response_json = json.loads(extract_json(response_body))
105106
return response_json.get("content", [{"text": ""}])[0].get("text", "")
106107

107108
def _prepare_input(

mem0/llms/azure_openai.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from mem0.configs.llms.base import BaseLlmConfig
88
from mem0.llms.base import LLMBase
9+
from mem0.memory.utils import extract_json
910

1011

1112
class AzureOpenAILLM(LLMBase):
@@ -53,7 +54,7 @@ def _parse_response(self, response, tools):
5354
processed_response["tool_calls"].append(
5455
{
5556
"name": tool_call.function.name,
56-
"arguments": json.loads(tool_call.function.arguments),
57+
"arguments": json.loads(extract_json(tool_call.function.arguments)),
5758
}
5859
)
5960

mem0/llms/deepseek.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from mem0.configs.llms.base import BaseLlmConfig
88
from mem0.llms.base import LLMBase
9+
from mem0.memory.utils import extract_json
910

1011

1112
class DeepSeekLLM(LLMBase):
@@ -41,7 +42,7 @@ def _parse_response(self, response, tools):
4142
processed_response["tool_calls"].append(
4243
{
4344
"name": tool_call.function.name,
44-
"arguments": json.loads(tool_call.function.arguments),
45+
"arguments": json.loads(extract_json(tool_call.function.arguments)),
4546
}
4647
)
4748

mem0/llms/groq.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from mem0.configs.llms.base import BaseLlmConfig
1111
from mem0.llms.base import LLMBase
12+
from mem0.memory.utils import extract_json
1213

1314

1415
class GroqLLM(LLMBase):
@@ -43,7 +44,7 @@ def _parse_response(self, response, tools):
4344
processed_response["tool_calls"].append(
4445
{
4546
"name": tool_call.function.name,
46-
"arguments": json.loads(tool_call.function.arguments),
47+
"arguments": json.loads(extract_json(tool_call.function.arguments)),
4748
}
4849
)
4950

mem0/llms/litellm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from mem0.configs.llms.base import BaseLlmConfig
1010
from mem0.llms.base import LLMBase
11+
from mem0.memory.utils import extract_json
1112

1213

1314
class LiteLLM(LLMBase):
@@ -39,7 +40,7 @@ def _parse_response(self, response, tools):
3940
processed_response["tool_calls"].append(
4041
{
4142
"name": tool_call.function.name,
42-
"arguments": json.loads(tool_call.function.arguments),
43+
"arguments": json.loads(extract_json(tool_call.function.arguments)),
4344
}
4445
)
4546

mem0/llms/openai.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from mem0.configs.llms.base import BaseLlmConfig
99
from mem0.llms.base import LLMBase
10+
from mem0.memory.utils import extract_json
1011

1112

1213
class OpenAILLM(LLMBase):
@@ -62,7 +63,7 @@ def _parse_response(self, response, tools):
6263
processed_response["tool_calls"].append(
6364
{
6465
"name": tool_call.function.name,
65-
"arguments": json.loads(tool_call.function.arguments),
66+
"arguments": json.loads(extract_json(tool_call.function.arguments)),
6667
}
6768
)
6869

mem0/llms/together.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from mem0.configs.llms.base import BaseLlmConfig
1111
from mem0.llms.base import LLMBase
12+
from mem0.memory.utils import extract_json
1213

1314

1415
class TogetherLLM(LLMBase):
@@ -43,7 +44,7 @@ def _parse_response(self, response, tools):
4344
processed_response["tool_calls"].append(
4445
{
4546
"name": tool_call.function.name,
46-
"arguments": json.loads(tool_call.function.arguments),
47+
"arguments": json.loads(extract_json(tool_call.function.arguments)),
4748
}
4849
)
4950

mem0/llms/utils/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)