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
@@ -1,205 +1,334 @@
1
- import os
2
- from typing import List
3
1
  import pytest
4
- import pathlib
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.modules.data.processing.document_types import TextDocument
12
- from cognee.modules.retrieval.exceptions.exceptions import NoDataError
2
+ from unittest.mock import AsyncMock, patch, MagicMock
3
+
13
4
  from cognee.modules.retrieval.completion_retriever import CompletionRetriever
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
5
+ from cognee.modules.retrieval.exceptions.exceptions import NoDataError
6
+ from cognee.infrastructure.databases.vector.exceptions import CollectionNotFoundError
17
7
 
18
8
 
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
9
+ @pytest.fixture
10
+ def mock_vector_engine():
11
+ """Create a mock vector engine."""
12
+ engine = AsyncMock()
13
+ engine.search = AsyncMock()
14
+ return engine
26
15
 
27
- metadata: dict = {"index_fields": ["text"]}
28
16
 
17
+ @pytest.mark.asyncio
18
+ async def test_get_context_success(mock_vector_engine):
19
+ """Test successful retrieval of context."""
20
+ mock_result1 = MagicMock()
21
+ mock_result1.payload = {"text": "Steve Rodger"}
22
+ mock_result2 = MagicMock()
23
+ mock_result2.payload = {"text": "Mike Broski"}
29
24
 
30
- class TestRAGCompletionRetriever:
31
- @pytest.mark.asyncio
32
- async def test_rag_completion_context_simple(self):
33
- system_directory_path = os.path.join(
34
- pathlib.Path(__file__).parent, ".cognee_system/test_rag_completion_context_simple"
35
- )
36
- cognee.config.system_root_directory(system_directory_path)
37
- data_directory_path = os.path.join(
38
- pathlib.Path(__file__).parent, ".data_storage/test_rag_completion_context_simple"
39
- )
40
- cognee.config.data_root_directory(data_directory_path)
25
+ mock_vector_engine.search.return_value = [mock_result1, mock_result2]
41
26
 
42
- await cognee.prune.prune_data()
43
- await cognee.prune.prune_system(metadata=True)
44
- await setup()
27
+ retriever = CompletionRetriever(top_k=2)
45
28
 
46
- document = TextDocument(
47
- name="Steve Rodger's career",
48
- raw_data_location="somewhere",
49
- external_metadata="",
50
- mime_type="text/plain",
51
- )
29
+ with patch(
30
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
31
+ return_value=mock_vector_engine,
32
+ ):
33
+ objects = await retriever.get_retrieved_objects("test query")
34
+ context = await retriever.get_context_from_objects("test query", objects)
52
35
 
53
- chunk1 = DocumentChunk(
54
- text="Steve Rodger",
55
- chunk_size=2,
56
- chunk_index=0,
57
- cut_type="sentence_end",
58
- is_part_of=document,
59
- contains=[],
60
- )
61
- chunk2 = DocumentChunk(
62
- text="Mike Broski",
63
- chunk_size=2,
64
- chunk_index=1,
65
- cut_type="sentence_end",
66
- is_part_of=document,
67
- contains=[],
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=document,
75
- contains=[],
76
- )
36
+ assert context == "Steve Rodger\nMike Broski"
37
+ mock_vector_engine.search.assert_awaited_once_with(
38
+ "DocumentChunk_text", "test query", limit=2, include_payload=True
39
+ )
77
40
 
78
- entities = [chunk1, chunk2, chunk3]
79
41
 
80
- await add_data_points(entities)
42
+ @pytest.mark.asyncio
43
+ async def test_get_context_collection_not_found_error(mock_vector_engine):
44
+ """Test that CollectionNotFoundError is converted to NoDataError."""
45
+ mock_vector_engine.search.side_effect = CollectionNotFoundError("Collection not found")
81
46
 
82
- retriever = CompletionRetriever()
47
+ retriever = CompletionRetriever()
83
48
 
84
- context = await retriever.get_context("Mike")
49
+ with patch(
50
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
51
+ return_value=mock_vector_engine,
52
+ ):
53
+ with pytest.raises(NoDataError, match="No data found"):
54
+ await retriever.get_retrieved_objects("test query")
85
55
 
86
- assert context == "Mike Broski", "Failed to get Mike Broski"
87
56
 
88
- @pytest.mark.asyncio
89
- async def test_rag_completion_context_complex(self):
90
- system_directory_path = os.path.join(
91
- pathlib.Path(__file__).parent, ".cognee_system/test_rag_completion_context_complex"
92
- )
93
- cognee.config.system_root_directory(system_directory_path)
94
- data_directory_path = os.path.join(
95
- pathlib.Path(__file__).parent, ".data_storage/test_rag_completion_context_complex"
96
- )
97
- cognee.config.data_root_directory(data_directory_path)
57
+ @pytest.mark.asyncio
58
+ async def test_get_context_empty_results(mock_vector_engine):
59
+ """Test that empty string is returned when no chunks are found."""
60
+ mock_vector_engine.search.return_value = []
98
61
 
