cognee 0.3.6__py3-none-any.whl → 0.3.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cognee/__init__.py +1 -0
- cognee/api/health.py +2 -12
- cognee/api/v1/add/add.py +46 -6
- cognee/api/v1/add/routers/get_add_router.py +5 -1
- cognee/api/v1/cognify/cognify.py +29 -9
- cognee/api/v1/datasets/datasets.py +11 -0
- cognee/api/v1/responses/default_tools.py +0 -1
- cognee/api/v1/responses/dispatch_function.py +1 -1
- cognee/api/v1/responses/routers/default_tools.py +0 -1
- cognee/api/v1/search/search.py +11 -9
- cognee/api/v1/settings/routers/get_settings_router.py +7 -1
- cognee/api/v1/ui/ui.py +47 -16
- cognee/api/v1/update/routers/get_update_router.py +1 -1
- cognee/api/v1/update/update.py +3 -3
- cognee/cli/_cognee.py +61 -10
- cognee/cli/commands/add_command.py +3 -3
- cognee/cli/commands/cognify_command.py +3 -3
- cognee/cli/commands/config_command.py +9 -7
- cognee/cli/commands/delete_command.py +3 -3
- cognee/cli/commands/search_command.py +3 -7
- cognee/cli/config.py +0 -1
- cognee/context_global_variables.py +5 -0
- cognee/exceptions/exceptions.py +1 -1
- cognee/infrastructure/databases/cache/__init__.py +2 -0
- cognee/infrastructure/databases/cache/cache_db_interface.py +79 -0
- cognee/infrastructure/databases/cache/config.py +44 -0
- cognee/infrastructure/databases/cache/get_cache_engine.py +67 -0
- cognee/infrastructure/databases/cache/redis/RedisAdapter.py +243 -0
- cognee/infrastructure/databases/exceptions/__init__.py +1 -0
- cognee/infrastructure/databases/exceptions/exceptions.py +18 -2
- cognee/infrastructure/databases/graph/get_graph_engine.py +1 -1
- cognee/infrastructure/databases/graph/graph_db_interface.py +5 -0
- cognee/infrastructure/databases/graph/kuzu/adapter.py +67 -44
- cognee/infrastructure/databases/graph/neo4j_driver/adapter.py +13 -3
- cognee/infrastructure/databases/graph/neo4j_driver/deadlock_retry.py +1 -1
- cognee/infrastructure/databases/graph/neptune_driver/neptune_utils.py +1 -1
- cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py +1 -1
- cognee/infrastructure/databases/vector/embeddings/FastembedEmbeddingEngine.py +21 -3
- cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +17 -10
- cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +17 -4
- cognee/infrastructure/databases/vector/embeddings/config.py +2 -3
- cognee/infrastructure/databases/vector/exceptions/exceptions.py +1 -1
- cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py +0 -1
- cognee/infrastructure/files/exceptions.py +1 -1
- cognee/infrastructure/files/storage/LocalFileStorage.py +9 -9
- cognee/infrastructure/files/storage/S3FileStorage.py +11 -11
- cognee/infrastructure/files/utils/guess_file_type.py +6 -0
- cognee/infrastructure/llm/prompts/search_type_selector_prompt.txt +0 -5
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +19 -9
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +17 -5
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +17 -5
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +32 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/mistral/__init__.py +0 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/mistral/adapter.py +109 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py +33 -8
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +40 -18
- cognee/infrastructure/loaders/LoaderEngine.py +27 -7
- cognee/infrastructure/loaders/external/__init__.py +7 -0
- cognee/infrastructure/loaders/external/advanced_pdf_loader.py +2 -8
- cognee/infrastructure/loaders/external/beautiful_soup_loader.py +310 -0
- cognee/infrastructure/loaders/supported_loaders.py +7 -0
- cognee/modules/data/exceptions/exceptions.py +1 -1
- cognee/modules/data/methods/__init__.py +3 -0
- cognee/modules/data/methods/get_dataset_data.py +4 -1
- cognee/modules/data/methods/has_dataset_data.py +21 -0
- cognee/modules/engine/models/TableRow.py +0 -1
- cognee/modules/ingestion/save_data_to_file.py +9 -2
- cognee/modules/pipelines/exceptions/exceptions.py +1 -1
- cognee/modules/pipelines/operations/pipeline.py +12 -1
- cognee/modules/pipelines/operations/run_tasks.py +25 -197
- cognee/modules/pipelines/operations/run_tasks_data_item.py +260 -0
- cognee/modules/pipelines/operations/run_tasks_distributed.py +121 -38
- cognee/modules/retrieval/EntityCompletionRetriever.py +48 -8
- cognee/modules/retrieval/base_graph_retriever.py +3 -1
- cognee/modules/retrieval/base_retriever.py +3 -1
- cognee/modules/retrieval/chunks_retriever.py +5 -1
- cognee/modules/retrieval/code_retriever.py +20 -2
- cognee/modules/retrieval/completion_retriever.py +50 -9
- cognee/modules/retrieval/cypher_search_retriever.py +11 -1
- cognee/modules/retrieval/graph_completion_context_extension_retriever.py +47 -8
- cognee/modules/retrieval/graph_completion_cot_retriever.py +32 -1
- cognee/modules/retrieval/graph_completion_retriever.py +54 -10
- cognee/modules/retrieval/lexical_retriever.py +20 -2
- cognee/modules/retrieval/natural_language_retriever.py +10 -1
- cognee/modules/retrieval/summaries_retriever.py +5 -1
- cognee/modules/retrieval/temporal_retriever.py +62 -10
- cognee/modules/retrieval/user_qa_feedback.py +3 -2
- cognee/modules/retrieval/utils/completion.py +5 -0
- cognee/modules/retrieval/utils/description_to_codepart_search.py +1 -1
- cognee/modules/retrieval/utils/session_cache.py +156 -0
- cognee/modules/search/methods/get_search_type_tools.py +0 -5
- cognee/modules/search/methods/no_access_control_search.py +12 -1
- cognee/modules/search/methods/search.py +34 -2
- cognee/modules/search/types/SearchType.py +0 -1
- cognee/modules/settings/get_settings.py +23 -0
- cognee/modules/users/methods/get_authenticated_user.py +3 -1
- cognee/modules/users/methods/get_default_user.py +1 -6
- cognee/modules/users/roles/methods/create_role.py +2 -2
- cognee/modules/users/tenants/methods/create_tenant.py +2 -2
- cognee/shared/exceptions/exceptions.py +1 -1
- cognee/tasks/codingagents/coding_rule_associations.py +1 -2
- cognee/tasks/documents/exceptions/exceptions.py +1 -1
- cognee/tasks/graph/extract_graph_from_data.py +2 -0
- cognee/tasks/ingestion/data_item_to_text_file.py +3 -3
- cognee/tasks/ingestion/ingest_data.py +11 -5
- cognee/tasks/ingestion/save_data_item_to_storage.py +12 -1
- cognee/tasks/storage/add_data_points.py +3 -10
- cognee/tasks/storage/index_data_points.py +19 -14
- cognee/tasks/storage/index_graph_edges.py +25 -11
- cognee/tasks/web_scraper/__init__.py +34 -0
- cognee/tasks/web_scraper/config.py +26 -0
- cognee/tasks/web_scraper/default_url_crawler.py +446 -0
- cognee/tasks/web_scraper/models.py +46 -0
- cognee/tasks/web_scraper/types.py +4 -0
- cognee/tasks/web_scraper/utils.py +142 -0
- cognee/tasks/web_scraper/web_scraper_task.py +396 -0
- cognee/tests/cli_tests/cli_unit_tests/test_cli_utils.py +0 -1
- cognee/tests/integration/web_url_crawler/test_default_url_crawler.py +13 -0
- cognee/tests/integration/web_url_crawler/test_tavily_crawler.py +19 -0
- cognee/tests/integration/web_url_crawler/test_url_adding_e2e.py +344 -0
- cognee/tests/subprocesses/reader.py +25 -0
- cognee/tests/subprocesses/simple_cognify_1.py +31 -0
- cognee/tests/subprocesses/simple_cognify_2.py +31 -0
- cognee/tests/subprocesses/writer.py +32 -0
- cognee/tests/tasks/descriptive_metrics/metrics_test_utils.py +0 -2
- cognee/tests/tasks/descriptive_metrics/neo4j_metrics_test.py +8 -3
- cognee/tests/tasks/entity_extraction/entity_extraction_test.py +89 -0
- cognee/tests/tasks/web_scraping/web_scraping_test.py +172 -0
- cognee/tests/test_add_docling_document.py +56 -0
- cognee/tests/test_chromadb.py +7 -11
- cognee/tests/test_concurrent_subprocess_access.py +76 -0
- cognee/tests/test_conversation_history.py +240 -0
- cognee/tests/test_kuzu.py +27 -15
- cognee/tests/test_lancedb.py +7 -11
- cognee/tests/test_library.py +32 -2
- cognee/tests/test_neo4j.py +24 -16
- cognee/tests/test_neptune_analytics_vector.py +7 -11
- cognee/tests/test_permissions.py +9 -13
- cognee/tests/test_pgvector.py +4 -4
- cognee/tests/test_remote_kuzu.py +8 -11
- cognee/tests/test_s3_file_storage.py +1 -1
- cognee/tests/test_search_db.py +6 -8
- cognee/tests/unit/infrastructure/databases/cache/test_cache_config.py +89 -0
- cognee/tests/unit/modules/retrieval/conversation_history_test.py +154 -0
- {cognee-0.3.6.dist-info → cognee-0.3.7.dist-info}/METADATA +21 -6
- {cognee-0.3.6.dist-info → cognee-0.3.7.dist-info}/RECORD +155 -126
- {cognee-0.3.6.dist-info → cognee-0.3.7.dist-info}/entry_points.txt +1 -0
- distributed/Dockerfile +0 -3
- distributed/entrypoint.py +21 -9
- distributed/signal.py +5 -0
- distributed/workers/data_point_saving_worker.py +64 -34
- distributed/workers/graph_saving_worker.py +71 -47
- cognee/infrastructure/databases/graph/memgraph/memgraph_adapter.py +0 -1116
- cognee/modules/retrieval/insights_retriever.py +0 -133
- cognee/tests/test_memgraph.py +0 -109
- cognee/tests/unit/modules/retrieval/insights_retriever_test.py +0 -251
- {cognee-0.3.6.dist-info → cognee-0.3.7.dist-info}/WHEEL +0 -0
- {cognee-0.3.6.dist-info → cognee-0.3.7.dist-info}/licenses/LICENSE +0 -0
- {cognee-0.3.6.dist-info → cognee-0.3.7.dist-info}/licenses/NOTICE.md +0 -0
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import asyncio
|
|
2
|
-
from typing import Any, Optional
|
|
3
|
-
|
|
4
|
-
from cognee.modules.graph.cognee_graph.CogneeGraphElements import Edge, Node
|
|
5
|
-
from cognee.modules.retrieval.base_graph_retriever import BaseGraphRetriever
|
|
6
|
-
from cognee.shared.logging_utils import get_logger
|
|
7
|
-
from cognee.infrastructure.databases.graph import get_graph_engine
|
|
8
|
-
from cognee.infrastructure.databases.vector import get_vector_engine
|
|
9
|
-
from cognee.modules.retrieval.exceptions.exceptions import NoDataError
|
|
10
|
-
from cognee.infrastructure.databases.vector.exceptions.exceptions import CollectionNotFoundError
|
|
11
|
-
|
|
12
|
-
logger = get_logger("InsightsRetriever")
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class InsightsRetriever(BaseGraphRetriever):
|
|
16
|
-
"""
|
|
17
|
-
Retriever for handling graph connection-based insights.
|
|
18
|
-
|
|
19
|
-
Public methods include:
|
|
20
|
-
- get_context
|
|
21
|
-
- get_completion
|
|
22
|
-
|
|
23
|
-
Instance variables include:
|
|
24
|
-
- exploration_levels
|
|
25
|
-
- top_k
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
def __init__(self, exploration_levels: int = 1, top_k: Optional[int] = 5):
|
|
29
|
-
"""Initialize retriever with exploration levels and search parameters."""
|
|
30
|
-
self.exploration_levels = exploration_levels
|
|
31
|
-
self.top_k = top_k
|
|
32
|
-
|
|
33
|
-
async def get_context(self, query: str) -> list:
|
|
34
|
-
"""
|
|
35
|
-
Find neighbours of a given node in the graph.
|
|
36
|
-
|
|
37
|
-
If the provided query does not correspond to an existing node,
|
|
38
|
-
search for similar entities and retrieve their connections.
|
|
39
|
-
Reraises NoDataError if there is no data found in the system.
|
|
40
|
-
|
|
41
|
-
Parameters:
|
|
42
|
-
-----------
|
|
43
|
-
|
|
44
|
-
- query (str): A string identifier for the node whose neighbours are to be
|
|
45
|
-
retrieved.
|
|
46
|
-
|
|
47
|
-
Returns:
|
|
48
|
-
--------
|
|
49
|
-
|
|
50
|
-
- list: A list of unique connections found for the queried node.
|
|
51
|
-
"""
|
|
52
|
-
if query is None:
|
|
53
|
-
return []
|
|
54
|
-
|
|
55
|
-
node_id = query
|
|
56
|
-
graph_engine = await get_graph_engine()
|
|
57
|
-
exact_node = await graph_engine.extract_node(node_id)
|
|
58
|
-
|
|
59
|
-
if exact_node is not None and "id" in exact_node:
|
|
60
|
-
node_connections = await graph_engine.get_connections(str(exact_node["id"]))
|
|
61
|
-
else:
|
|
62
|
-
vector_engine = get_vector_engine()
|
|
63
|
-
|
|
64
|
-
try:
|
|
65
|
-
results = await asyncio.gather(
|
|
66
|
-
vector_engine.search("Entity_name", query_text=query, limit=self.top_k),
|
|
67
|
-
vector_engine.search("EntityType_name", query_text=query, limit=self.top_k),
|
|
68
|
-
)
|
|
69
|
-
except CollectionNotFoundError as error:
|
|
70
|
-
logger.error("Entity collections not found")
|
|
71
|
-
raise NoDataError("No data found in the system, please add data first.") from error
|
|
72
|
-
|
|
73
|
-
results = [*results[0], *results[1]]
|
|
74
|
-
relevant_results = [result for result in results if result.score < 0.5][: self.top_k]
|
|
75
|
-
|
|
76
|
-
if len(relevant_results) == 0:
|
|
77
|
-
return []
|
|
78
|
-
|
|
79
|
-
node_connections_results = await asyncio.gather(
|
|
80
|
-
*[graph_engine.get_connections(result.id) for result in relevant_results]
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
node_connections = []
|
|
84
|
-
for neighbours in node_connections_results:
|
|
85
|
-
node_connections.extend(neighbours)
|
|
86
|
-
|
|
87
|
-
unique_node_connections_map = {}
|
|
88
|
-
unique_node_connections = []
|
|
89
|
-
|
|
90
|
-
for node_connection in node_connections:
|
|
91
|
-
if "id" not in node_connection[0] or "id" not in node_connection[2]:
|
|
92
|
-
continue
|
|
93
|
-
|
|
94
|
-
unique_id = f"{node_connection[0]['id']} {node_connection[1]['relationship_name']} {node_connection[2]['id']}"
|
|
95
|
-
if unique_id not in unique_node_connections_map:
|
|
96
|
-
unique_node_connections_map[unique_id] = True
|
|
97
|
-
unique_node_connections.append(node_connection)
|
|
98
|
-
|
|
99
|
-
return unique_node_connections
|
|
100
|
-
# return [
|
|
101
|
-
# Edge(
|
|
102
|
-
# node1=Node(node_id=connection[0]["id"], attributes=connection[0]),
|
|
103
|
-
# node2=Node(node_id=connection[2]["id"], attributes=connection[2]),
|
|
104
|
-
# attributes={
|
|
105
|
-
# **connection[1],
|
|
106
|
-
# "relationship_type": connection[1]["relationship_name"],
|
|
107
|
-
# },
|
|
108
|
-
# )
|
|
109
|
-
# for connection in unique_node_connections
|
|
110
|
-
# ]
|
|
111
|
-
|
|
112
|
-
async def get_completion(self, query: str, context: Optional[Any] = None) -> Any:
|
|
113
|
-
"""
|
|
114
|
-
Returns the graph connections context.
|
|
115
|
-
|
|
116
|
-
If a context is not provided, it fetches the context using the query provided.
|
|
117
|
-
|
|
118
|
-
Parameters:
|
|
119
|
-
-----------
|
|
120
|
-
|
|
121
|
-
- query (str): A string identifier used to fetch the context.
|
|
122
|
-
- context (Optional[Any]): An optional context to use for the completion; if None,
|
|
123
|
-
it fetches the context based on the query. (default None)
|
|
124
|
-
|
|
125
|
-
Returns:
|
|
126
|
-
--------
|
|
127
|
-
|
|
128
|
-
- Any: The context used for the completion, which is either provided or fetched
|
|
129
|
-
based on the query.
|
|
130
|
-
"""
|
|
131
|
-
if context is None:
|
|
132
|
-
context = await self.get_context(query)
|
|
133
|
-
return context
|
cognee/tests/test_memgraph.py
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
|
|
3
|
-
import pathlib
|
|
4
|
-
import cognee
|
|
5
|
-
from cognee.infrastructure.files.storage import get_storage_config
|
|
6
|
-
from cognee.modules.search.operations import get_history
|
|
7
|
-
from cognee.modules.users.methods import get_default_user
|
|
8
|
-
from cognee.shared.logging_utils import get_logger
|
|
9
|
-
from cognee.modules.search.types import SearchType
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
logger = get_logger()
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
async def main():
|
|
16
|
-
cognee.config.set_graph_database_provider("memgraph")
|
|
17
|
-
data_directory_path = str(
|
|
18
|
-
pathlib.Path(
|
|
19
|
-
os.path.join(pathlib.Path(__file__).parent, ".data_storage/test_memgraph")
|
|
20
|
-
).resolve()
|
|
21
|
-
)
|
|
22
|
-
cognee.config.data_root_directory(data_directory_path)
|
|
23
|
-
cognee_directory_path = str(
|
|
24
|
-
pathlib.Path(
|
|
25
|
-
os.path.join(pathlib.Path(__file__).parent, ".cognee_system/test_memgraph")
|
|
26
|
-
).resolve()
|
|
27
|
-
)
|
|
28
|
-
cognee.config.system_root_directory(cognee_directory_path)
|
|
29
|
-
|
|
30
|
-
await cognee.prune.prune_data()
|
|
31
|
-
await cognee.prune.prune_system(metadata=True)
|
|
32
|
-
|
|
33
|
-
dataset_name = "cs_explanations"
|
|
34
|
-
|
|
35
|
-
explanation_file_path = os.path.join(
|
|
36
|
-
pathlib.Path(__file__).parent, "test_data/Natural_language_processing.txt"
|
|
37
|
-
)
|
|
38
|
-
await cognee.add([explanation_file_path], dataset_name)
|
|
39
|
-
|
|
40
|
-
text = """A quantum computer is a computer that takes advantage of quantum mechanical phenomena.
|
|
41
|
-
At small scales, physical matter exhibits properties of both particles and waves, and quantum computing leverages this behavior, specifically quantum superposition and entanglement, using specialized hardware that supports the preparation and manipulation of quantum states.
|
|
42
|
-
Classical physics cannot explain the operation of these quantum devices, and a scalable quantum computer could perform some calculations exponentially faster (with respect to input size scaling) than any modern "classical" computer. In particular, a large-scale quantum computer could break widely used encryption schemes and aid physicists in performing physical simulations; however, the current state of the technology is largely experimental and impractical, with several obstacles to useful applications. Moreover, scalable quantum computers do not hold promise for many practical tasks, and for many important tasks quantum speedups are proven impossible.
|
|
43
|
-
The basic unit of information in quantum computing is the qubit, similar to the bit in traditional digital electronics. Unlike a classical bit, a qubit can exist in a superposition of its two "basis" states. When measuring a qubit, the result is a probabilistic output of a classical bit, therefore making quantum computers nondeterministic in general. If a quantum computer manipulates the qubit in a particular way, wave interference effects can amplify the desired measurement results. The design of quantum algorithms involves creating procedures that allow a quantum computer to perform calculations efficiently and quickly.
|
|
44
|
-
Physically engineering high-quality qubits has proven challenging. If a physical qubit is not sufficiently isolated from its environment, it suffers from quantum decoherence, introducing noise into calculations. Paradoxically, perfectly isolating qubits is also undesirable because quantum computations typically need to initialize qubits, perform controlled qubit interactions, and measure the resulting quantum states. Each of those operations introduces errors and suffers from noise, and such inaccuracies accumulate.
|
|
45
|
-
In principle, a non-quantum (classical) computer can solve the same computational problems as a quantum computer, given enough time. Quantum advantage comes in the form of time complexity rather than computability, and quantum complexity theory shows that some quantum algorithms for carefully selected tasks require exponentially fewer computational steps than the best known non-quantum algorithms. Such tasks can in theory be solved on a large-scale quantum computer whereas classical computers would not finish computations in any reasonable amount of time. However, quantum speedup is not universal or even typical across computational tasks, since basic tasks such as sorting are proven to not allow any asymptotic quantum speedup. Claims of quantum supremacy have drawn significant attention to the discipline, but are demonstrated on contrived tasks, while near-term practical use cases remain limited.
|
|
46
|
-
"""
|
|
47
|
-
|
|
48
|
-
await cognee.add([text], dataset_name)
|
|
49
|
-
|
|
50
|
-
await cognee.cognify([dataset_name])
|
|
51
|
-
|
|
52
|
-
from cognee.infrastructure.databases.vector import get_vector_engine
|
|
53
|
-
|
|
54
|
-
vector_engine = get_vector_engine()
|
|
55
|
-
random_node = (await vector_engine.search("Entity_name", "Quantum computer"))[0]
|
|
56
|
-
random_node_name = random_node.payload["text"]
|
|
57
|
-
|
|
58
|
-
search_results = await cognee.search(
|
|
59
|
-
query_type=SearchType.INSIGHTS, query_text=random_node_name
|
|
60
|
-
)
|
|
61
|
-
assert len(search_results) != 0, "The search results list is empty."
|
|
62
|
-
print("\n\nExtracted sentences are:\n")
|
|
63
|
-
for result in search_results:
|
|
64
|
-
print(f"{result}\n")
|
|
65
|
-
|
|
66
|
-
search_results = await cognee.search(query_type=SearchType.CHUNKS, query_text=random_node_name)
|
|
67
|
-
assert len(search_results) != 0, "The search results list is empty."
|
|
68
|
-
print("\n\nExtracted chunks are:\n")
|
|
69
|
-
for result in search_results:
|
|
70
|
-
print(f"{result}\n")
|
|
71
|
-
|
|
72
|
-
search_results = await cognee.search(
|
|
73
|
-
query_type=SearchType.SUMMARIES, query_text=random_node_name
|
|
74
|
-
)
|
|
75
|
-
assert len(search_results) != 0, "Query related summaries don't exist."
|
|
76
|
-
print("\nExtracted results are:\n")
|
|
77
|
-
for result in search_results:
|
|
78
|
-
print(f"{result}\n")
|
|
79
|
-
|
|
80
|
-
search_results = await cognee.search(
|
|
81
|
-
query_type=SearchType.NATURAL_LANGUAGE,
|
|
82
|
-
query_text=f"Find nodes connected to node with name {random_node_name}",
|
|
83
|
-
)
|
|
84
|
-
assert len(search_results) != 0, "Query related natural language don't exist."
|
|
85
|
-
print("\nExtracted results are:\n")
|
|
86
|
-
for result in search_results:
|
|
87
|
-
print(f"{result}\n")
|
|
88
|
-
|
|
89
|
-
user = await get_default_user()
|
|
90
|
-
history = await get_history(user.id)
|
|
91
|
-
|
|
92
|
-
assert len(history) == 8, "Search history is not correct."
|
|
93
|
-
|
|
94
|
-
await cognee.prune.prune_data()
|
|
95
|
-
data_root_directory = get_storage_config()["data_root_directory"]
|
|
96
|
-
assert not os.path.isdir(data_root_directory), "Local data files are not deleted"
|
|
97
|
-
|
|
98
|
-
await cognee.prune.prune_system(metadata=True)
|
|
99
|
-
from cognee.infrastructure.databases.graph import get_graph_engine
|
|
100
|
-
|
|
101
|
-
graph_engine = await get_graph_engine()
|
|
102
|
-
nodes, edges = await graph_engine.get_graph_data()
|
|
103
|
-
assert len(nodes) == 0 and len(edges) == 0, "Memgraph graph database is not empty"
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if __name__ == "__main__":
|
|
107
|
-
import asyncio
|
|
108
|
-
|
|
109
|
-
asyncio.run(main())
|
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import pytest
|
|
3
|
-
import pathlib
|
|
4
|
-
|
|
5
|
-
import cognee
|
|
6
|
-
from cognee.low_level import setup
|
|
7
|
-
from cognee.tasks.storage import add_data_points
|
|
8
|
-
from cognee.modules.engine.models import Entity, EntityType
|
|
9
|
-
from cognee.infrastructure.databases.graph import get_graph_engine
|
|
10
|
-
from cognee.infrastructure.databases.vector import get_vector_engine
|
|
11
|
-
from cognee.modules.retrieval.exceptions.exceptions import NoDataError
|
|
12
|
-
from cognee.modules.retrieval.insights_retriever import InsightsRetriever
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class TestInsightsRetriever:
|
|
16
|
-
@pytest.mark.asyncio
|
|
17
|
-
async def test_insights_context_simple(self):
|
|
18
|
-
system_directory_path = os.path.join(
|
|
19
|
-
pathlib.Path(__file__).parent, ".cognee_system/test_insights_context_simple"
|
|
20
|
-
)
|
|
21
|
-
cognee.config.system_root_directory(system_directory_path)
|
|
22
|
-
data_directory_path = os.path.join(
|
|
23
|
-
pathlib.Path(__file__).parent, ".data_storage/test_insights_context_simple"
|
|
24
|
-
)
|
|
25
|
-
cognee.config.data_root_directory(data_directory_path)
|
|
26
|
-
|
|
27
|
-
await cognee.prune.prune_data()
|
|
28
|
-
await cognee.prune.prune_system(metadata=True)
|
|
29
|
-
await setup()
|
|
30
|
-
|
|
31
|
-
entityTypePerson = EntityType(
|
|
32
|
-
name="Person",
|
|
33
|
-
description="An individual",
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
person1 = Entity(
|
|
37
|
-
name="Steve Rodger",
|
|
38
|
-
is_a=entityTypePerson,
|
|
39
|
-
description="An American actor, comedian, and filmmaker",
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
person2 = Entity(
|
|
43
|
-
name="Mike Broski",
|
|
44
|
-
is_a=entityTypePerson,
|
|
45
|
-
description="Financial advisor and philanthropist",
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
person3 = Entity(
|
|
49
|
-
name="Christina Mayer",
|
|
50
|
-
is_a=entityTypePerson,
|
|
51
|
-
description="Maker of next generation of iconic American music videos",
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
entityTypeCompany = EntityType(
|
|
55
|
-
name="Company",
|
|
56
|
-
description="An organization that operates on an annual basis",
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
company1 = Entity(
|
|
60
|
-
name="Apple",
|
|
61
|
-
is_a=entityTypeCompany,
|
|
62
|
-
description="An American multinational technology company headquartered in Cupertino, California",
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
company2 = Entity(
|
|
66
|
-
name="Google",
|
|
67
|
-
is_a=entityTypeCompany,
|
|
68
|
-
description="An American multinational technology company that specializes in Internet-related services and products",
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
company3 = Entity(
|
|
72
|
-
name="Facebook",
|
|
73
|
-
is_a=entityTypeCompany,
|
|
74
|
-
description="An American social media, messaging, and online platform",
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
entities = [person1, person2, person3, company1, company2, company3]
|
|
78
|
-
|
|
79
|
-
await add_data_points(entities)
|
|
80
|
-
|
|
81
|
-
retriever = InsightsRetriever()
|
|
82
|
-
|
|
83
|
-
context = await retriever.get_context("Mike")
|
|
84
|
-
|
|
85
|
-
assert context[0][0]["name"] == "Mike Broski", "Failed to get Mike Broski"
|
|
86
|
-
|
|
87
|
-
@pytest.mark.asyncio
|
|
88
|
-
async def test_insights_context_complex(self):
|
|
89
|
-
system_directory_path = os.path.join(
|
|
90
|
-
pathlib.Path(__file__).parent, ".cognee_system/test_insights_context_complex"
|
|
91
|
-
)
|
|
92
|
-
cognee.config.system_root_directory(system_directory_path)
|
|
93
|
-
data_directory_path = os.path.join(
|
|
94
|
-
pathlib.Path(__file__).parent, ".data_storage/test_insights_context_complex"
|
|
95
|
-
)
|
|
96
|
-
cognee.config.data_root_directory(data_directory_path)
|
|
97
|
-
|
|
98
|
-
await cognee.prune.prune_data()
|
|
99
|
-
await cognee.prune.prune_system(metadata=True)
|
|
100
|
-
await setup()
|
|
101
|
-
|
|
102
|
-
entityTypePerson = EntityType(
|
|
103
|
-
name="Person",
|
|
104
|
-
description="An individual",
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
person1 = Entity(
|
|
108
|
-
name="Steve Rodger",
|
|
109
|
-
is_a=entityTypePerson,
|
|
110
|
-
description="An American actor, comedian, and filmmaker",
|
|
111
|
-
)
|
|
112
|
-
|
|
113
|
-
person2 = Entity(
|
|
114
|
-
name="Mike Broski",
|
|
115
|
-
is_a=entityTypePerson,
|
|
116
|
-
description="Financial advisor and philanthropist",
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
person3 = Entity(
|
|
120
|
-
name="Christina Mayer",
|
|
121
|
-
is_a=entityTypePerson,
|
|
122
|
-
description="Maker of next generation of iconic American music videos",
|
|
123
|
-
)
|
|
124
|
-
|
|
125
|
-
person4 = Entity(
|
|
126
|
-
name="Jason Statham",
|
|
127
|
-
is_a=entityTypePerson,
|
|
128
|
-
description="An American actor",
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
person5 = Entity(
|
|
132
|
-
name="Mike Tyson",
|
|
133
|
-
is_a=entityTypePerson,
|
|
134
|
-
description="A former professional boxer from the United States",
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
entityTypeCompany = EntityType(
|
|
138
|
-
name="Company",
|
|
139
|
-
description="An organization that operates on an annual basis",
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
company1 = Entity(
|
|
143
|
-
name="Apple",
|
|
144
|
-
is_a=entityTypeCompany,
|
|
145
|
-
description="An American multinational technology company headquartered in Cupertino, California",
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
company2 = Entity(
|
|
149
|
-
name="Google",
|
|
150
|
-
is_a=entityTypeCompany,
|
|
151
|
-
description="An American multinational technology company that specializes in Internet-related services and products",
|
|
152
|
-
)
|
|
153
|
-
|
|
154
|
-
company3 = Entity(
|
|
155
|
-
name="Facebook",
|
|
156
|
-
is_a=entityTypeCompany,
|
|
157
|
-
description="An American social media, messaging, and online platform",
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
entities = [person1, person2, person3, company1, company2, company3]
|
|
161
|
-
|
|
162
|
-
await add_data_points(entities)
|
|
163
|
-
|
|
164
|
-
graph_engine = await get_graph_engine()
|
|
165
|
-
|
|
166
|
-
await graph_engine.add_edges(
|
|
167
|
-
[
|
|
168
|
-
(
|
|
169
|
-
(str)(person1.id),
|
|
170
|
-
(str)(company1.id),
|
|
171
|
-
"works_for",
|
|
172
|
-
dict(
|
|
173
|
-
relationship_name="works_for",
|
|
174
|
-
source_node_id=person1.id,
|
|
175
|
-
target_node_id=company1.id,
|
|
176
|
-
),
|
|
177
|
-
),
|
|
178
|
-
(
|
|
179
|
-
(str)(person2.id),
|
|
180
|
-
(str)(company2.id),
|
|
181
|
-
"works_for",
|
|
182
|
-
dict(
|
|
183
|
-
relationship_name="works_for",
|
|
184
|
-
source_node_id=person2.id,
|
|
185
|
-
target_node_id=company2.id,
|
|
186
|
-
),
|
|
187
|
-
),
|
|
188
|
-
(
|
|
189
|
-
(str)(person3.id),
|
|
190
|
-
(str)(company3.id),
|
|
191
|
-
"works_for",
|
|
192
|
-
dict(
|
|
193
|
-
relationship_name="works_for",
|
|
194
|
-
source_node_id=person3.id,
|
|
195
|
-
target_node_id=company3.id,
|
|
196
|
-
),
|
|
197
|
-
),
|
|
198
|
-
(
|
|
199
|
-
(str)(person4.id),
|
|
200
|
-
(str)(company1.id),
|
|
201
|
-
"works_for",
|
|
202
|
-
dict(
|
|
203
|
-
relationship_name="works_for",
|
|
204
|
-
source_node_id=person4.id,
|
|
205
|
-
target_node_id=company1.id,
|
|
206
|
-
),
|
|
207
|
-
),
|
|
208
|
-
(
|
|
209
|
-
(str)(person5.id),
|
|
210
|
-
(str)(company1.id),
|
|
211
|
-
"works_for",
|
|
212
|
-
dict(
|
|
213
|
-
relationship_name="works_for",
|
|
214
|
-
source_node_id=person5.id,
|
|
215
|
-
target_node_id=company1.id,
|
|
216
|
-
),
|
|
217
|
-
),
|
|
218
|
-
]
|
|
219
|
-
)
|
|
220
|
-
|
|
221
|
-
retriever = InsightsRetriever(top_k=20)
|
|
222
|
-
|
|
223
|
-
context = await retriever.get_context("Christina")
|
|
224
|
-
|
|
225
|
-
assert context[0][0]["name"] == "Christina Mayer", "Failed to get Christina Mayer"
|
|
226
|
-
|
|
227
|
-
@pytest.mark.asyncio
|
|
228
|
-
async def test_insights_context_on_empty_graph(self):
|
|
229
|
-
system_directory_path = os.path.join(
|
|
230
|
-
pathlib.Path(__file__).parent, ".cognee_system/test_insights_context_on_empty_graph"
|
|
231
|
-
)
|
|
232
|
-
cognee.config.system_root_directory(system_directory_path)
|
|
233
|
-
data_directory_path = os.path.join(
|
|
234
|
-
pathlib.Path(__file__).parent, ".data_storage/test_insights_context_on_empty_graph"
|
|
235
|
-
)
|
|
236
|
-
cognee.config.data_root_directory(data_directory_path)
|
|
237
|
-
|
|
238
|
-
await cognee.prune.prune_data()
|
|
239
|
-
await cognee.prune.prune_system(metadata=True)
|
|
240
|
-
|
|
241
|
-
retriever = InsightsRetriever()
|
|
242
|
-
|
|
243
|
-
with pytest.raises(NoDataError):
|
|
244
|
-
await retriever.get_context("Christina Mayer")
|
|
245
|
-
|
|
246
|
-
vector_engine = get_vector_engine()
|
|
247
|
-
await vector_engine.create_collection("Entity_name", payload_schema=Entity)
|
|
248
|
-
await vector_engine.create_collection("EntityType_name", payload_schema=EntityType)
|
|
249
|
-
|
|
250
|
-
context = await retriever.get_context("Christina Mayer")
|
|
251
|
-
assert context == [], "Returned context should be empty on an empty graph"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|