2024-08-18 13:22:31 -04:00
|
|
|
import asyncio
|
|
|
|
|
import logging
|
2024-08-26 10:30:22 -04:00
|
|
|
import re
|
2024-08-22 14:26:26 -04:00
|
|
|
from collections import defaultdict
|
2024-08-21 12:03:32 -04:00
|
|
|
from time import time
|
2024-09-04 10:05:45 -04:00
|
|
|
from typing import Any
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-09-04 10:05:45 -04:00
|
|
|
from neo4j import AsyncDriver, Query
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-25 10:07:50 -07:00
|
|
|
from graphiti_core.edges import EntityEdge
|
2024-08-27 16:18:01 -04:00
|
|
|
from graphiti_core.helpers import parse_db_date
|
2024-08-25 10:07:50 -07:00
|
|
|
from graphiti_core.nodes import EntityNode, EpisodicNode
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2024-08-21 12:03:32 -04:00
|
|
|
RELEVANT_SCHEMA_LIMIT = 3
|
|
|
|
|
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-22 14:26:26 -04:00
|
|
|
async def get_mentioned_nodes(driver: AsyncDriver, episodes: list[EpisodicNode]):
|
2024-08-23 14:18:45 -04:00
|
|
|
episode_uuids = [episode.uuid for episode in episodes]
|
|
|
|
|
records, _, _ = await driver.execute_query(
|
|
|
|
|
"""
|
2024-08-22 14:26:26 -04:00
|
|
|
MATCH (episode:Episodic)-[:MENTIONS]->(n:Entity) WHERE episode.uuid IN $uuids
|
|
|
|
|
RETURN DISTINCT
|
|
|
|
|
n.uuid As uuid,
|
2024-09-05 14:09:19 -04:00
|
|
|
n.name AS name,
|
|
|
|
|
n.name_embedding AS name_embedding
|
2024-08-22 14:26:26 -04:00
|
|
|
n.created_at AS created_at,
|
|
|
|
|
n.summary AS summary
|
|
|
|
|
""",
|
2024-08-23 14:18:45 -04:00
|
|
|
uuids=episode_uuids,
|
|
|
|
|
)
|
2024-08-22 14:26:26 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
nodes: list[EntityNode] = []
|
2024-08-22 14:26:26 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
for record in records:
|
|
|
|
|
nodes.append(
|
|
|
|
|
EntityNode(
|
|
|
|
|
uuid=record['uuid'],
|
|
|
|
|
name=record['name'],
|
2024-09-05 14:09:19 -04:00
|
|
|
name_embedding=record['name_embedding'],
|
2024-08-23 14:18:45 -04:00
|
|
|
labels=['Entity'],
|
|
|
|
|
created_at=record['created_at'].to_native(),
|
|
|
|
|
summary=record['summary'],
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-08-22 14:26:26 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return nodes
|
2024-08-22 14:26:26 -04:00
|
|
|
|
|
|
|
|
|
2024-08-18 13:22:31 -04:00
|
|
|
async def bfs(node_ids: list[str], driver: AsyncDriver):
|
2024-08-23 14:18:45 -04:00
|
|
|
records, _, _ = await driver.execute_query(
|
|
|
|
|
"""
|
2024-08-18 13:22:31 -04:00
|
|
|
MATCH (n WHERE n.uuid in $node_ids)-[r]->(m)
|
2024-08-22 14:26:26 -04:00
|
|
|
RETURN DISTINCT
|
2024-08-18 13:22:31 -04:00
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
n.name AS source_name,
|
|
|
|
|
n.summary AS source_summary,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
m.name AS target_name,
|
|
|
|
|
m.summary AS target_summary,
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
2024-09-04 10:05:45 -04:00
|
|
|
|
2024-08-18 13:22:31 -04:00
|
|
|
""",
|
2024-08-23 14:18:45 -04:00
|
|
|
node_ids=node_ids,
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-04 10:05:45 -04:00
|
|
|
context: dict[str, Any] = {}
|
2024-08-23 14:18:45 -04:00
|
|
|
|
|
|
|
|
for record in records:
|
|
|
|
|
n_uuid = record['source_node_uuid']
|
|
|
|
|
if n_uuid in context:
|
|
|
|
|
context[n_uuid]['facts'].append(record['fact'])
|
|
|
|
|
else:
|
|
|
|
|
context[n_uuid] = {
|
|
|
|
|
'name': record['source_name'],
|
|
|
|
|
'summary': record['source_summary'],
|
|
|
|
|
'facts': [record['fact']],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_uuid = record['target_node_uuid']
|
|
|
|
|
if m_uuid not in context:
|
|
|
|
|
context[m_uuid] = {
|
|
|
|
|
'name': record['target_name'],
|
|
|
|
|
'summary': record['target_summary'],
|
|
|
|
|
'facts': [],
|
|
|
|
|
}
|
|
|
|
|
logger.info(f'bfs search returned context: {context}')
|
|
|
|
|
return context
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def edge_similarity_search(
|
2024-09-05 12:05:44 -04:00
|
|
|
driver: AsyncDriver,
|
|
|
|
|
search_vector: list[float],
|
|
|
|
|
source_node_uuid: str | None,
|
|
|
|
|
target_node_uuid: str | None,
|
|
|
|
|
limit: int = RELEVANT_SCHEMA_LIMIT,
|
2024-08-18 13:22:31 -04:00
|
|
|
) -> list[EntityEdge]:
|
2024-08-23 14:18:45 -04:00
|
|
|
# vector similarity search over embedded facts
|
2024-09-04 10:05:45 -04:00
|
|
|
query = Query("""
|
2024-08-27 16:18:01 -04:00
|
|
|
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
|
2024-09-03 13:25:52 -04:00
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
|
2024-08-18 13:22:31 -04:00
|
|
|
RETURN
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
2024-08-27 16:18:01 -04:00
|
|
|
ORDER BY score DESC
|
2024-09-04 10:05:45 -04:00
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
if source_node_uuid is None and target_node_uuid is None:
|
|
|
|
|
query = Query("""
|
|
|
|
|
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
|
|
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity)
|
|
|
|
|
RETURN
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
|
|
|
|
ORDER BY score DESC
|
|
|
|
|
""")
|
|
|
|
|
elif source_node_uuid is None:
|
|
|
|
|
query = Query("""
|
|
|
|
|
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
|
|
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
|
|
|
|
|
RETURN
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
|
|
|
|
ORDER BY score DESC
|
|
|
|
|
""")
|
|
|
|
|
elif target_node_uuid is None:
|
|
|
|
|
query = Query("""
|
|
|
|
|
CALL db.index.vector.queryRelationships("fact_embedding", $limit, $search_vector)
|
|
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity)
|
|
|
|
|
RETURN
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
|
|
|
|
ORDER BY score DESC
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
records, _, _ = await driver.execute_query(
|
|
|
|
|
query,
|
2024-08-23 14:18:45 -04:00
|
|
|
search_vector=search_vector,
|
2024-09-03 13:25:52 -04:00
|
|
|
source_uuid=source_node_uuid,
|
|
|
|
|
target_uuid=target_node_uuid,
|
2024-08-23 14:18:45 -04:00
|
|
|
limit=limit,
|
|
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
edges: list[EntityEdge] = []
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
for record in records:
|
|
|
|
|
edge = EntityEdge(
|
|
|
|
|
uuid=record['uuid'],
|
|
|
|
|
source_node_uuid=record['source_node_uuid'],
|
|
|
|
|
target_node_uuid=record['target_node_uuid'],
|
|
|
|
|
fact=record['fact'],
|
|
|
|
|
name=record['name'],
|
|
|
|
|
episodes=record['episodes'],
|
|
|
|
|
fact_embedding=record['fact_embedding'],
|
|
|
|
|
created_at=record['created_at'].to_native(),
|
|
|
|
|
expired_at=parse_db_date(record['expired_at']),
|
|
|
|
|
valid_at=parse_db_date(record['valid_at']),
|
|
|
|
|
invalid_at=parse_db_date(record['invalid_at']),
|
|
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
edges.append(edge)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return edges
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def entity_similarity_search(
|
2024-09-05 12:05:44 -04:00
|
|
|
search_vector: list[float], driver: AsyncDriver, limit=RELEVANT_SCHEMA_LIMIT
|
2024-08-18 13:22:31 -04:00
|
|
|
) -> list[EntityNode]:
|
2024-08-23 14:18:45 -04:00
|
|
|
# vector similarity search over entity names
|
|
|
|
|
records, _, _ = await driver.execute_query(
|
|
|
|
|
"""
|
2024-08-21 12:03:32 -04:00
|
|
|
CALL db.index.vector.queryNodes("name_embedding", $limit, $search_vector)
|
2024-08-18 13:22:31 -04:00
|
|
|
YIELD node AS n, score
|
|
|
|
|
RETURN
|
|
|
|
|
n.uuid As uuid,
|
|
|
|
|
n.name AS name,
|
2024-09-05 14:09:19 -04:00
|
|
|
n.name_embedding AS name_embedding,
|
2024-08-18 13:22:31 -04:00
|
|
|
n.created_at AS created_at,
|
|
|
|
|
n.summary AS summary
|
|
|
|
|
ORDER BY score DESC
|
|
|
|
|
""",
|
2024-08-23 14:18:45 -04:00
|
|
|
search_vector=search_vector,
|
|
|
|
|
limit=limit,
|
|
|
|
|
)
|
|
|
|
|
nodes: list[EntityNode] = []
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
for record in records:
|
|
|
|
|
nodes.append(
|
|
|
|
|
EntityNode(
|
|
|
|
|
uuid=record['uuid'],
|
|
|
|
|
name=record['name'],
|
2024-09-04 10:05:45 -04:00
|
|
|
name_embedding=record['name_embedding'],
|
2024-08-23 14:18:45 -04:00
|
|
|
labels=['Entity'],
|
|
|
|
|
created_at=record['created_at'].to_native(),
|
|
|
|
|
summary=record['summary'],
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return nodes
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
|
2024-08-21 12:03:32 -04:00
|
|
|
async def entity_fulltext_search(
|
2024-09-05 12:05:44 -04:00
|
|
|
query: str, driver: AsyncDriver, limit=RELEVANT_SCHEMA_LIMIT
|
2024-08-21 12:03:32 -04:00
|
|
|
) -> list[EntityNode]:
|
2024-08-23 14:18:45 -04:00
|
|
|
# BM25 search to get top nodes
|
2024-08-26 10:30:22 -04:00
|
|
|
fuzzy_query = re.sub(r'[^\w\s]', '', query) + '~'
|
2024-08-23 14:18:45 -04:00
|
|
|
records, _, _ = await driver.execute_query(
|
|
|
|
|
"""
|
2024-08-18 13:22:31 -04:00
|
|
|
CALL db.index.fulltext.queryNodes("name_and_summary", $query) YIELD node, score
|
2024-08-22 14:26:26 -04:00
|
|
|
RETURN
|
2024-09-04 10:05:45 -04:00
|
|
|
node.uuid AS uuid,
|
2024-08-18 13:22:31 -04:00
|
|
|
node.name AS name,
|
2024-09-05 14:09:19 -04:00
|
|
|
node.name_embedding AS name_embedding,
|
2024-08-18 13:22:31 -04:00
|
|
|
node.created_at AS created_at,
|
|
|
|
|
node.summary AS summary
|
|
|
|
|
ORDER BY score DESC
|
2024-08-21 12:03:32 -04:00
|
|
|
LIMIT $limit
|
2024-08-18 13:22:31 -04:00
|
|
|
""",
|
2024-08-23 14:18:45 -04:00
|
|
|
query=fuzzy_query,
|
|
|
|
|
limit=limit,
|
|
|
|
|
)
|
|
|
|
|
nodes: list[EntityNode] = []
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
for record in records:
|
|
|
|
|
nodes.append(
|
|
|
|
|
EntityNode(
|
|
|
|
|
uuid=record['uuid'],
|
|
|
|
|
name=record['name'],
|
2024-09-04 10:05:45 -04:00
|
|
|
name_embedding=record['name_embedding'],
|
2024-08-23 14:18:45 -04:00
|
|
|
labels=['Entity'],
|
|
|
|
|
created_at=record['created_at'].to_native(),
|
|
|
|
|
summary=record['summary'],
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return nodes
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
|
2024-08-21 12:03:32 -04:00
|
|
|
async def edge_fulltext_search(
|
2024-09-05 12:05:44 -04:00
|
|
|
driver: AsyncDriver,
|
|
|
|
|
query: str,
|
|
|
|
|
source_node_uuid: str | None,
|
|
|
|
|
target_node_uuid: str | None,
|
|
|
|
|
limit=RELEVANT_SCHEMA_LIMIT,
|
2024-08-21 12:03:32 -04:00
|
|
|
) -> list[EntityEdge]:
|
2024-08-23 14:18:45 -04:00
|
|
|
# fulltext search over facts
|
2024-09-04 10:05:45 -04:00
|
|
|
cypher_query = Query("""
|
|
|
|
|
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
|
|
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
|
|
|
|
|
RETURN
|
2024-08-18 13:22:31 -04:00
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
2024-08-21 12:03:32 -04:00
|
|
|
ORDER BY score DESC LIMIT $limit
|
2024-09-04 10:05:45 -04:00
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
if source_node_uuid is None and target_node_uuid is None:
|
|
|
|
|
cypher_query = Query("""
|
|
|
|
|
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
|
|
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity)
|
|
|
|
|
RETURN
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
|
|
|
|
ORDER BY score DESC LIMIT $limit
|
|
|
|
|
""")
|
|
|
|
|
elif source_node_uuid is None:
|
|
|
|
|
cypher_query = Query("""
|
|
|
|
|
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
|
|
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity)-[r {uuid: rel.uuid}]-(m:Entity {uuid: $target_uuid})
|
|
|
|
|
RETURN
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
|
|
|
|
ORDER BY score DESC LIMIT $limit
|
|
|
|
|
""")
|
|
|
|
|
elif target_node_uuid is None:
|
|
|
|
|
cypher_query = Query("""
|
|
|
|
|
CALL db.index.fulltext.queryRelationships("name_and_fact", $query)
|
|
|
|
|
YIELD relationship AS rel, score
|
|
|
|
|
MATCH (n:Entity {uuid: $source_uuid})-[r {uuid: rel.uuid}]-(m:Entity)
|
|
|
|
|
RETURN
|
|
|
|
|
r.uuid AS uuid,
|
|
|
|
|
n.uuid AS source_node_uuid,
|
|
|
|
|
m.uuid AS target_node_uuid,
|
|
|
|
|
r.created_at AS created_at,
|
|
|
|
|
r.name AS name,
|
|
|
|
|
r.fact AS fact,
|
|
|
|
|
r.fact_embedding AS fact_embedding,
|
|
|
|
|
r.episodes AS episodes,
|
|
|
|
|
r.expired_at AS expired_at,
|
|
|
|
|
r.valid_at AS valid_at,
|
|
|
|
|
r.invalid_at AS invalid_at
|
|
|
|
|
ORDER BY score DESC LIMIT $limit
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
fuzzy_query = re.sub(r'[^\w\s]', '', query) + '~'
|
|
|
|
|
|
|
|
|
|
records, _, _ = await driver.execute_query(
|
|
|
|
|
cypher_query,
|
2024-08-23 14:18:45 -04:00
|
|
|
query=fuzzy_query,
|
2024-09-03 13:25:52 -04:00
|
|
|
source_uuid=source_node_uuid,
|
|
|
|
|
target_uuid=target_node_uuid,
|
2024-08-23 14:18:45 -04:00
|
|
|
limit=limit,
|
|
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
edges: list[EntityEdge] = []
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
for record in records:
|
|
|
|
|
edge = EntityEdge(
|
|
|
|
|
uuid=record['uuid'],
|
|
|
|
|
source_node_uuid=record['source_node_uuid'],
|
|
|
|
|
target_node_uuid=record['target_node_uuid'],
|
|
|
|
|
fact=record['fact'],
|
|
|
|
|
name=record['name'],
|
|
|
|
|
episodes=record['episodes'],
|
|
|
|
|
fact_embedding=record['fact_embedding'],
|
|
|
|
|
created_at=record['created_at'].to_native(),
|
|
|
|
|
expired_at=parse_db_date(record['expired_at']),
|
|
|
|
|
valid_at=parse_db_date(record['valid_at']),
|
|
|
|
|
invalid_at=parse_db_date(record['invalid_at']),
|
|
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
edges.append(edge)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return edges
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
|
2024-08-26 20:00:28 -07:00
|
|
|
async def hybrid_node_search(
|
2024-09-05 12:05:44 -04:00
|
|
|
queries: list[str],
|
|
|
|
|
embeddings: list[list[float]],
|
|
|
|
|
driver: AsyncDriver,
|
|
|
|
|
limit: int = RELEVANT_SCHEMA_LIMIT,
|
2024-08-18 13:22:31 -04:00
|
|
|
) -> list[EntityNode]:
|
2024-08-26 20:00:28 -07:00
|
|
|
"""
|
|
|
|
|
Perform a hybrid search for nodes using both text queries and embeddings.
|
|
|
|
|
|
|
|
|
|
This method combines fulltext search and vector similarity search to find
|
2024-09-04 10:05:45 -04:00
|
|
|
relevant nodes in the graph database. It uses a rrf reranker.
|
2024-08-26 20:00:28 -07:00
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
queries : list[str]
|
|
|
|
|
A list of text queries to search for.
|
|
|
|
|
embeddings : list[list[float]]
|
|
|
|
|
A list of embedding vectors corresponding to the queries. If empty only fulltext search is performed.
|
|
|
|
|
driver : AsyncDriver
|
|
|
|
|
The Neo4j driver instance for database operations.
|
|
|
|
|
limit : int | None, optional
|
|
|
|
|
The maximum number of results to return per search method. If None, a default limit will be applied.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
list[EntityNode]
|
|
|
|
|
A list of unique EntityNode objects that match the search criteria.
|
|
|
|
|
|
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
This method performs the following steps:
|
|
|
|
|
1. Executes fulltext searches for each query.
|
|
|
|
|
2. Executes vector similarity searches for each embedding.
|
|
|
|
|
3. Combines and deduplicates the results from both search types.
|
|
|
|
|
4. Logs the performance metrics of the search operation.
|
|
|
|
|
|
|
|
|
|
The search results are deduplicated based on the node UUIDs to ensure
|
|
|
|
|
uniqueness in the returned list. The 'limit' parameter is applied to each
|
|
|
|
|
individual search method before deduplication. If not specified, a default
|
|
|
|
|
limit (defined in the individual search functions) will be used.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
start = time()
|
2024-08-30 10:48:28 -04:00
|
|
|
|
|
|
|
|
results: list[list[EntityNode]] = list(
|
|
|
|
|
await asyncio.gather(
|
|
|
|
|
*[entity_fulltext_search(q, driver, 2 * limit) for q in queries],
|
|
|
|
|
*[entity_similarity_search(e, driver, 2 * limit) for e in embeddings],
|
|
|
|
|
)
|
2024-08-23 14:18:45 -04:00
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-30 10:48:28 -04:00
|
|
|
node_uuid_map: dict[str, EntityNode] = {
|
|
|
|
|
node.uuid: node for result in results for node in result
|
|
|
|
|
}
|
|
|
|
|
result_uuids = [[node.uuid for node in result] for result in results]
|
|
|
|
|
|
|
|
|
|
ranked_uuids = rrf(result_uuids)
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-30 10:48:28 -04:00
|
|
|
relevant_nodes: list[EntityNode] = [node_uuid_map[uuid] for uuid in ranked_uuids]
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
end = time()
|
2024-08-30 10:48:28 -04:00
|
|
|
logger.info(f'Found relevant nodes: {ranked_uuids} in {(end - start) * 1000} ms')
|
2024-08-26 20:00:28 -07:00
|
|
|
return relevant_nodes
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-26 20:00:28 -07:00
|
|
|
|
|
|
|
|
async def get_relevant_nodes(
|
2024-09-05 12:05:44 -04:00
|
|
|
nodes: list[EntityNode],
|
|
|
|
|
driver: AsyncDriver,
|
2024-08-26 20:00:28 -07:00
|
|
|
) -> list[EntityNode]:
|
|
|
|
|
"""
|
|
|
|
|
Retrieve relevant nodes based on the provided list of EntityNodes.
|
|
|
|
|
|
|
|
|
|
This method performs a hybrid search using both the names and embeddings
|
|
|
|
|
of the input nodes to find relevant nodes in the graph database.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
nodes : list[EntityNode]
|
|
|
|
|
A list of EntityNode objects to use as the basis for the search.
|
|
|
|
|
driver : AsyncDriver
|
|
|
|
|
The Neo4j driver instance for database operations.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
list[EntityNode]
|
|
|
|
|
A list of EntityNode objects that are deemed relevant based on the input nodes.
|
|
|
|
|
|
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
This method uses the hybrid_node_search function to perform the search,
|
|
|
|
|
which combines fulltext search and vector similarity search.
|
|
|
|
|
It extracts the names and name embeddings (if available) from the input nodes
|
|
|
|
|
to use as search criteria.
|
|
|
|
|
"""
|
|
|
|
|
relevant_nodes = await hybrid_node_search(
|
|
|
|
|
[node.name for node in nodes],
|
|
|
|
|
[node.name_embedding for node in nodes if node.name_embedding is not None],
|
|
|
|
|
driver,
|
|
|
|
|
)
|
2024-08-23 14:18:45 -04:00
|
|
|
return relevant_nodes
|
2024-08-18 13:22:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_relevant_edges(
|
2024-09-05 12:05:44 -04:00
|
|
|
driver: AsyncDriver,
|
|
|
|
|
edges: list[EntityEdge],
|
|
|
|
|
source_node_uuid: str | None,
|
|
|
|
|
target_node_uuid: str | None,
|
|
|
|
|
limit: int = RELEVANT_SCHEMA_LIMIT,
|
2024-08-18 13:22:31 -04:00
|
|
|
) -> list[EntityEdge]:
|
2024-08-23 14:18:45 -04:00
|
|
|
start = time()
|
|
|
|
|
relevant_edges: list[EntityEdge] = []
|
|
|
|
|
relevant_edge_uuids = set()
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
results = await asyncio.gather(
|
|
|
|
|
*[
|
2024-09-03 13:25:52 -04:00
|
|
|
edge_similarity_search(
|
2024-09-04 10:05:45 -04:00
|
|
|
driver, edge.fact_embedding, source_node_uuid, target_node_uuid, limit
|
2024-09-03 13:25:52 -04:00
|
|
|
)
|
2024-08-23 14:18:45 -04:00
|
|
|
for edge in edges
|
|
|
|
|
if edge.fact_embedding is not None
|
|
|
|
|
],
|
2024-09-03 13:25:52 -04:00
|
|
|
*[
|
2024-09-04 10:05:45 -04:00
|
|
|
edge_fulltext_search(driver, edge.fact, source_node_uuid, target_node_uuid, limit)
|
2024-09-03 13:25:52 -04:00
|
|
|
for edge in edges
|
|
|
|
|
],
|
2024-08-23 14:18:45 -04:00
|
|
|
)
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
for result in results:
|
|
|
|
|
for edge in result:
|
|
|
|
|
if edge.uuid in relevant_edge_uuids:
|
|
|
|
|
continue
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
relevant_edge_uuids.add(edge.uuid)
|
|
|
|
|
relevant_edges.append(edge)
|
2024-08-21 12:03:32 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
end = time()
|
|
|
|
|
logger.info(f'Found relevant edges: {relevant_edge_uuids} in {(end - start) * 1000} ms')
|
2024-08-18 13:22:31 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return relevant_edges
|
2024-08-22 14:26:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# takes in a list of rankings of uuids
|
|
|
|
|
def rrf(results: list[list[str]], rank_const=1) -> list[str]:
|
2024-08-26 18:34:57 -04:00
|
|
|
scores: dict[str, float] = defaultdict(float)
|
2024-08-23 14:18:45 -04:00
|
|
|
for result in results:
|
|
|
|
|
for i, uuid in enumerate(result):
|
|
|
|
|
scores[uuid] += 1 / (i + rank_const)
|
2024-08-22 14:26:26 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
scored_uuids = [term for term in scores.items()]
|
|
|
|
|
scored_uuids.sort(reverse=True, key=lambda term: term[1])
|
2024-08-22 14:26:26 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
sorted_uuids = [term[0] for term in scored_uuids]
|
2024-08-22 14:26:26 -04:00
|
|
|
|
2024-08-23 14:18:45 -04:00
|
|
|
return sorted_uuids
|
2024-08-26 18:34:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def node_distance_reranker(
|
2024-09-05 12:05:44 -04:00
|
|
|
driver: AsyncDriver, results: list[list[str]], center_node_uuid: str
|
2024-08-26 18:34:57 -04:00
|
|
|
) -> list[str]:
|
|
|
|
|
# use rrf as a preliminary ranker
|
|
|
|
|
sorted_uuids = rrf(results)
|
|
|
|
|
scores: dict[str, float] = {}
|
|
|
|
|
|
|
|
|
|
for uuid in sorted_uuids:
|
2024-09-04 10:05:45 -04:00
|
|
|
# Find the shortest path to center node
|
2024-08-26 18:34:57 -04:00
|
|
|
records, _, _ = await driver.execute_query(
|
|
|
|
|
"""
|
|
|
|
|
MATCH (source:Entity)-[r:RELATES_TO {uuid: $edge_uuid}]->(target:Entity)
|
2024-09-01 12:16:04 -07:00
|
|
|
MATCH p = SHORTEST 1 (center:Entity)-[:RELATES_TO*1..10]->(n:Entity)
|
2024-08-26 18:34:57 -04:00
|
|
|
WHERE center.uuid = $center_uuid AND n.uuid IN [source.uuid, target.uuid]
|
|
|
|
|
RETURN min(length(p)) AS score, source.uuid AS source_uuid, target.uuid AS target_uuid
|
|
|
|
|
""",
|
|
|
|
|
edge_uuid=uuid,
|
|
|
|
|
center_uuid=center_node_uuid,
|
|
|
|
|
)
|
|
|
|
|
distance = 0.01
|
|
|
|
|
|
|
|
|
|
for record in records:
|
|
|
|
|
if (
|
2024-09-05 12:05:44 -04:00
|
|
|
record['source_uuid'] == center_node_uuid
|
|
|
|
|
or record['target_uuid'] == center_node_uuid
|
2024-08-26 18:34:57 -04:00
|
|
|
):
|
|
|
|
|
continue
|
|
|
|
|
distance = record['score']
|
|
|
|
|
|
|
|
|
|
if uuid in scores:
|
|
|
|
|
scores[uuid] = min(1 / distance, scores[uuid])
|
|
|
|
|
else:
|
|
|
|
|
scores[uuid] = 1 / distance
|
|
|
|
|
|
|
|
|
|
# rerank on shortest distance
|
|
|
|
|
sorted_uuids.sort(reverse=True, key=lambda cur_uuid: scores[cur_uuid])
|
|
|
|
|
|
|
|
|
|
return sorted_uuids
|