99
- await cognee.prune.prune_data()
100
- await cognee.prune.prune_system(metadata=True)
101
- await setup()
62
+ retriever = CompletionRetriever()
102
63
 
103
- document1 = TextDocument(
104
- name="Employee List",
105
- raw_data_location="somewhere",
106
- external_metadata="",
107
- mime_type="text/plain",
108
- )
64
+ with patch(
65
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
66
+ return_value=mock_vector_engine,
67
+ ):
68
+ context = await retriever.get_context_from_objects("test query", [])
109
69
 
110
- document2 = TextDocument(
111
- name="Car List",
112
- raw_data_location="somewhere",
113
- external_metadata="",
114
- mime_type="text/plain",
115
- )
70
+ assert context == ""
116
71
 
117
- chunk1 = DocumentChunk(
118
- text="Steve Rodger",
119
- chunk_size=2,
120
- chunk_index=0,
121
- cut_type="sentence_end",
122
- is_part_of=document1,
123
- contains=[],
124
- )
125
- chunk2 = DocumentChunk(
126
- text="Mike Broski",
127
- chunk_size=2,
128
- chunk_index=1,
129
- cut_type="sentence_end",
130
- is_part_of=document1,
131
- contains=[],
132
- )
133
- chunk3 = DocumentChunk(
134
- text="Christina Mayer",
135
- chunk_size=2,
136
- chunk_index=2,
137
- cut_type="sentence_end",
138
- is_part_of=document1,
139
- contains=[],
140
- )
141
72
 
142
- chunk4 = DocumentChunk(
143
- text="Range Rover",
144
- chunk_size=2,
145
- chunk_index=0,
146
- cut_type="sentence_end",
147
- is_part_of=document2,
148
- contains=[],
149
- )
150
- chunk5 = DocumentChunk(
151
- text="Hyundai",
152
- chunk_size=2,
153
- chunk_index=1,
154
- cut_type="sentence_end",
155
- is_part_of=document2,
156
- contains=[],
157
- )
158
- chunk6 = DocumentChunk(
159
- text="Chrysler",
160
- chunk_size=2,
161
- chunk_index=2,
162
- cut_type="sentence_end",
163
- is_part_of=document2,
164
- contains=[],
165
- )
73
+ @pytest.mark.asyncio
74
+ async def test_get_context_top_k_limit(mock_vector_engine):
75
+ """Test that top_k parameter limits the number of results."""
76
+ mock_results = [MagicMock() for _ in range(2)]
77
+ for i, result in enumerate(mock_results):
78
+ result.payload = {"text": f"Chunk {i}"}
166
79
 
167
- entities = [chunk1, chunk2, chunk3, chunk4, chunk5, chunk6]
80
+ mock_vector_engine.search.return_value = mock_results
168
81
 
169
- await add_data_points(entities)
82
+ retriever = CompletionRetriever(top_k=2)
170
83
 
171
- # TODO: top_k doesn't affect the output, it should be fixed.
172
- retriever = CompletionRetriever(top_k=20)
84
+ with patch(
85
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
86
+ return_value=mock_vector_engine,
87
+ ):
88
+ objects = await retriever.get_retrieved_objects("test query")
89
+ context = await retriever.get_context_from_objects("test query", objects)
173
90
 
174
- context = await retriever.get_context("Christina")
91
+ assert context == "Chunk 0\nChunk 1"
92
+ mock_vector_engine.search.assert_awaited_once_with(
93
+ "DocumentChunk_text", "test query", limit=2, include_payload=True
94
+ )
175
95
 
176
- assert context[0:15] == "Christina Mayer", "Failed to get Christina Mayer"
177
96
 
