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,195 @@
1
+ import os
2
+ import pytest
3
+ import pathlib
4
+ import pytest_asyncio
5
+ import cognee
6
+
7
+ from cognee.low_level import setup
8
+ from cognee.tasks.storage import add_data_points
9
+ from cognee.infrastructure.databases.vector import get_vector_engine
10
+ from cognee.modules.chunking.models import DocumentChunk
11
+ from cognee.tasks.summarization.models import TextSummary
12
+ from cognee.modules.data.processing.document_types import TextDocument
13
+ from cognee.modules.retrieval.exceptions.exceptions import NoDataError
14
+ from cognee.modules.retrieval.summaries_retriever import SummariesRetriever
15
+
16
+
17
+ @pytest_asyncio.fixture
18
+ async def setup_test_environment_with_summaries():
19
+ """Set up a clean test environment with summaries."""
20
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
21
+ system_directory_path = str(base_dir / ".cognee_system/test_summaries_retriever_context")
22
+ data_directory_path = str(base_dir / ".data_storage/test_summaries_retriever_context")
23
+
24
+ cognee.config.system_root_directory(system_directory_path)
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
+ document1 = TextDocument(
32
+ name="Employee List",
33
+ raw_data_location="somewhere",
34
+ external_metadata="",
35
+ mime_type="text/plain",
36
+ )
37
+
38
+ document2 = TextDocument(
39
+ name="Car List",
40
+ raw_data_location="somewhere",
41
+ external_metadata="",
42
+ mime_type="text/plain",
43
+ )
44
+
45
+ chunk1 = DocumentChunk(
46
+ text="Steve Rodger",
47
+ chunk_size=2,
48
+ chunk_index=0,
49
+ cut_type="sentence_end",
50
+ is_part_of=document1,
51
+ contains=[],
52
+ )
53
+ chunk1_summary = TextSummary(
54
+ text="S.R.",
55
+ made_from=chunk1,
56
+ )
57
+ chunk2 = DocumentChunk(
58
+ text="Mike Broski",
59
+ chunk_size=2,
60
+ chunk_index=1,
61
+ cut_type="sentence_end",
62
+ is_part_of=document1,
63
+ contains=[],
64
+ )
65
+ chunk2_summary = TextSummary(
66
+ text="M.B.",
67
+ made_from=chunk2,
68
+ )
69
+ chunk3 = DocumentChunk(
70
+ text="Christina Mayer",
71
+ chunk_size=2,
72
+ chunk_index=2,
73
+ cut_type="sentence_end",
74
+ is_part_of=document1,
75
+ contains=[],
76
+ )
77
+ chunk3_summary = TextSummary(
78
+ text="C.M.",
79
+ made_from=chunk3,
80
+ )
81
+ chunk4 = DocumentChunk(
82
+ text="Range Rover",
83
+ chunk_size=2,
84
+ chunk_index=0,
85
+ cut_type="sentence_end",
86
+ is_part_of=document2,
87
+ contains=[],
88
+ )
89
+ chunk4_summary = TextSummary(
90
+ text="R.R.",
91
+ made_from=chunk4,
92
+ )
93
+ chunk5 = DocumentChunk(
94
+ text="Hyundai",
95
+ chunk_size=2,
96
+ chunk_index=1,
97
+ cut_type="sentence_end",
98
+ is_part_of=document2,
99
+ contains=[],
100
+ )
101
+ chunk5_summary = TextSummary(
102
+ text="H.Y.",
103
+ made_from=chunk5,
104
+ )
105
+ chunk6 = DocumentChunk(
106
+ text="Chrysler",
107
+ chunk_size=2,
108
+ chunk_index=2,
109
+ cut_type="sentence_end",
110
+ is_part_of=document2,
111
+ contains=[],
112
+ )
113
+ chunk6_summary = TextSummary(
114
+ text="C.H.",
115
+ made_from=chunk6,
116
+ )
117
+
118
+ entities = [
119
+ chunk1_summary,
120
+ chunk2_summary,
121
+ chunk3_summary,
122
+ chunk4_summary,
123
+ chunk5_summary,
124
+ chunk6_summary,
125
+ ]
126
+
127
+ await add_data_points(entities)
128
+
129
+ yield
130
+
131
+ try:
132
+ await cognee.prune.prune_data()
133
+ await cognee.prune.prune_system(metadata=True)
134
+ except Exception:
135
+ pass
136
+
137
+
138
+ @pytest_asyncio.fixture
139
+ async def setup_test_environment_empty():
140
+ """Set up a clean test environment without summaries."""
141
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
142
+ system_directory_path = str(base_dir / ".cognee_system/test_summaries_retriever_context_empty")
143
+ data_directory_path = str(base_dir / ".data_storage/test_summaries_retriever_context_empty")
144
+
145
+ cognee.config.system_root_directory(system_directory_path)
146
+ cognee.config.data_root_directory(data_directory_path)
147
+
148
+ await cognee.prune.prune_data()
149
+ await cognee.prune.prune_system(metadata=True)
150
+
151
+ yield
152
+
153
+ try:
154
+ await cognee.prune.prune_data()
155
+ await cognee.prune.prune_system(metadata=True)
156
+ except Exception:
157
+ pass
158
+
159
+
160
+ @pytest.mark.asyncio
161
+ async def test_summaries_retriever(setup_test_environment_with_summaries):
162
+ """Integration test: verify SummariesRetriever can retrieve summary context."""
163
+ retriever = SummariesRetriever(top_k=20)
164
+ query = "Christina"
165
+ summaries = await retriever.get_retrieved_objects(query)
166
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=summaries)
167
+
168
+ completion = await retriever.get_completion_from_context(
169
+ query=query, retrieved_objects=summaries, context=context
170
+ )
171
+
172
+ assert isinstance(completion, list), "Context should be a list"
173
+ assert len(completion) > 0, "Context should not be empty"
174
+ assert completion[0]["text"] == "C.M.", "Failed to get Christina Mayer"
175
+
176
+
177
+ @pytest.mark.asyncio
178
+ async def test_summaries_retriever_on_empty_graph(setup_test_environment_empty):
179
+ """Integration test: verify SummariesRetriever handles empty graph correctly."""
180
+ retriever = SummariesRetriever()
181
+ query = "Christina Mayer"
182
+
183
+ with pytest.raises(NoDataError):
184
+ await retriever.get_retrieved_objects(query)
185
+
186
+ vector_engine = get_vector_engine()
187
+ await vector_engine.create_collection("TextSummary_text", payload_schema=TextSummary)
188
+
189
+ summaries = await retriever.get_retrieved_objects(query)
190
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=summaries)
191
+ completion = await retriever.get_completion_from_context(
192
+ query=query, retrieved_objects=summaries, context=context
193
+ )
194
+
195
+ assert completion == [], "Returned context should be empty on an empty graph"
@@ -0,0 +1,336 @@
1
+ import os
2
+ import pytest
3
+ import pathlib
4
+ import pytest_asyncio
5
+ import cognee
6
+
7
+ from cognee.low_level import setup, DataPoint
8
+ from cognee.tasks.storage import add_data_points
9
+ from cognee.modules.retrieval.temporal_retriever import TemporalRetriever
10
+ from cognee.modules.engine.models.Event import Event
11
+ from cognee.modules.engine.models.Timestamp import Timestamp
12
+ from cognee.modules.engine.models.Interval import Interval
13
+
14
+
15
+ @pytest_asyncio.fixture
16
+ async def setup_test_environment_with_events():
17
+ """Set up a clean test environment with temporal events."""
18
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
19
+ system_directory_path = str(base_dir / ".cognee_system/test_temporal_retriever_with_events")
20
+ data_directory_path = str(base_dir / ".data_storage/test_temporal_retriever_with_events")
21
+
22
+ cognee.config.system_root_directory(system_directory_path)
23
+ cognee.config.data_root_directory(data_directory_path)
24
+
25
+ await cognee.prune.prune_data()
26
+ await cognee.prune.prune_system(metadata=True)
27
+ await setup()
28
+
29
+ # Create timestamps for events
30
+ timestamp1 = Timestamp(
31
+ time_at=1609459200, # 2021-01-01 00:00:00
32
+ year=2021,
33
+ month=1,
34
+ day=1,
35
+ hour=0,
36
+ minute=0,
37
+ second=0,
38
+ timestamp_str="2021-01-01T00:00:00",
39
+ )
40
+
41
+ timestamp2 = Timestamp(
42
+ time_at=1612137600, # 2021-02-01 00:00:00
43
+ year=2021,
44
+ month=2,
45
+ day=1,
46
+ hour=0,
47
+ minute=0,
48
+ second=0,
49
+ timestamp_str="2021-02-01T00:00:00",
50
+ )
51
+
52
+ timestamp3 = Timestamp(
53
+ time_at=1614556800, # 2021-03-01 00:00:00
54
+ year=2021,
55
+ month=3,
56
+ day=1,
57
+ hour=0,
58
+ minute=0,
59
+ second=0,
60
+ timestamp_str="2021-03-01T00:00:00",
61
+ )
62
+
63
+ timestamp4 = Timestamp(
64
+ time_at=1625097600, # 2021-07-01 00:00:00
65
+ year=2021,
66
+ month=7,
67
+ day=1,
68
+ hour=0,
69
+ minute=0,
70
+ second=0,
71
+ timestamp_str="2021-07-01T00:00:00",
72
+ )
73
+
74
+ timestamp5 = Timestamp(
75
+ time_at=1633046400, # 2021-10-01 00:00:00
76
+ year=2021,
77
+ month=10,
78
+ day=1,
79
+ hour=0,
80
+ minute=0,
81
+ second=0,
82
+ timestamp_str="2021-10-01T00:00:00",
83
+ )
84
+
85
+ # Create interval for event spanning multiple timestamps
86
+ interval1 = Interval(time_from=timestamp2, time_to=timestamp3)
87
+
88
+ # Create events with timestamps
89
+ event1 = Event(
90
+ name="Project Alpha Launch",
91
+ description="Launched Project Alpha at the beginning of 2021",
92
+ at=timestamp1,
93
+ location="San Francisco",
94
+ )
95
+
96
+ event2 = Event(
97
+ name="Team Meeting",
98
+ description="Monthly team meeting discussing Q1 goals",
99
+ during=interval1,
100
+ location="New York",
101
+ )
102
+
103
+ event3 = Event(
104
+ name="Product Release",
105
+ description="Released new product features in July",
106
+ at=timestamp4,
107
+ location="Remote",
108
+ )
109
+
110
+ event4 = Event(
111
+ name="Company Retreat",
112
+ description="Annual company retreat in October",
113
+ at=timestamp5,
114
+ location="Lake Tahoe",
115
+ )
116
+
117
+ entities = [event1, event2, event3, event4]
118
+
119
+ await add_data_points(entities)
120
+
121
+ yield
122
+
123
+ try:
124
+ await cognee.prune.prune_data()
125
+ await cognee.prune.prune_system(metadata=True)
126
+ except Exception:
127
+ pass
128
+
129
+
130
+ @pytest_asyncio.fixture
131
+ async def setup_test_environment_with_graph_data():
132
+ """Set up a clean test environment with graph data (for fallback to triplets)."""
133
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
134
+ system_directory_path = str(base_dir / ".cognee_system/test_temporal_retriever_with_graph")
135
+ data_directory_path = str(base_dir / ".data_storage/test_temporal_retriever_with_graph")
136
+
137
+ cognee.config.system_root_directory(system_directory_path)
138
+ cognee.config.data_root_directory(data_directory_path)
139
+
140
+ await cognee.prune.prune_data()
141
+ await cognee.prune.prune_system(metadata=True)
142
+ await setup()
143
+
144
+ class Company(DataPoint):
145
+ name: str
146
+ description: str
147
+
148
+ class Person(DataPoint):
149
+ name: str
150
+ description: str
151
+ works_for: Company
152
+
153
+ company1 = Company(name="Figma", description="Figma is a company")
154
+ person1 = Person(
155
+ name="Steve Rodger",
156
+ description="This is description about Steve Rodger",
157
+ works_for=company1,
158
+ )
159
+
160
+ entities = [company1, person1]
161
+
162
+ await add_data_points(entities)
163
+
164
+ yield
165
+
166
+ try:
167
+ await cognee.prune.prune_data()
168
+ await cognee.prune.prune_system(metadata=True)
169
+ except Exception:
170
+ pass
171
+
172
+
173
+ @pytest_asyncio.fixture
174
+ async def setup_test_environment_empty():
175
+ """Set up a clean test environment without data."""
176
+ base_dir = pathlib.Path(__file__).parent.parent.parent.parent
177
+ system_directory_path = str(base_dir / ".cognee_system/test_temporal_retriever_empty")
178
+ data_directory_path = str(base_dir / ".data_storage/test_temporal_retriever_empty")
179
+
180
+ cognee.config.system_root_directory(system_directory_path)
181
+ cognee.config.data_root_directory(data_directory_path)
182
+
183
+ await cognee.prune.prune_data()
184
+ await cognee.prune.prune_system(metadata=True)
185
+ await setup()
186
+
187
+ yield
188
+
189
+ try:
190
+ await cognee.prune.prune_data()
191
+ await cognee.prune.prune_system(metadata=True)
192
+ except Exception:
193
+ pass
194
+
195
+
196
+ @pytest.mark.asyncio
197
+ async def test_temporal_retriever_context_with_time_range(setup_test_environment_with_events):
198
+ """Integration test: verify TemporalRetriever can retrieve events within time range."""
199
+ retriever = TemporalRetriever(top_k=5)
200
+ query = "What happened in January 2021?"
201
+
202
+ triplets = await retriever.get_retrieved_objects(query)
203
+
204
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
205
+
206
+ assert isinstance(context, str), "Context should be a string"
207
+ assert len(context) > 0, "Context should not be empty"
208
+ assert "Project Alpha" in context or "Launch" in context, (
209
+ "Should retrieve Project Alpha Launch event from January 2021"
210
+ )
211
+
212
+
213
+ @pytest.mark.asyncio
214
+ async def test_temporal_retriever_context_with_single_time(setup_test_environment_with_events):
215
+ """Integration test: verify TemporalRetriever can retrieve events at specific time."""
216
+ retriever = TemporalRetriever(top_k=5)
217
+ query = "What happened in July 2021?"
218
+
219
+ triplets = await retriever.get_retrieved_objects(query)
220
+
221
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
222
+
223
+ assert isinstance(context, str), "Context should be a string"
224
+ assert len(context) > 0, "Context should not be empty"
225
+ assert "Product Release" in context or "July" in context, (
226
+ "Should retrieve Product Release event from July 2021"
227
+ )
228
+
229
+
230
+ @pytest.mark.asyncio
231
+ async def test_temporal_retriever_context_fallback_to_triplets(
232
+ setup_test_environment_with_graph_data,
233
+ ):
234
+ """Integration test: verify TemporalRetriever falls back to triplets when no time extracted."""
235
+ retriever = TemporalRetriever(top_k=5)
236
+ query = "Who works at Figma?"
237
+
238
+ triplets = await retriever.get_retrieved_objects(query)
239
+
240
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
241
+
242
+ assert isinstance(context, str), "Context should be a string"
243
+ assert len(context) > 0, "Context should not be empty"
244
+ assert "Steve" in context or "Figma" in context, (
245
+ "Should retrieve graph data via triplet search fallback"
246
+ )
247
+
248
+
249
+ @pytest.mark.asyncio
250
+ async def test_temporal_retriever_context_empty_graph(setup_test_environment_empty):
251
+ """Integration test: verify TemporalRetriever handles empty graph correctly."""
252
+ retriever = TemporalRetriever()
253
+ query = "What happened?"
254
+
255
+ triplets = await retriever.get_retrieved_objects(query)
256
+
257
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
258
+
259
+ assert isinstance(context, str), "Context should be a string"
260
+ assert len(context) >= 0, "Context should be a string (possibly empty)"
261
+
262
+
263
+ @pytest.mark.asyncio
264
+ async def test_temporal_retriever_get_completion(setup_test_environment_with_events):
265
+ """Integration test: verify TemporalRetriever can generate completions."""
266
+ retriever = TemporalRetriever()
267
+ query = "What happened in January 2021?"
268
+
269
+ triplets = await retriever.get_retrieved_objects(query)
270
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
271
+
272
+ completion = await retriever.get_completion_from_context(
273
+ query=query, retrieved_objects=triplets, context=context
274
+ )
275
+
276
+ assert isinstance(completion, list), "Completion should be a list"
277
+ assert len(completion) > 0, "Completion should not be empty"
278
+ assert all(isinstance(item, str) and item.strip() for item in completion), (
279
+ "Completion items should be non-empty strings"
280
+ )
281
+
282
+
283
+ @pytest.mark.asyncio
284
+ async def test_temporal_retriever_get_completion_fallback(setup_test_environment_with_graph_data):
285
+ """Integration test: verify TemporalRetriever get_completion works with triplet fallback."""
286
+ retriever = TemporalRetriever()
287
+ query = "Who works at Figma?"
288
+
289
+ triplets = await retriever.get_retrieved_objects(query)
290
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
291
+
292
+ completion = await retriever.get_completion_from_context(
293
+ query=query, retrieved_objects=triplets, context=context
294
+ )
295
+
296
+ assert isinstance(completion, list), "Completion should be a list"
297
+ assert len(completion) > 0, "Completion should not be empty"
298
+ assert all(isinstance(item, str) and item.strip() for item in completion), (
299
+ "Completion items should be non-empty strings"
300
+ )
301
+
302
+
303
+ @pytest.mark.asyncio
304
+ async def test_temporal_retriever_top_k_limit(setup_test_environment_with_events):
305
+ """Integration test: verify TemporalRetriever respects top_k parameter."""
306
+ retriever = TemporalRetriever(top_k=2)
307
+ query = "What happened in 2021?"
308
+
309
+ triplets = await retriever.get_retrieved_objects(query)
310
+
311
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
312
+
313
+ assert isinstance(context, str), "Context should be a string"
314
+ separator_count = context.count("#####################")
315
+ assert separator_count <= 1, "Should respect top_k limit of 2 events"
316
+
317
+
318
+ @pytest.mark.asyncio
319
+ async def test_temporal_retriever_multiple_events(setup_test_environment_with_events):
320
+ """Integration test: verify TemporalRetriever can retrieve multiple events."""
321
+ retriever = TemporalRetriever(top_k=10)
322
+ query = "What events occurred in 2021?"
323
+
324
+ triplets = await retriever.get_retrieved_objects(query)
325
+
326
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
327
+
328
+ assert isinstance(context, str), "Context should be a string"
329
+ assert len(context) > 0, "Context should not be empty"
330
+
331
+ assert (
332
+ "Project Alpha" in context
333
+ or "Team Meeting" in context
334
+ or "Product Release" in context
335
+ or "Company Retreat" in context
336
+ ), "Should retrieve at least one event from 2021"
@@ -78,7 +78,51 @@ async def setup_test_environment_empty():
78
78
  async def test_triplet_retriever_context_simple(setup_test_environment_with_triplets):
