Skip to content

Commit

Permalink
Finish falkordb fact memory and retrievers
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanSallami committed Sep 16, 2024
1 parent 18ee99a commit 26dc1c0
Show file tree
Hide file tree
Showing 18 changed files with 746 additions and 357 deletions.
1 change: 0 additions & 1 deletion hybridagi/core/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class Fact(BaseModel):
subj: Entity = Field(description="Entity that is the subject of the fact", default=None)
rel: Relationship = Field(description="Relation between the subject and object entities", default=None)
obj: Entity = Field(description="Entity that is the object of the fact", default=None)
weight: float = Field(description="The fact weight (between 0.0 and 1.0, default 1.0)", default=1.0)
vector: Optional[List[float]] = Field(description="Vector representation of the fact", default=None)
metadata: Optional[Dict[str, Any]] = Field(description="Additional information about the fact", default={})

Expand Down
59 changes: 24 additions & 35 deletions hybridagi/memory/integration/falkordb/falkordb_document_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ def update(self, doc_or_docs: Union[Document, DocumentList]) -> None:
Raises:
ValueError: If the input is neither a Document nor a DocumentList.
Note:
- If a document with the given ID already exists, it will be updated.
- If a document with the given ID doesn't exist, a new one will be created.
- For documents with a parent_id, a PART_OF relationship is created or updated.
- Document metadata is stored as properties on the Document node.
"""
if not isinstance(doc_or_docs, (Document, DocumentList)):
raise ValueError("Invalid datatype provided must be Document or DocumentList")
Expand All @@ -81,29 +75,23 @@ def update(self, doc_or_docs: Union[Document, DocumentList]) -> None:
documents = doc_or_docs
for doc in documents.docs:
doc_id = str(doc.id)
if doc.vector is not None:
params = {
"id": doc_id,
"parent_id": str(doc.parent_id) if doc.parent_id else "",
"text": doc.text,
"vector": doc.vector,
"metadata": json.dumps(doc.metadata)
}
self._graph.query(
"MERGE (d:Document {id: $id}) SET d.text=$text, d.parent_id=$parent_id d.metadata=$metadata, d.vector:vecf32($vector)",
params = params,
)
else:
params = {
"id": doc_id,
"parent_id": str(doc.parent_id) if doc.parent_id else "",
"text": doc.text,
"metadata": json.dumps(doc.metadata)
}
self._graph.query(
"MERGE (d:Document {id: $id}) SET d.text=$text, d.parent_id=$parent_id, d.metadata=$metadata",
params = params,
)
params = {
"id": doc_id,
"parent_id": str(doc.parent_id) if doc.parent_id else None,
"text": doc.text,
"vector": list(doc.vector) if doc.vector is not None else None,
"metadata": json.dumps(doc.metadata)
}
self._graph.query(
" ".join([
"MERGE (d:Document {id: $id})"
"SET"
"d.text=$text",
"d.parent_id=$parent_id",
"d.metadata=$metadata",
"d.vector:vecf32($vector)"]),
params = params,
)
params = {
"id": doc_id,
}
Expand All @@ -118,7 +106,9 @@ def update(self, doc_or_docs: Union[Document, DocumentList]) -> None:
"parent": parent_id,
}
self._graph.query(
"MATCH (d:Document {id: $id}) MERGE (d)-[:PART_OF]->(:Document {id: $parent})",
" ".join([
"MATCH (d:Document {id: $id})",
"MERGE (d)-[:PART_OF]->(:Document {id: $parent})"]),
params = params,
)

Expand All @@ -139,11 +129,10 @@ def remove(self, id_or_ids: Union[UUID, str, List[Union[UUID, str]]]) -> None:
documents_ids = id_or_ids
for doc_id in documents_ids:
doc_id = str(doc_id)
if self.exist(doc_id):
self._graph.query(
"MATCH (n:Document {id: $id}) DETACH DELETE n",
params={"id": doc_id}
)
self._graph.query(
"MATCH (n:Document {id: $id}) DETACH DELETE n",
params={"id": doc_id}
)

def get(self, id_or_ids: Union[UUID, str, List[Union[UUID, str]]]) -> DocumentList:
"""
Expand Down
Loading

0 comments on commit 26dc1c0

Please sign in to comment.