diff --git a/cognee/modules/retrieval/cypher_search_retriever.py b/cognee/modules/retrieval/cypher_search_retriever.py index 1b4efb88d..9978f2536 100644 --- a/cognee/modules/retrieval/cypher_search_retriever.py +++ b/cognee/modules/retrieval/cypher_search_retriever.py @@ -44,6 +44,12 @@ class CypherSearchRetriever(BaseRetriever): """ try: graph_engine = await get_graph_engine() + is_empty = await graph_engine.is_empty() + + if is_empty: + logger.warning("Search attempt on an empty knowledge graph") + return [] + result = await graph_engine.query(query) except Exception as e: logger.error("Failed to execture cypher search retrieval: %s", str(e)) diff --git a/cognee/modules/retrieval/graph_completion_retriever.py b/cognee/modules/retrieval/graph_completion_retriever.py index b14b83977..b7ab4edae 100644 --- a/cognee/modules/retrieval/graph_completion_retriever.py +++ b/cognee/modules/retrieval/graph_completion_retriever.py @@ -124,6 +124,13 @@ class GraphCompletionRetriever(BaseGraphRetriever): - str: A string representing the resolved context from the retrieved triplets, or an empty string if no triplets are found. """ + graph_engine = await get_graph_engine() + is_empty = await graph_engine.is_empty() + + if is_empty: + logger.warning("Search attempt on an empty knowledge graph") + return [] + triplets = await self.get_triplets(query) if len(triplets) == 0: diff --git a/cognee/modules/retrieval/natural_language_retriever.py b/cognee/modules/retrieval/natural_language_retriever.py index 056a62163..6496b49b2 100644 --- a/cognee/modules/retrieval/natural_language_retriever.py +++ b/cognee/modules/retrieval/natural_language_retriever.py @@ -122,6 +122,11 @@ class NaturalLanguageRetriever(BaseRetriever): query. """ graph_engine = await get_graph_engine() + is_empty = await graph_engine.is_empty() + + if is_empty: + logger.warning("Search attempt on an empty knowledge graph") + return [] return await self._execute_cypher_query(query, graph_engine) diff --git a/cognee/modules/search/methods/no_access_control_search.py b/cognee/modules/search/methods/no_access_control_search.py index c43105ca0..fcb02da46 100644 --- a/cognee/modules/search/methods/no_access_control_search.py +++ b/cognee/modules/search/methods/no_access_control_search.py @@ -1,12 +1,16 @@ from typing import Any, List, Optional, Tuple, Type, Union +from cognee.infrastructure.databases.graph import get_graph_engine from cognee.modules.data.models.Dataset import Dataset from cognee.modules.engine.models.node_set import NodeSet from cognee.modules.graph.cognee_graph.CogneeGraphElements import Edge from cognee.modules.search.types import SearchType +from cognee.shared.logging_utils import get_logger from .get_search_type_tools import get_search_type_tools +logger = get_logger() + async def no_access_control_search( query_type: SearchType, @@ -32,6 +36,12 @@ async def no_access_control_search( save_interaction=save_interaction, last_k=last_k, ) + graph_engine = await get_graph_engine() + is_empty = await graph_engine.is_empty() + + if is_empty: + # TODO: we can log here, but not all search types use graph. Still keeping this here for reviewer input + logger.warning("Search attempt on an empty knowledge graph") if len(search_tools) == 2: [get_completion, get_context] = search_tools diff --git a/cognee/modules/search/methods/search.py b/cognee/modules/search/methods/search.py index c3c59feac..aeab21a02 100644 --- a/cognee/modules/search/methods/search.py +++ b/cognee/modules/search/methods/search.py @@ -5,6 +5,8 @@ from uuid import UUID from fastapi.encoders import jsonable_encoder from typing import Any, List, Optional, Tuple, Type, Union +from cognee.infrastructure.databases.graph import get_graph_engine +from cognee.shared.logging_utils import get_logger from cognee.shared.utils import send_telemetry from cognee.context_global_variables import set_database_global_context_variables @@ -27,6 +29,8 @@ from .get_search_type_tools import get_search_type_tools from .no_access_control_search import no_access_control_search from ..utils.prepare_search_result import prepare_search_result +logger = get_logger() + async def search( query_text: str, @@ -329,6 +333,13 @@ async def search_in_datasets_context( # Set database configuration in async context for each dataset user has access for await set_database_global_context_variables(dataset.id, dataset.owner_id) + graph_engine = await get_graph_engine() + is_empty = await graph_engine.is_empty() + + if is_empty: + # TODO: we can log here, but not all search types use graph. Still keeping this here for reviewer input + logger.warning("Search attempt on an empty knowledge graph") + specific_search_tools = await get_search_type_tools( query_type=query_type, query_text=query_text,