79
79
  """Integration test: verify TripletRetriever can retrieve triplet context."""
80
80
  retriever = TripletRetriever(top_k=5)
81
+ query = "Alice"
81
82
 
82
- context = await retriever.get_context("Alice")
83
+ triplets = await retriever.get_retrieved_objects(query)
84
+
85
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
83
86
 
84
87
  assert "Alice knows Bob" in context, "Failed to get Alice triplet"
88
+ assert isinstance(context, str), "Context should be a string"
89
+ assert len(context) > 0, "Context should not be empty"
90
+
91
+
92
+ @pytest.mark.asyncio
93
+ async def test_triplet_retriever_context_multiple_triplets(setup_test_environment_with_triplets):
94
+ """Integration test: verify TripletRetriever can retrieve multiple triplets."""
95
+ retriever = TripletRetriever(top_k=5)
96
+ query = "Bob"
97
+
98
+ triplets = await retriever.get_retrieved_objects(query)
99
+
100
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
101
+
102
+ assert "Alice knows Bob" in context or "Bob works at Tech Corp" in context, (
103
+ "Failed to get Bob-related triplets"
104
+ )
105
+
106
+
107
+ @pytest.mark.asyncio
108
+ async def test_triplet_retriever_top_k_limit(setup_test_environment_with_triplets):
109
+ """Integration test: verify TripletRetriever respects top_k parameter."""
110
+ retriever = TripletRetriever(top_k=1)
111
+ query = "Alice"
112
+
113
+ triplets = await retriever.get_retrieved_objects(query)
114
+
115
+ context = await retriever.get_context_from_objects(query=query, retrieved_objects=triplets)
116
+
117
+ assert isinstance(context, str), "Context should be a string"
118
+
119
+
120
+ @pytest.mark.asyncio
121
+ async def test_triplet_retriever_context_empty(setup_test_environment_empty):
122
+ """Integration test: verify TripletRetriever handles empty graph correctly."""
123
+ await setup()
124
+
125
+ retriever = TripletRetriever()
126
+
127
+ with pytest.raises(NoDataError):
128
+ await retriever.get_retrieved_objects("Alice")