cognee 0.5.1__py3-none-any.whl → 0.5.2__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.
Files changed (265) hide show
  1. cognee/__init__.py +2 -0
  2. cognee/alembic/README +1 -0
  3. cognee/alembic/env.py +107 -0
  4. cognee/alembic/script.py.mako +26 -0
  5. cognee/alembic/versions/1a58b986e6e1_enable_delete_for_old_tutorial_notebooks.py +52 -0
  6. cognee/alembic/versions/1d0bb7fede17_add_pipeline_run_status.py +33 -0
  7. cognee/alembic/versions/1daae0df1866_incremental_loading.py +48 -0
  8. cognee/alembic/versions/211ab850ef3d_add_sync_operations_table.py +118 -0
  9. cognee/alembic/versions/45957f0a9849_add_notebook_table.py +46 -0
  10. cognee/alembic/versions/46a6ce2bd2b2_expand_dataset_database_with_json_.py +333 -0
  11. cognee/alembic/versions/482cd6517ce4_add_default_user.py +30 -0
  12. cognee/alembic/versions/76625596c5c3_expand_dataset_database_for_multi_user.py +98 -0
  13. cognee/alembic/versions/8057ae7329c2_initial_migration.py +25 -0
  14. cognee/alembic/versions/9e7a3cb85175_loader_separation.py +104 -0
  15. cognee/alembic/versions/a1b2c3d4e5f6_add_label_column_to_data.py +38 -0
  16. cognee/alembic/versions/ab7e313804ae_permission_system_rework.py +236 -0
  17. cognee/alembic/versions/b9274c27a25a_kuzu_11_migration.py +75 -0
  18. cognee/alembic/versions/c946955da633_multi_tenant_support.py +137 -0
  19. cognee/alembic/versions/e1ec1dcb50b6_add_last_accessed_to_data.py +51 -0
  20. cognee/alembic/versions/e4ebee1091e7_expand_data_model_info.py +140 -0
  21. cognee/alembic.ini +117 -0
  22. cognee/api/v1/add/add.py +2 -1
  23. cognee/api/v1/add/routers/get_add_router.py +2 -0
  24. cognee/api/v1/cognify/cognify.py +11 -6
  25. cognee/api/v1/cognify/routers/get_cognify_router.py +8 -0
  26. cognee/api/v1/config/config.py +60 -0
  27. cognee/api/v1/datasets/routers/get_datasets_router.py +46 -3
  28. cognee/api/v1/memify/routers/get_memify_router.py +3 -0
  29. cognee/api/v1/search/routers/get_search_router.py +21 -6
  30. cognee/api/v1/search/search.py +21 -5
  31. cognee/api/v1/sync/routers/get_sync_router.py +3 -3
  32. cognee/cli/commands/add_command.py +1 -1
  33. cognee/cli/commands/cognify_command.py +6 -0
  34. cognee/cli/commands/config_command.py +1 -1
  35. cognee/context_global_variables.py +5 -1
  36. cognee/eval_framework/answer_generation/answer_generation_executor.py +7 -8
  37. cognee/infrastructure/databases/cache/cache_db_interface.py +38 -1
  38. cognee/infrastructure/databases/cache/config.py +6 -0
  39. cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py +21 -0
  40. cognee/infrastructure/databases/cache/get_cache_engine.py +9 -3
  41. cognee/infrastructure/databases/cache/redis/RedisAdapter.py +60 -1
  42. cognee/infrastructure/databases/dataset_database_handler/supported_dataset_database_handlers.py +7 -0
  43. cognee/infrastructure/databases/graph/get_graph_engine.py +29 -1
  44. cognee/infrastructure/databases/graph/neo4j_driver/Neo4jAuraDevDatasetDatabaseHandler.py +62 -27
  45. cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py +17 -4
  46. cognee/infrastructure/databases/relational/config.py +16 -1
  47. cognee/infrastructure/databases/relational/create_relational_engine.py +13 -3
  48. cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py +26 -3
  49. cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py +2 -0
  50. cognee/infrastructure/databases/vector/config.py +6 -0
  51. cognee/infrastructure/databases/vector/create_vector_engine.py +70 -16
  52. cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +64 -9
  53. cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +13 -2
  54. cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py +16 -3
  55. cognee/infrastructure/databases/vector/models/ScoredResult.py +3 -3
  56. cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py +16 -3
  57. cognee/infrastructure/databases/vector/pgvector/PGVectorDatasetDatabaseHandler.py +86 -0
  58. cognee/infrastructure/databases/vector/pgvector/create_db_and_tables.py +81 -2
  59. cognee/infrastructure/databases/vector/vector_db_interface.py +8 -0
  60. cognee/infrastructure/files/utils/get_data_file_path.py +33 -27
  61. cognee/infrastructure/llm/LLMGateway.py +0 -13
  62. cognee/infrastructure/llm/prompts/extract_query_time.txt +1 -1
  63. cognee/infrastructure/llm/prompts/generate_event_entity_prompt.txt +1 -1
  64. cognee/infrastructure/llm/prompts/generate_event_graph_prompt.txt +1 -1
  65. cognee/infrastructure/llm/prompts/generate_graph_prompt.txt +2 -2
  66. cognee/infrastructure/llm/prompts/generate_graph_prompt_guided.txt +1 -1
  67. cognee/infrastructure/llm/prompts/generate_graph_prompt_oneshot.txt +2 -2
  68. cognee/infrastructure/llm/prompts/generate_graph_prompt_simple.txt +1 -1
  69. cognee/infrastructure/llm/prompts/generate_graph_prompt_strict.txt +1 -1
  70. cognee/infrastructure/llm/prompts/search_type_selector_prompt.txt +6 -6
  71. cognee/infrastructure/llm/prompts/test.txt +1 -1
  72. cognee/infrastructure/llm/prompts/translate_content.txt +19 -0
  73. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +17 -12
  74. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +31 -25
  75. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +132 -7
  76. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +29 -5
  77. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/llama_cpp/adapter.py +191 -0
  78. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/llm_interface.py +2 -6
  79. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/mistral/adapter.py +58 -13
  80. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py +0 -1
  81. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +25 -131
  82. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/types.py +10 -0
  83. cognee/modules/chunking/models/DocumentChunk.py +0 -1
  84. cognee/modules/cognify/config.py +2 -0
  85. cognee/modules/data/models/Data.py +3 -1
  86. cognee/modules/engine/models/Entity.py +0 -1
  87. cognee/modules/engine/operations/setup.py +6 -0
  88. cognee/modules/graph/cognee_graph/CogneeGraph.py +150 -37
  89. cognee/modules/graph/cognee_graph/CogneeGraphElements.py +48 -2
  90. cognee/modules/graph/utils/__init__.py +1 -0
  91. cognee/modules/graph/utils/get_entity_nodes_from_triplets.py +12 -0
  92. cognee/modules/notebooks/methods/__init__.py +1 -0
  93. cognee/modules/notebooks/methods/create_notebook.py +0 -34
  94. cognee/modules/notebooks/methods/create_tutorial_notebooks.py +191 -0
  95. cognee/modules/notebooks/methods/get_notebooks.py +12 -8
  96. cognee/modules/notebooks/tutorials/cognee-basics/cell-1.md +3 -0
  97. cognee/modules/notebooks/tutorials/cognee-basics/cell-2.md +10 -0
  98. cognee/modules/notebooks/tutorials/cognee-basics/cell-3.md +7 -0
  99. cognee/modules/notebooks/tutorials/cognee-basics/cell-4.py +28 -0
  100. cognee/modules/notebooks/tutorials/cognee-basics/cell-5.py +3 -0
  101. cognee/modules/notebooks/tutorials/cognee-basics/cell-6.py +9 -0
  102. cognee/modules/notebooks/tutorials/cognee-basics/cell-7.py +17 -0
  103. cognee/modules/notebooks/tutorials/cognee-basics/config.json +4 -0
  104. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-1.md +3 -0
  105. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-10.md +3 -0
  106. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-11.md +3 -0
  107. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-12.py +3 -0
  108. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-13.md +7 -0
  109. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-14.py +6 -0
  110. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-15.md +3 -0
  111. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-16.py +7 -0
  112. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-2.md +9 -0
  113. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-3.md +7 -0
  114. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-4.md +9 -0
  115. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-5.md +5 -0
  116. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-6.py +13 -0
  117. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-7.md +3 -0
  118. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-8.md +3 -0
  119. cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-9.py +31 -0
  120. cognee/modules/notebooks/tutorials/python-development-with-cognee/config.json +4 -0
  121. cognee/modules/notebooks/tutorials/python-development-with-cognee/data/copilot_conversations.json +107 -0
  122. cognee/modules/notebooks/tutorials/python-development-with-cognee/data/guido_contributions.json +976 -0
  123. cognee/modules/notebooks/tutorials/python-development-with-cognee/data/my_developer_rules.md +79 -0
  124. cognee/modules/notebooks/tutorials/python-development-with-cognee/data/pep_style_guide.md +74 -0
  125. cognee/modules/notebooks/tutorials/python-development-with-cognee/data/zen_principles.md +74 -0
  126. cognee/modules/retrieval/EntityCompletionRetriever.py +51 -38
  127. cognee/modules/retrieval/__init__.py +0 -1
  128. cognee/modules/retrieval/base_retriever.py +66 -10
  129. cognee/modules/retrieval/chunks_retriever.py +57 -49
  130. cognee/modules/retrieval/coding_rules_retriever.py +12 -5
  131. cognee/modules/retrieval/completion_retriever.py +29 -28
  132. cognee/modules/retrieval/cypher_search_retriever.py +25 -20
  133. cognee/modules/retrieval/graph_completion_context_extension_retriever.py +42 -46
  134. cognee/modules/retrieval/graph_completion_cot_retriever.py +68 -51
  135. cognee/modules/retrieval/graph_completion_retriever.py +78 -63
  136. cognee/modules/retrieval/graph_summary_completion_retriever.py +2 -0
  137. cognee/modules/retrieval/lexical_retriever.py +34 -12
  138. cognee/modules/retrieval/natural_language_retriever.py +18 -15
  139. cognee/modules/retrieval/summaries_retriever.py +51 -34
  140. cognee/modules/retrieval/temporal_retriever.py +59 -49
  141. cognee/modules/retrieval/triplet_retriever.py +32 -33
  142. cognee/modules/retrieval/utils/access_tracking.py +88 -0
  143. cognee/modules/retrieval/utils/brute_force_triplet_search.py +99 -103
  144. cognee/modules/retrieval/utils/node_edge_vector_search.py +174 -0
  145. cognee/modules/search/methods/__init__.py +1 -0
  146. cognee/modules/search/methods/get_retriever_output.py +53 -0
  147. cognee/modules/search/methods/get_search_type_retriever_instance.py +252 -0
  148. cognee/modules/search/methods/search.py +90 -222
  149. cognee/modules/search/models/SearchResultPayload.py +67 -0
  150. cognee/modules/search/types/SearchResult.py +1 -8
  151. cognee/modules/search/types/SearchType.py +1 -2
  152. cognee/modules/search/types/__init__.py +1 -1
  153. cognee/modules/search/utils/__init__.py +1 -2
  154. cognee/modules/search/utils/transform_insights_to_graph.py +2 -2
  155. cognee/modules/search/utils/{transform_context_to_graph.py → transform_triplets_to_graph.py} +2 -2
  156. cognee/modules/users/authentication/default/default_transport.py +11 -1
  157. cognee/modules/users/authentication/get_api_auth_backend.py +2 -1
  158. cognee/modules/users/authentication/get_client_auth_backend.py +2 -1
  159. cognee/modules/users/methods/create_user.py +0 -9
  160. cognee/modules/users/permissions/methods/has_user_management_permission.py +29 -0
  161. cognee/modules/visualization/cognee_network_visualization.py +1 -1
  162. cognee/run_migrations.py +48 -0
  163. cognee/shared/exceptions/__init__.py +1 -3
  164. cognee/shared/exceptions/exceptions.py +11 -1
  165. cognee/shared/usage_logger.py +332 -0
  166. cognee/shared/utils.py +12 -5
  167. cognee/tasks/chunks/__init__.py +9 -0
  168. cognee/tasks/cleanup/cleanup_unused_data.py +172 -0
  169. cognee/tasks/graph/__init__.py +7 -0
  170. cognee/tasks/ingestion/data_item.py +8 -0
  171. cognee/tasks/ingestion/ingest_data.py +12 -1
  172. cognee/tasks/ingestion/save_data_item_to_storage.py +5 -0
  173. cognee/tasks/memify/__init__.py +8 -0
  174. cognee/tasks/memify/extract_usage_frequency.py +613 -0
  175. cognee/tasks/summarization/models.py +0 -2
  176. cognee/tasks/temporal_graph/__init__.py +0 -1
  177. cognee/tasks/translation/__init__.py +96 -0
  178. cognee/tasks/translation/config.py +110 -0
  179. cognee/tasks/translation/detect_language.py +190 -0
  180. cognee/tasks/translation/exceptions.py +62 -0
  181. cognee/tasks/translation/models.py +72 -0
  182. cognee/tasks/translation/providers/__init__.py +44 -0
  183. cognee/tasks/translation/providers/azure_provider.py +192 -0
  184. cognee/tasks/translation/providers/base.py +85 -0
  185. cognee/tasks/translation/providers/google_provider.py +158 -0
  186. cognee/tasks/translation/providers/llm_provider.py +143 -0
  187. cognee/tasks/translation/translate_content.py +282 -0
  188. cognee/tasks/web_scraper/default_url_crawler.py +6 -2
  189. cognee/tests/cli_tests/cli_unit_tests/test_cli_commands.py +1 -0
  190. cognee/tests/cli_tests/cli_unit_tests/test_cli_edge_cases.py +3 -0
  191. cognee/tests/integration/retrieval/test_brute_force_triplet_search_with_cognify.py +62 -0
  192. cognee/tests/integration/retrieval/test_chunks_retriever.py +351 -0
  193. cognee/tests/integration/retrieval/test_graph_completion_retriever.py +276 -0
  194. cognee/tests/integration/retrieval/test_graph_completion_retriever_context_extension.py +228 -0
  195. cognee/tests/integration/retrieval/test_graph_completion_retriever_cot.py +217 -0
  196. cognee/tests/integration/retrieval/test_rag_completion_retriever.py +319 -0
  197. cognee/tests/integration/retrieval/test_structured_output.py +258 -0
  198. cognee/tests/integration/retrieval/test_summaries_retriever.py +195 -0
  199. cognee/tests/integration/retrieval/test_temporal_retriever.py +336 -0
  200. cognee/tests/integration/retrieval/test_triplet_retriever.py +45 -1
  201. cognee/tests/integration/shared/test_usage_logger_integration.py +255 -0
  202. cognee/tests/tasks/translation/README.md +147 -0
  203. cognee/tests/tasks/translation/__init__.py +1 -0
  204. cognee/tests/tasks/translation/config_test.py +93 -0
  205. cognee/tests/tasks/translation/detect_language_test.py +118 -0
  206. cognee/tests/tasks/translation/providers_test.py +151 -0
  207. cognee/tests/tasks/translation/translate_content_test.py +213 -0
  208. cognee/tests/test_chromadb.py +1 -1
  209. cognee/tests/test_cleanup_unused_data.py +165 -0
  210. cognee/tests/test_custom_data_label.py +68 -0
  211. cognee/tests/test_delete_by_id.py +6 -6
  212. cognee/tests/test_extract_usage_frequency.py +308 -0
  213. cognee/tests/test_kuzu.py +17 -7
  214. cognee/tests/test_lancedb.py +3 -1
  215. cognee/tests/test_library.py +1 -1
  216. cognee/tests/test_neo4j.py +17 -7
  217. cognee/tests/test_neptune_analytics_vector.py +3 -1
  218. cognee/tests/test_permissions.py +172 -187
  219. cognee/tests/test_pgvector.py +3 -1
  220. cognee/tests/test_relational_db_migration.py +15 -1
  221. cognee/tests/test_remote_kuzu.py +3 -1
  222. cognee/tests/test_s3_file_storage.py +1 -1
  223. cognee/tests/test_search_db.py +345 -205
  224. cognee/tests/test_usage_logger_e2e.py +268 -0
  225. cognee/tests/unit/api/test_get_raw_data_endpoint.py +206 -0
  226. cognee/tests/unit/eval_framework/answer_generation_test.py +4 -3
  227. cognee/tests/unit/eval_framework/benchmark_adapters_test.py +25 -0
  228. cognee/tests/unit/eval_framework/corpus_builder_test.py +33 -4
  229. cognee/tests/unit/infrastructure/databases/cache/test_cache_config.py +2 -0
  230. cognee/tests/unit/infrastructure/databases/relational/test_RelationalConfig.py +69 -0
  231. cognee/tests/unit/modules/graph/cognee_graph_elements_test.py +42 -2
  232. cognee/tests/unit/modules/graph/cognee_graph_test.py +329 -31
  233. cognee/tests/unit/modules/retrieval/chunks_retriever_test.py +122 -168
  234. cognee/tests/unit/modules/retrieval/conversation_history_test.py +338 -0
  235. cognee/tests/unit/modules/retrieval/graph_completion_retriever_context_extension_test.py +486 -157
  236. cognee/tests/unit/modules/retrieval/graph_completion_retriever_cot_test.py +693 -155
  237. cognee/tests/unit/modules/retrieval/graph_completion_retriever_test.py +619 -200
  238. cognee/tests/unit/modules/retrieval/rag_completion_retriever_test.py +300 -171
  239. cognee/tests/unit/modules/retrieval/summaries_retriever_test.py +184 -155
  240. cognee/tests/unit/modules/retrieval/temporal_retriever_test.py +544 -79
  241. cognee/tests/unit/modules/retrieval/test_brute_force_triplet_search.py +476 -28
  242. cognee/tests/unit/modules/retrieval/test_completion.py +343 -0
  243. cognee/tests/unit/modules/retrieval/test_graph_summary_completion_retriever.py +157 -0
  244. cognee/tests/unit/modules/retrieval/test_node_edge_vector_search.py +273 -0
  245. cognee/tests/unit/modules/retrieval/test_user_qa_feedback.py +312 -0
  246. cognee/tests/unit/modules/retrieval/triplet_retriever_test.py +267 -7
  247. cognee/tests/unit/modules/search/test_get_search_type_retriever_instance.py +125 -0
  248. cognee/tests/unit/modules/search/test_search.py +96 -20
  249. cognee/tests/unit/modules/search/test_search_prepare_search_result_contract.py +190 -0
  250. cognee/tests/unit/modules/users/test_tutorial_notebook_creation.py +511 -297
  251. cognee/tests/unit/shared/test_usage_logger.py +241 -0
  252. cognee/tests/unit/users/permissions/test_has_user_management_permission.py +46 -0
  253. {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/METADATA +22 -17
  254. {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/RECORD +258 -157
  255. cognee/api/.env.example +0 -5
  256. cognee/modules/retrieval/base_graph_retriever.py +0 -24
  257. cognee/modules/search/methods/get_search_type_tools.py +0 -223
  258. cognee/modules/search/methods/no_access_control_search.py +0 -62
  259. cognee/modules/search/utils/prepare_search_result.py +0 -63
  260. cognee/tests/test_feedback_enrichment.py +0 -174
  261. cognee/tests/unit/modules/retrieval/structured_output_test.py +0 -204
  262. {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/WHEEL +0 -0
  263. {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/entry_points.txt +0 -0
  264. {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/licenses/LICENSE +0 -0
  265. {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/licenses/NOTICE.md +0 -0
@@ -0,0 +1,351 @@
1
+ import os
2
+ import pytest
3
+ import pathlib
4
+ import pytest_asyncio
5
+ from typing import List
6
+ import cognee
7
+
8
+ from cognee.low_level import setup
9
+ from cognee.tasks.storage import add_data_points
10
+ from cognee.infrastructure.databases.vector import get_vector_engine
11
+ from cognee.modules.chunking.models import DocumentChunk
12
+ from cognee.modules.data.processing.document_types import TextDocument
13
+ from cognee.modules.retrieval.chunks_retriever import ChunksRetriever
14
+ from cognee.infrastructure.engine import DataPoint
15
+ from cognee.modules.data.processing.document_types import Document
16
+ from cognee.modules.engine.models import Entity
17
+
18
+
19
+ class DocumentChunkWithEntities(DataPoint):
20
+ text: str
21
+ chunk_size: int
22
+ chunk_index: int
23
+ cut_type: str
24
+ is_part_of: Document
25
+ contains: List[Entity] = None
26
+
27
+ metadata: dict = {"index_fields": ["text"]}
28
+
29
+
30
+ @pytest_asyncio.fixture
31
+ async def setup_test_environment_with_chunks_simple():
32
+ """Set up a clean test environment with simple chunks."""
33
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
34
+ system_directory_path = str(base_dir / ".cognee_system/test_chunks_retriever_context_simple")
35
+ data_directory_path = str(base_dir / ".data_storage/test_chunks_retriever_context_simple")
36
+
37
+ cognee.config.system_root_directory(system_directory_path)
38
+ cognee.config.data_root_directory(data_directory_path)
39
+
40
+ await cognee.prune.prune_data()
41
+ await cognee.prune.prune_system(metadata=True)
42
+ from cognee.infrastructure.databases.graph.get_graph_engine import _create_graph_engine
43
+ from cognee.infrastructure.databases.vector.create_vector_engine import (
44
+ _create_vector_engine,
45
+ )
46
+ from cognee.infrastructure.databases.relational.create_relational_engine import (
47
+ create_relational_engine,
48
+ )
49
+
50
+ _create_graph_engine.cache_clear()
51
+ _create_vector_engine.cache_clear()
52
+ create_relational_engine.cache_clear()
53
+ await setup()
54
+
55
+ document = TextDocument(
56
+ name="Steve Rodger's career",
57
+ raw_data_location="somewhere",
58
+ external_metadata="",
59
+ mime_type="text/plain",
60
+ )
61
+
62
+ chunk1 = DocumentChunk(
63
+ text="Steve Rodger",
64
+ chunk_size=2,
65
+ chunk_index=0,
66
+ cut_type="sentence_end",
67
+ is_part_of=document,
68
+ contains=[],
69
+ )
70
+ chunk2 = DocumentChunk(
71
+ text="Mike Broski",
72
+ chunk_size=2,
73
+ chunk_index=1,
74
+ cut_type="sentence_end",
75
+ is_part_of=document,
76
+ contains=[],
77
+ )
78
+ chunk3 = DocumentChunk(
79
+ text="Christina Mayer",
80
+ chunk_size=2,
81
+ chunk_index=2,
82
+ cut_type="sentence_end",
83
+ is_part_of=document,
84
+ contains=[],
85
+ )
86
+
87
+ entities = [chunk1, chunk2, chunk3]
88
+
89
+ await add_data_points(entities)
90
+
91
+ yield
92
+
93
+ try:
94
+ await cognee.prune.prune_data()
95
+ await cognee.prune.prune_system(metadata=True)
96
+ from cognee.infrastructure.databases.graph.get_graph_engine import _create_graph_engine
97
+ from cognee.infrastructure.databases.vector.create_vector_engine import (
98
+ _create_vector_engine,
99
+ )
100
+ from cognee.infrastructure.databases.relational.create_relational_engine import (
101
+ create_relational_engine,
102
+ )
103
+
104
+ _create_graph_engine.cache_clear()
105
+ _create_vector_engine.cache_clear()
106
+ create_relational_engine.cache_clear()
107
+ except Exception:
108
+ pass
109
+
110
+
111
+ @pytest_asyncio.fixture
112
+ async def setup_test_environment_with_chunks_complex():
113
+ """Set up a clean test environment with complex chunks."""
114
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
115
+ system_directory_path = str(base_dir / ".cognee_system/test_chunks_retriever_context_complex")
116
+ data_directory_path = str(base_dir / ".data_storage/test_chunks_retriever_context_complex")
117
+
118
+ cognee.config.system_root_directory(system_directory_path)
119
+ cognee.config.data_root_directory(data_directory_path)
120
+
121
+ await cognee.prune.prune_data()
122
+ await cognee.prune.prune_system(metadata=True)
123
+ from cognee.infrastructure.databases.graph.get_graph_engine import _create_graph_engine
124
+ from cognee.infrastructure.databases.vector.create_vector_engine import (
125
+ _create_vector_engine,
126
+ )
127
+ from cognee.infrastructure.databases.relational.create_relational_engine import (
128
+ create_relational_engine,
129
+ )
130
+
131
+ _create_graph_engine.cache_clear()
132
+ _create_vector_engine.cache_clear()
133
+ create_relational_engine.cache_clear()
134
+ await setup()
135
+
136
+ document1 = TextDocument(
137
+ name="Employee List",
138
+ raw_data_location="somewhere",
139
+ external_metadata="",
140
+ mime_type="text/plain",
141
+ )
142
+
143
+ document2 = TextDocument(
144
+ name="Car List",
145
+ raw_data_location="somewhere",
146
+ external_metadata="",
147
+ mime_type="text/plain",
148
+ )
149
+
150
+ chunk1 = DocumentChunk(
151
+ text="Steve Rodger",
152
+ chunk_size=2,
153
+ chunk_index=0,
154
+ cut_type="sentence_end",
155
+ is_part_of=document1,
156
+ contains=[],
157
+ )
158
+ chunk2 = DocumentChunk(
159
+ text="Mike Broski",
160
+ chunk_size=2,
161
+ chunk_index=1,
162
+ cut_type="sentence_end",
163
+ is_part_of=document1,
164
+ contains=[],
165
+ )
166
+ chunk3 = DocumentChunk(
167
+ text="Christina Mayer",
168
+ chunk_size=2,
169
+ chunk_index=2,
170
+ cut_type="sentence_end",
171
+ is_part_of=document1,
172
+ contains=[],
173
+ )
174
+
175
+ chunk4 = DocumentChunk(
176
+ text="Range Rover",
177
+ chunk_size=2,
178
+ chunk_index=0,
179
+ cut_type="sentence_end",
180
+ is_part_of=document2,
181
+ contains=[],
182
+ )
183
+ chunk5 = DocumentChunk(
184
+ text="Hyundai",
185
+ chunk_size=2,
186
+ chunk_index=1,
187
+ cut_type="sentence_end",
188
+ is_part_of=document2,
189
+ contains=[],
190
+ )
191
+ chunk6 = DocumentChunk(
192
+ text="Chrysler",
193
+ chunk_size=2,
194
+ chunk_index=2,
195
+ cut_type="sentence_end",
196
+ is_part_of=document2,
197
+ contains=[],
198
+ )
199
+
200
+ entities = [chunk1, chunk2, chunk3, chunk4, chunk5, chunk6]
201
+
202
+ await add_data_points(entities)
203
+
204
+ yield
205
+
206
+ try:
207
+ await cognee.prune.prune_data()
208
+ await cognee.prune.prune_system(metadata=True)
209
+ from cognee.infrastructure.databases.graph.get_graph_engine import _create_graph_engine
210
+ from cognee.infrastructure.databases.vector.create_vector_engine import (
211
+ _create_vector_engine,
212
+ )
213
+ from cognee.infrastructure.databases.relational.create_relational_engine import (
214
+ create_relational_engine,
215
+ )
216
+
217
+ _create_graph_engine.cache_clear()
218
+ _create_vector_engine.cache_clear()
219
+ create_relational_engine.cache_clear()
220
+ except Exception:
221
+ pass
222
+
223
+
224
+ @pytest_asyncio.fixture
225
+ async def setup_test_environment_empty():
226
+ """Set up a clean test environment without chunks."""
227
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
228
+ system_directory_path = str(base_dir / ".cognee_system/test_chunks_retriever_context_empty")
229
+ data_directory_path = str(base_dir / ".data_storage/test_chunks_retriever_context_empty")
230
+
231
+ cognee.config.system_root_directory(system_directory_path)
232
+ cognee.config.data_root_directory(data_directory_path)
233
+
234
+ await cognee.prune.prune_data()
235
+ await cognee.prune.prune_system(metadata=True)
236
+ from cognee.infrastructure.databases.graph.get_graph_engine import _create_graph_engine
237
+ from cognee.infrastructure.databases.vector.create_vector_engine import (
238
+ _create_vector_engine,
239
+ )
240
+ from cognee.infrastructure.databases.relational.create_relational_engine import (
241
+ create_relational_engine,
242
+ )
243
+
244
+ _create_graph_engine.cache_clear()
245
+ _create_vector_engine.cache_clear()
246
+ create_relational_engine.cache_clear()
247
+
248
+ yield
249
+
250
+ try:
251
+ await cognee.prune.prune_data()
252
+ await cognee.prune.prune_system(metadata=True)
253
+ from cognee.infrastructure.databases.graph.get_graph_engine import _create_graph_engine
254
+ from cognee.infrastructure.databases.vector.create_vector_engine import (
255
+ _create_vector_engine,
256
+ )
257
+ from cognee.infrastructure.databases.relational.create_relational_engine import (
258
+ create_relational_engine,
259
+ )
260
+
261
+ _create_graph_engine.cache_clear()
262
+ _create_vector_engine.cache_clear()
263
+ create_relational_engine.cache_clear()
264
+ except Exception:
265
+ pass
266
+
267
+
268
+ @pytest.mark.asyncio
269
+ async def test_chunks_retriever_multiple_chunks(setup_test_environment_with_chunks_simple):
270
+ """Integration test: verify ChunksRetriever can retrieve multiple chunks."""
271
+ retriever = ChunksRetriever()
272
+ query = "Steve"
273
+ chunks = await retriever.get_retrieved_objects("Steve")
274
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=chunks)
275
+
276
+ completion = await retriever.get_completion_from_context(
277
+ query=query, retrieved_objects=chunks, context=context
278
+ )
279
+
280
+ assert isinstance(completion, list), "Retrieved objects should be a list"
281
+ assert len(completion) > 0, "Retrieved objects list should not be empty"
282
+ assert any(chunk["text"] == "Steve Rodger" for chunk in completion), (
283
+ "Failed to get Steve Rodger chunk"
284
+ )
285
+
286
+
287
+ @pytest.mark.asyncio
288
+ async def test_chunks_retriever_top_k_limit(setup_test_environment_with_chunks_complex):
289
+ """Integration test: verify ChunksRetriever respects top_k parameter."""
290
+ retriever = ChunksRetriever(top_k=2)
291
+ query = "Employee"
292
+
293
+ chunks = await retriever.get_retrieved_objects("Steve")
294
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=chunks)
295
+
296
+ completion = await retriever.get_completion_from_context(
297
+ query=query, retrieved_objects=chunks, context=context
298
+ )
299
+
300
+ assert isinstance(completion, list), "Context should be a list"
301
+ assert len(completion) <= 2, "Should respect top_k limit"
302
+
303
+
304
+ @pytest.mark.asyncio
305
+ async def test_chunks_retriever_context_complex(setup_test_environment_with_chunks_complex):
306
+ """Integration test: verify ChunksRetriever can retrieve chunk context (complex)."""
307
+ retriever = ChunksRetriever(top_k=20)
308
+ query = "Christina"
309
+
310
+ chunks = await retriever.get_retrieved_objects(query)
311
+
312
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=chunks)
313
+
314
+ assert context[0:15] == "Christina Mayer", "Failed to get Christina Mayer"
315
+
316
+
317
+ @pytest.mark.asyncio
318
+ async def test_chunks_retriever_on_empty_graph(setup_test_environment_empty):
319
+ """Integration test: verify ChunksRetriever handles empty graph correctly."""
320
+ retriever = ChunksRetriever()
321
+ query = "Christina Mayer"
322
+
323
+ vector_engine = get_vector_engine()
324
+ await vector_engine.create_collection(
325
+ "DocumentChunk_text", payload_schema=DocumentChunkWithEntities
326
+ )
327
+
328
+ chunks = await retriever.get_retrieved_objects(query)
329
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=chunks)
330
+
331
+ completion = await retriever.get_completion_from_context(
332
+ query=query, retrieved_objects=chunks, context=context
333
+ )
334
+ assert isinstance(completion, list), "Retrieved objects should be a list"
335
+ assert len(completion) == 0, "Found chunks when none should exist"
336
+
337
+
338
+ @pytest.mark.asyncio
339
+ async def test_chunks_retriever_context_on_empty_graph(setup_test_environment_empty):
340
+ """Integration test: verify ChunksRetriever context handles empty graph correctly."""
341
+ retriever = ChunksRetriever()
342
+ query = "Christina Mayer"
343
+
344
+ vector_engine = get_vector_engine()
345
+ await vector_engine.create_collection(
346
+ "DocumentChunk_text", payload_schema=DocumentChunkWithEntities
347
+ )
348
+
349
+ chunks = await retriever.get_retrieved_objects(query)
350
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=chunks)
351
+ assert context == "", "Found chunks when none should exist"
@@ -0,0 +1,276 @@
1
+ import os
2
+ import pytest
3
+ import pathlib
4
+ import pytest_asyncio
5
+ from typing import Optional, Union
6
+ import cognee
7
+
8
+ from cognee.low_level import setup, DataPoint
9
+ from cognee.tasks.storage import add_data_points
10
+ from cognee.modules.retrieval.graph_completion_retriever import GraphCompletionRetriever
11
+
12
+
13
+ @pytest_asyncio.fixture
14
+ async def setup_test_environment_simple():
15
+ """Set up a clean test environment with simple graph data."""
16
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
17
+ system_directory_path = str(base_dir / ".cognee_system/test_graph_completion_context_simple")
18
+ data_directory_path = str(base_dir / ".data_storage/test_graph_completion_context_simple")
19
+
20
+ cognee.config.system_root_directory(system_directory_path)
21
+ cognee.config.data_root_directory(data_directory_path)
22
+
23
+ await cognee.prune.prune_data()
24
+ await cognee.prune.prune_system(metadata=True)
25
+ await setup()
26
+
27
+ class Company(DataPoint):
28
+ name: str
29
+ description: str
30
+
31
+ class Person(DataPoint):
32
+ name: str
33
+ description: str
34
+ works_for: Company
35
+
36
+ company1 = Company(name="Figma", description="Figma is a company")
37
+ company2 = Company(name="Canva", description="Canvas is a company")
38
+ person1 = Person(
39
+ name="Steve Rodger",
40
+ description="This is description about Steve Rodger",
41
+ works_for=company1,
42
+ )
43
+ person2 = Person(
44
+ name="Ike Loma", description="This is description about Ike Loma", works_for=company1
45
+ )
46
+ person3 = Person(
47
+ name="Jason Statham",
48
+ description="This is description about Jason Statham",
49
+ works_for=company1,
50
+ )
51
+ person4 = Person(
52
+ name="Mike Broski",
53
+ description="This is description about Mike Broski",
54
+ works_for=company2,
55
+ )
56
+ person5 = Person(
57
+ name="Christina Mayer",
58
+ description="This is description about Christina Mayer",
59
+ works_for=company2,
60
+ )
61
+
62
+ entities = [company1, company2, person1, person2, person3, person4, person5]
63
+
64
+ await add_data_points(entities)
65
+
66
+ yield
67
+
68
+ try:
69
+ await cognee.prune.prune_data()
70
+ await cognee.prune.prune_system(metadata=True)
71
+ except Exception:
72
+ pass
73
+
74
+
75
+ @pytest_asyncio.fixture
76
+ async def setup_test_environment_complex():
77
+ """Set up a clean test environment with complex graph data."""
78
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
79
+ system_directory_path = str(base_dir / ".cognee_system/test_graph_completion_context_complex")
80
+ data_directory_path = str(base_dir / ".data_storage/test_graph_completion_context_complex")
81
+
82
+ cognee.config.system_root_directory(system_directory_path)
83
+ cognee.config.data_root_directory(data_directory_path)
84
+
85
+ await cognee.prune.prune_data()
86
+ await cognee.prune.prune_system(metadata=True)
87
+ await setup()
88
+
89
+ class Company(DataPoint):
90
+ name: str
91
+ metadata: dict = {"index_fields": ["name"]}
92
+
93
+ class Car(DataPoint):
94
+ brand: str
95
+ model: str
96
+ year: int
97
+
98
+ class Location(DataPoint):
99
+ country: str
100
+ city: str
101
+
102
+ class Home(DataPoint):
103
+ location: Location
104
+ rooms: int
105
+ sqm: int
106
+
107
+ class Person(DataPoint):
108
+ name: str
109
+ works_for: Company
110
+ owns: Optional[list[Union[Car, Home]]] = None
111
+
112
+ company1 = Company(name="Figma")
113
+ company2 = Company(name="Canva")
114
+
115
+ person1 = Person(name="Mike Rodger", works_for=company1)
116
+ person1.owns = [Car(brand="Toyota", model="Camry", year=2020)]
117
+
118
+ person2 = Person(name="Ike Loma", works_for=company1)
119
+ person2.owns = [
120
+ Car(brand="Tesla", model="Model S", year=2021),
121
+ Home(location=Location(country="USA", city="New York"), sqm=80, rooms=4),
122
+ ]
123
+
124
+ person3 = Person(name="Jason Statham", works_for=company1)
125
+
126
+ person4 = Person(name="Mike Broski", works_for=company2)
127
+ person4.owns = [Car(brand="Ford", model="Mustang", year=1978)]
128
+
129
+ person5 = Person(name="Christina Mayer", works_for=company2)
130
+ person5.owns = [Car(brand="Honda", model="Civic", year=2023)]
131
+
132
+ entities = [company1, company2, person1, person2, person3, person4, person5]
133
+
134
+ await add_data_points(entities)
135
+
136
+ yield
137
+
138
+ try:
139
+ await cognee.prune.prune_data()
140
+ await cognee.prune.prune_system(metadata=True)
141
+ except Exception:
142
+ pass
143
+
144
+
145
+ @pytest_asyncio.fixture
146
+ async def setup_test_environment_empty():
147
+ """Set up a clean test environment without graph data."""
148
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
149
+ system_directory_path = str(
150
+ base_dir / ".cognee_system/test_get_graph_completion_context_on_empty_graph"
151
+ )
152
+ data_directory_path = str(
153
+ base_dir / ".data_storage/test_get_graph_completion_context_on_empty_graph"
154
+ )
155
+
156
+ cognee.config.system_root_directory(system_directory_path)
157
+ cognee.config.data_root_directory(data_directory_path)
158
+
159
+ await cognee.prune.prune_data()
160
+ await cognee.prune.prune_system(metadata=True)
161
+ await setup()
162
+
163
+ yield
164
+
165
+ try:
166
+ await cognee.prune.prune_data()
167
+ await cognee.prune.prune_system(metadata=True)
168
+ except Exception:
169
+ pass
170
+
171
+
172
+ @pytest.mark.asyncio
173
+ async def test_graph_completion_context_simple(setup_test_environment_simple):
174
+ """Integration test: verify GraphCompletionRetriever can retrieve context (simple)."""
175
+ retriever = GraphCompletionRetriever()
176
+ query = "Who works at Canva?"
177
+
178
+ triplets = await retriever.get_retrieved_objects(query)
179
+
180
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
181
+
182
+ # Ensure the top-level sections are present
183
+ assert "Nodes:" in context, "Missing 'Nodes:' section in context"
184
+ assert "Connections:" in context, "Missing 'Connections:' section in context"
185
+
186
+ # --- Nodes headers ---
187
+ assert "Node: Steve Rodger" in context, "Missing node header for Steve Rodger"
188
+ assert "Node: Figma" in context, "Missing node header for Figma"
189
+ assert "Node: Ike Loma" in context, "Missing node header for Ike Loma"
190
+ assert "Node: Jason Statham" in context, "Missing node header for Jason Statham"
191
+ assert "Node: Mike Broski" in context, "Missing node header for Mike Broski"
192
+ assert "Node: Canva" in context, "Missing node header for Canva"
193
+ assert "Node: Christina Mayer" in context, "Missing node header for Christina Mayer"
194
+
195
+ # --- Node contents ---
196
+ assert (
197
+ "__node_content_start__\nThis is description about Steve Rodger\n__node_content_end__"
198
+ in context
199
+ ), "Description block for Steve Rodger altered"
200
+ assert "__node_content_start__\nFigma is a company\n__node_content_end__" in context, (
201
+ "Description block for Figma altered"
202
+ )
203
+ assert (
204
+ "__node_content_start__\nThis is description about Ike Loma\n__node_content_end__"
205
+ in context
206
+ ), "Description block for Ike Loma altered"
207
+ assert (
208
+ "__node_content_start__\nThis is description about Jason Statham\n__node_content_end__"
209
+ in context
210
+ ), "Description block for Jason Statham altered"
211
+ assert (
212
+ "__node_content_start__\nThis is description about Mike Broski\n__node_content_end__"
213
+ in context
214
+ ), "Description block for Mike Broski altered"
215
+ assert "__node_content_start__\nCanvas is a company\n__node_content_end__" in context, (
216
+ "Description block for Canva altered"
217
+ )
218
+ assert (
219
+ "__node_content_start__\nThis is description about Christina Mayer\n__node_content_end__"
220
+ in context
221
+ ), "Description block for Christina Mayer altered"
222
+
223
+ # --- Connections ---
224
+ assert "Steve Rodger --[works_for]--> Figma" in context, (
225
+ "Connection Steve Rodger→Figma missing or changed"
226
+ )
227
+ assert "Ike Loma --[works_for]--> Figma" in context, (
228
+ "Connection Ike Loma→Figma missing or changed"
229
+ )
230
+ assert "Jason Statham --[works_for]--> Figma" in context, (
231
+ "Connection Jason Statham→Figma missing or changed"
232
+ )
233
+ assert "Mike Broski --[works_for]--> Canva" in context, (
234
+ "Connection Mike Broski→Canva missing or changed"
235
+ )
236
+ assert "Christina Mayer --[works_for]--> Canva" in context, (
237
+ "Connection Christina Mayer→Canva missing or changed"
238
+ )
239
+
240
+
241
+ @pytest.mark.asyncio
242
+ async def test_graph_completion_context_complex(setup_test_environment_complex):
243
+ """Integration test: verify GraphCompletionRetriever can retrieve context (complex)."""
244
+ retriever = GraphCompletionRetriever(top_k=20)
245
+ query = "Who works at Figma?"
246
+
247
+ triplets = await retriever.get_retrieved_objects(query)
248
+
249
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
250
+
251
+ assert "Mike Rodger --[works_for]--> Figma" in context, "Failed to get Mike Rodger"
252
+ assert "Ike Loma --[works_for]--> Figma" in context, "Failed to get Ike Loma"
253
+ assert "Jason Statham --[works_for]--> Figma" in context, "Failed to get Jason Statham"
254
+
255
+
256
+ @pytest.mark.asyncio
257
+ async def test_get_graph_completion_context_on_empty_graph(setup_test_environment_empty):
258
+ """Integration test: verify GraphCompletionRetriever handles empty graph correctly."""
259
+ retriever = GraphCompletionRetriever()
260
+ query = "Who works at Figma?"
261
+
262
+ triplets = await retriever.get_retrieved_objects(query)
263
+
264
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
265
+ assert context == "", "Context should be empty on an empty graph"
266
+
267
+
268
+ @pytest.mark.asyncio
269
+ async def test_graph_completion_get_triplets_empty(setup_test_environment_empty):
270
+ """Integration test: verify GraphCompletionRetriever get_triplets handles empty graph."""
271
+ retriever = GraphCompletionRetriever()
272
+
273
+ triplets = await retriever.get_triplets("Who works at Figma?")
274
+
275
+ assert isinstance(triplets, list), "Triplets should be a list"
276
+ assert len(triplets) == 0, "Should return empty list on empty graph"