178
- @pytest.mark.asyncio
179
- async def test_get_rag_completion_context_on_empty_graph(self):
180
- system_directory_path = os.path.join(
181
- pathlib.Path(__file__).parent,
182
- ".cognee_system/test_get_rag_completion_context_on_empty_graph",
183
- )
184
- cognee.config.system_root_directory(system_directory_path)
185
- data_directory_path = os.path.join(
186
- pathlib.Path(__file__).parent,
187
- ".data_storage/test_get_rag_completion_context_on_empty_graph",
97
+ @pytest.mark.asyncio
98
+ async def test_get_context_single_chunk(mock_vector_engine):
99
+ """Test get_context with single chunk result."""
100
+ mock_result = MagicMock()
101
+ mock_result.payload = {"text": "Single chunk text"}
102
+ mock_vector_engine.search.return_value = [mock_result]
103
+
104
+ retriever = CompletionRetriever()
105
+
106
+ with patch(
107
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
108
+ return_value=mock_vector_engine,
109
+ ):
110
+ objects = await retriever.get_retrieved_objects("test query")
111
+ context = await retriever.get_context_from_objects("test query", objects)
112
+
113
+ assert context == "Single chunk text"
114
+
115
+
116
+ @pytest.mark.asyncio
117
+ async def test_get_completion_without_session(mock_vector_engine):
118
+ """Test get_completion without session caching."""
119
+ mock_result = MagicMock()
120
+ mock_result.payload = {"text": "Chunk text"}
121
+ mock_vector_engine.search.return_value = [mock_result]
122
+
123
+ retriever = CompletionRetriever()
124
+
125
+ with (
126
+ patch(
127
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
128
+ return_value=mock_vector_engine,
129
+ ),
130
+ patch(
131
+ "cognee.modules.retrieval.completion_retriever.generate_completion",
132
+ return_value="Generated answer",
133
+ ),
134
+ patch("cognee.modules.retrieval.completion_retriever.CacheConfig") as mock_cache_config,
135
+ ):
136
+ mock_config = MagicMock()
137
+ mock_config.caching = False
138
+ mock_cache_config.return_value = mock_config
139
+
140
+ completion = await retriever.get_completion_from_context("test query", None, "")
141
+
142
+ assert isinstance(completion, list)
143
+ assert len(completion) == 1
144
+ assert completion[0] == "Generated answer"
145
+
146
+
147
+ @pytest.mark.asyncio
148
+ async def test_get_completion_with_provided_context(mock_vector_engine):
149
+ """Test get_completion with provided context."""
150
+ retriever = CompletionRetriever()
151
+
152
+ with (
153
+ patch(
154
+ "cognee.modules.retrieval.completion_retriever.generate_completion",
155
+ return_value="Generated answer",
156
+ ),
157
+ patch("cognee.modules.retrieval.completion_retriever.CacheConfig") as mock_cache_config,
158
+ ):
159
+ mock_config = MagicMock()
160
+ mock_config.caching = False
161
+ mock_cache_config.return_value = mock_config
162
+
163
+ completion = await retriever.get_completion_from_context(
164
+ "test query", None, context="Provided context"
188
165
  )
189
- cognee.config.data_root_directory(data_directory_path)
190
166
 
191
- await cognee.prune.prune_data()
192
- await cognee.prune.prune_system(metadata=True)
167
+ assert isinstance(completion, list)
168
+ assert len(completion) == 1
169
+ assert completion[0] == "Generated answer"
170
+
171
+
172
+ @pytest.mark.asyncio
173
+ async def test_get_completion_with_session(mock_vector_engine):
174
+ """Test get_completion with session caching enabled."""
175
+ mock_result = MagicMock()
176
+ mock_result.payload = {"text": "Chunk text"}
177
+ mock_vector_engine.search.return_value = [mock_result]
193
178
 
194
- retriever = CompletionRetriever()
179
+ retriever = CompletionRetriever(session_id="test_session")
195
180
 
196
- with pytest.raises(NoDataError):
197
- await retriever.get_context("Christina Mayer")
181
+ mock_user = MagicMock()
182
+ mock_user.id = "test-user-id"
198
183
 
199
- vector_engine = get_vector_engine()
200
- await vector_engine.create_collection(
201
- "DocumentChunk_text", payload_schema=DocumentChunkWithEntities
184
+ with (
185
+ patch(
186
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
187
+ return_value=mock_vector_engine,
188
+ ),
189
+ patch(
190
+ "cognee.modules.retrieval.completion_retriever.get_conversation_history",
191
+ return_value="Previous conversation",
192
+ ),
193
+ patch(
194
+ "cognee.modules.retrieval.completion_retriever.summarize_text",
195
+ return_value="Context summary",
196
+ ),
197
+ patch(
198
+ "cognee.modules.retrieval.completion_retriever.generate_completion",
199
+ return_value="Generated answer",
200
+ ),
201
+ patch(
202
+ "cognee.modules.retrieval.completion_retriever.save_conversation_history",
203
+ ) as mock_save,
204
+ patch("cognee.modules.retrieval.completion_retriever.CacheConfig") as mock_cache_config,
205
+ patch("cognee.modules.retrieval.completion_retriever.session_user") as mock_session_user,
206
+ ):
207
+ mock_config = MagicMock()
208
+ mock_config.caching = True
209
+ mock_cache_config.return_value = mock_config
210
+ mock_session_user.get.return_value = mock_user
211
+
212
+ completion = await retriever.get_completion_from_context(
213
+ "test query", [mock_result], "test"
202
214
  )
203
215
 
204
- context = await retriever.get_context("Christina Mayer")
205
- assert context == "", "Returned context should be empty on an empty graph"
216
+ assert isinstance(completion, list)
217
+ assert len(completion) == 1
218
+ assert completion[0] == "Generated answer"
219
+ mock_save.assert_awaited_once()
220
+
221
+
222
+ @pytest.mark.asyncio
223
+ async def test_get_completion_with_session_no_user_id(mock_vector_engine):
224
+ """Test get_completion with session config but no user ID."""
225
+ mock_result = MagicMock()
226
+ mock_result.payload = {"text": "Chunk text"}
227
+ mock_vector_engine.search.return_value = [mock_result]
228
+
229
+ retriever = CompletionRetriever()
230
+
231
+ with (
232
+ patch(
233
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
234
+ return_value=mock_vector_engine,
235
+ ),
236
+ patch(
237
+ "cognee.modules.retrieval.completion_retriever.generate_completion",
238
+ return_value="Generated answer",
239
+ ),
240
+ patch("cognee.modules.retrieval.completion_retriever.CacheConfig") as mock_cache_config,
241
+ patch("cognee.modules.retrieval.completion_retriever.session_user") as mock_session_user,
242
+ ):
243
+ mock_config = MagicMock()
244
+ mock_config.caching = True
245
+ mock_cache_config.return_value = mock_config
246
+ mock_session_user.get.return_value = None # No user
247
+
248
+ completion = await retriever.get_completion_from_context("test query", None, "")
249
+
250
+ assert isinstance(completion, list)
251
+ assert len(completion) == 1
252
+
253
+
254
+ @pytest.mark.asyncio
255
+ async def test_get_completion_with_response_model(mock_vector_engine):
256
+ """Test get_completion with custom response model."""
257
+ from pydantic import BaseModel
258
+
259
+ class TestModel(BaseModel):
260
+ answer: str
261
+
262
+ mock_result = MagicMock()
263
+ mock_result.payload = {"text": "Chunk text"}
264
+ mock_vector_engine.search.return_value = [mock_result]
265
+
266
+ retriever = CompletionRetriever(response_model=TestModel)
267
+
268
+ with (
269
+ patch(
270
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
271
+ return_value=mock_vector_engine,
272
+ ),
273
+ patch(
274
+ "cognee.modules.retrieval.completion_retriever.generate_completion",
275
+ return_value=TestModel(answer="Test answer"),
276
+ ),
277
+ patch("cognee.modules.retrieval.completion_retriever.CacheConfig") as mock_cache_config,
278
+ ):
279
+ mock_config = MagicMock()
280
+ mock_config.caching = False
281
+ mock_cache_config.return_value = mock_config
282
+
283
+ completion = await retriever.get_completion_from_context("test query", None, None)
284
+
285
+ assert isinstance(completion, list)
286
+ assert len(completion) == 1
287
+ assert isinstance(completion[0], TestModel)
288
+
289
+
290
+ @pytest.mark.asyncio
291
+ async def test_init_defaults():
292
+ """Test CompletionRetriever initialization with defaults."""
293
+ retriever = CompletionRetriever()
294
+
295
+ assert retriever.user_prompt_path == "context_for_question.txt"
296
+ assert retriever.system_prompt_path == "answer_simple_question.txt"
297
+ assert retriever.top_k == 1
298
+ assert retriever.system_prompt is None
299
+
300
+
301
+ @pytest.mark.asyncio
302
+ async def test_init_custom_params():
303
+ """Test CompletionRetriever initialization with custom parameters."""
304
+ retriever = CompletionRetriever(
305
+ user_prompt_path="custom_user.txt",
306
+ system_prompt_path="custom_system.txt",
307
+ system_prompt="Custom prompt",
308
+ top_k=10,
309
+ )
310
+
311
+ assert retriever.user_prompt_path == "custom_user.txt"
312
+ assert retriever.system_prompt_path == "custom_system.txt"
313
+ assert retriever.system_prompt == "Custom prompt"
314
+ assert retriever.top_k == 10
315
+
316
+
317
+ @pytest.mark.asyncio
318
+ async def test_get_context_missing_text_key(mock_vector_engine):
319
+ """Test get_context handles missing text key in payload."""
320
+ mock_result = MagicMock()
321
+ mock_result.payload = {"other_key": "value"}
322
+
323
+ mock_vector_engine.search.return_value = [mock_result]
324
+
325
+ retriever = CompletionRetriever()
326
+
327
+ with patch(
328
+ "cognee.modules.retrieval.completion_retriever.get_vector_engine",
329
+ return_value=mock_vector_engine,
330
+ ):
331
+ with pytest.raises(KeyError):
332
+ objects = await retriever.get_retrieved_objects("test query")
333
+ context = await retriever.get_context_from_objects("test query", objects)
334
+ await retriever.get_completion_from_context("test query", objects, context)