Add: Json Parsing to solve Hallucination Errors (#3013)
This commit is contained in:
@@ -6,6 +6,7 @@ from typing import List, Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
from mem0.vector_stores.base import VectorStoreBase
|
||||
from mem0.memory.utils import extract_json
|
||||
|
||||
try:
|
||||
from azure.core.credentials import AzureKeyCredential
|
||||
@@ -233,7 +234,7 @@ class AzureAISearch(VectorStoreBase):
|
||||
|
||||
results = []
|
||||
for result in search_results:
|
||||
payload = json.loads(result["payload"])
|
||||
payload = json.loads(extract_json(result["payload"]))
|
||||
results.append(OutputData(id=result["id"], score=result["@search.score"], payload=payload))
|
||||
return results
|
||||
|
||||
@@ -288,7 +289,8 @@ class AzureAISearch(VectorStoreBase):
|
||||
result = self.search_client.get_document(key=vector_id)
|
||||
except ResourceNotFoundError:
|
||||
return None
|
||||
return OutputData(id=result["id"], score=None, payload=json.loads(result["payload"]))
|
||||
payload = json.loads(extract_json(result["payload"]))
|
||||
return OutputData(id=result["id"], score=None, payload=payload)
|
||||
|
||||
def list_cols(self) -> List[str]:
|
||||
"""
|
||||
@@ -335,7 +337,7 @@ class AzureAISearch(VectorStoreBase):
|
||||
search_results = self.search_client.search(search_text="*", filter=filter_expression, top=limit)
|
||||
results = []
|
||||
for result in search_results:
|
||||
payload = json.loads(result["payload"])
|
||||
payload = json.loads(extract_json(result["payload"]))
|
||||
results.append(OutputData(id=result["id"], score=result["@search.score"], payload=payload))
|
||||
return [results]
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ from redisvl.query import VectorQuery
|
||||
from redisvl.query.filter import Tag
|
||||
|
||||
from mem0.vector_stores.base import VectorStoreBase
|
||||
from mem0.memory.utils import extract_json
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -175,7 +176,7 @@ class RedisDB(VectorStoreBase):
|
||||
else {}
|
||||
),
|
||||
**{field: result[field] for field in ["agent_id", "run_id", "user_id"] if field in result},
|
||||
**{k: v for k, v in json.loads(result["metadata"]).items()},
|
||||
**{k: v for k, v in json.loads(extract_json(result["metadata"])).items()},
|
||||
},
|
||||
)
|
||||
for result in results
|
||||
@@ -219,7 +220,7 @@ class RedisDB(VectorStoreBase):
|
||||
else {}
|
||||
),
|
||||
**{field: result[field] for field in ["agent_id", "run_id", "user_id"] if field in result},
|
||||
**{k: v for k, v in json.loads(result["metadata"]).items()},
|
||||
**{k: v for k, v in json.loads(extract_json(result["metadata"])).items()},
|
||||
}
|
||||
|
||||
return MemoryResult(id=result["memory_id"], payload=payload)
|
||||
@@ -286,7 +287,7 @@ class RedisDB(VectorStoreBase):
|
||||
for field in ["agent_id", "run_id", "user_id"]
|
||||
if field in result.__dict__
|
||||
},
|
||||
**{k: v for k, v in json.loads(result["metadata"]).items()},
|
||||
**{k: v for k, v in json.loads(extract_json(result["metadata"])).items()},
|
||||
},
|
||||
)
|
||||
for result in results.docs
|
||||
|
||||
Reference in New Issue
Block a user