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,191 @@
1
+ """Adapter for Instructor-backed Structured Output Framework for Llama CPP"""
2
+
3
+ import litellm
4
+ import logging
5
+ import instructor
6
+ from typing import Type, Optional
7
+ from openai import AsyncOpenAI
8
+ from pydantic import BaseModel
9
+
10
+ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
11
+ LLMInterface,
12
+ )
13
+ from cognee.shared.logging_utils import get_logger
14
+ from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
15
+
16
+ from tenacity import (
17
+ retry,
18
+ stop_after_delay,
19
+ wait_exponential_jitter,
20
+ retry_if_not_exception_type,
21
+ before_sleep_log,
22
+ )
23
+
24
+ logger = get_logger()
25
+
26
+
27
+ class LlamaCppAPIAdapter(LLMInterface):
28
+ """
29
+ Adapter for Llama CPP LLM provider with support for TWO modes:
30
+
31
+ 1. SERVER MODE (OpenAI-compatible):
32
+ - Connects to llama-cpp-python server via HTTP (local or remote)
33
+ - Uses instructor.from_openai()
34
+ - Requires: endpoint, api_key, model
35
+
36
+ 2. LOCAL MODE (In-process):
37
+ - Loads model directly using llama-cpp-python library
38
+ - Uses instructor.patch() on llama.Llama object
39
+ - Requires: model_path
40
+
41
+ Public methods:
42
+ - acreate_structured_output
43
+
44
+ Instance variables:
45
+ - name
46
+ - model (for server mode) or model_path (for local mode)
47
+ - mode_type: "server" or "local"
48
+ - max_completion_tokens
49
+ - aclient
50
+ """
51
+
52
+ name: str
53
+ model: Optional[str]
54
+ model_path: Optional[str]
55
+ mode_type: str # "server" or "local"
56
+ default_instructor_mode = instructor.Mode.JSON
57
+
58
+ def __init__(
59
+ self,
60
+ name: str = "LlamaCpp",
61
+ max_completion_tokens: int = 2048,
62
+ instructor_mode: Optional[str] = None,
63
+ # Server mode parameters
64
+ endpoint: Optional[str] = None,
65
+ api_key: Optional[str] = None,
66
+ model: Optional[str] = None,
67
+ # Local mode parameters
68
+ model_path: Optional[str] = None,
69
+ n_ctx: int = 2048,
70
+ n_gpu_layers: int = 0,
71
+ chat_format: str = "chatml",
72
+ ):
73
+ self.name = name
74
+ self.max_completion_tokens = max_completion_tokens
75
+ self.instructor_mode = instructor_mode if instructor_mode else self.default_instructor_mode
76
+
77
+ # Determine which mode to use
78
+ if model_path:
79
+ self._init_local_mode(model_path, n_ctx, n_gpu_layers, chat_format)
80
+ elif endpoint:
81
+ self._init_server_mode(endpoint, api_key, model)
82
+ else:
83
+ raise ValueError(
84
+ "Must provide either 'model_path' (for local mode) or 'endpoint' (for server mode)"
85
+ )
86
+
87
+ def _init_local_mode(self, model_path: str, n_ctx: int, n_gpu_layers: int, chat_format: str):
88
+ """Initialize local mode using llama-cpp-python library directly"""
89
+ try:
90
+ import llama_cpp
91
+ except ImportError:
92
+ raise ImportError(
93
+ "llama-cpp-python is not installed. Install with: pip install llama-cpp-python"
94
+ )
95
+
96
+ logger.info(f"Initializing LlamaCpp in LOCAL mode with model: {model_path}")
97
+
98
+ self.mode_type = "local"
99
+ self.model_path = model_path
100
+ self.model = None
101
+
102
+ # Initialize llama-cpp-python with the model
103
+ self.llama = llama_cpp.Llama(
104
+ model_path=model_path,
105
+ n_gpu_layers=n_gpu_layers, # -1 for all GPU, 0 for CPU only
106
+ chat_format=chat_format,
107
+ n_ctx=n_ctx,
108
+ verbose=False,
109
+ )
110
+
111
+ self.aclient = instructor.patch(
112
+ create=self.llama.create_chat_completion_openai_v1,
113
+ mode=instructor.Mode(self.instructor_mode),
114
+ )
115
+
116
+ def _init_server_mode(self, endpoint: str, api_key: Optional[str], model: Optional[str]):
117
+ """Initialize server mode connecting to llama-cpp-python server"""
118
+ logger.info(f"Initializing LlamaCpp in SERVER mode with endpoint: {endpoint}")
119
+
120
+ self.mode_type = "server"
121
+ self.model = model
122
+ self.model_path = None
123
+ self.endpoint = endpoint
124
+ self.api_key = api_key
125
+
126
+ # Use instructor.from_openai() for server mode (OpenAI-compatible API)
127
+ self.aclient = instructor.from_openai(
128
+ AsyncOpenAI(base_url=self.endpoint, api_key=self.api_key),
129
+ mode=instructor.Mode(self.instructor_mode),
130
+ )
131
+
132
+ @retry(
133
+ stop=stop_after_delay(128),
134
+ wait=wait_exponential_jitter(8, 128),
135
+ retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
136
+ before_sleep=before_sleep_log(logger, logging.DEBUG),
137
+ reraise=True,
138
+ )
139
+ async def acreate_structured_output(
140
+ self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
141
+ ) -> BaseModel:
142
+ """
143
+ Generate a structured output from the LLM using the provided text and system prompt.
144
+
145
+ Works in both local and server modes transparently.
146
+
147
+ Parameters:
148
+ -----------
149
+ - text_input (str): The input text provided by the user.
150
+ - system_prompt (str): The system prompt that guides the response generation.
151
+ - response_model (Type[BaseModel]): The model type that the response should conform to.
152
+
153
+ Returns:
154
+ --------
155
+ - BaseModel: A structured output that conforms to the specified response model.
156
+ """
157
+ async with llm_rate_limiter_context_manager():
158
+ # Prepare messages (system first, then user is more standard)
159
+ messages = [
160
+ {"role": "system", "content": system_prompt},
161
+ {"role": "user", "content": text_input},
162
+ ]
163
+
164
+ if self.mode_type == "server":
165
+ # Server mode: use async client with OpenAI-compatible API
166
+ response = await self.aclient.chat.completions.create(
167
+ model=self.model,
168
+ messages=messages,
169
+ response_model=response_model,
170
+ max_retries=2,
171
+ max_completion_tokens=self.max_completion_tokens,
172
+ **kwargs,
173
+ )
174
+
175
+ else:
176
+ import asyncio
177
+
178
+ # Local mode: instructor.patch() returns a SYNC callable
179
+ # Per docs: https://python.useinstructor.com/integrations/llama-cpp-python/
180
+ def _call_sync():
181
+ return self.aclient(
182
+ messages=messages,
183
+ response_model=response_model,
184
+ max_tokens=self.max_completion_tokens,
185
+ **kwargs,
186
+ )
187
+
188
+ # Run sync function in thread pool to avoid blocking
189
+ response = await asyncio.to_thread(_call_sync)
190
+
191
+ return response
@@ -3,18 +3,14 @@
3
3
  from typing import Type, Protocol
4
4
  from abc import abstractmethod
5
5
  from pydantic import BaseModel
6
- from cognee.infrastructure.llm.LLMGateway import LLMGateway
7
6
 
8
7
 
9
8
  class LLMInterface(Protocol):
10
9
  """
11
- Define an interface for LLM models with methods for structured output and prompt
12
- display.
10
+ Define an interface for LLM models with methods for structured output, multimodal processing, and prompt display.
13
11
 
14
12
  Methods:
15
- - acreate_structured_output(text_input: str, system_prompt: str, response_model:
16
- Type[BaseModel])
17
- - show_prompt(text_input: str, system_prompt: str)
13
+ - acreate_structured_output(text_input: str, system_prompt: str, response_model: Type[BaseModel])
18
14
  """
19
15
 
20
16
  @abstractmethod
@@ -1,13 +1,13 @@
1
1
  import litellm
2
2
  import instructor
3
3
  from pydantic import BaseModel
4
- from typing import Type
4
+ from typing import Type, Optional
5
5
  from litellm import JSONSchemaValidationError
6
-
6
+ from cognee.infrastructure.files.utils.open_data_file import open_data_file
7
7
  from cognee.shared.logging_utils import get_logger
8
8
  from cognee.modules.observability.get_observe import get_observe
9
- from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
10
- LLMInterface,
9
+ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.generic_llm_api.adapter import (
10
+ GenericAPIAdapter,
11
11
  )
12
12
  from cognee.infrastructure.llm.config import get_llm_config
13
13
  from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
@@ -20,12 +20,14 @@ from tenacity import (
20
20
  retry_if_not_exception_type,
21
21
  before_sleep_log,
22
22
  )
23
+ from ..types import TranscriptionReturnType
24
+ from mistralai import Mistral
23
25
 
24
26
  logger = get_logger()
25
27
  observe = get_observe()
26
28
 
27
29
 
28
- class MistralAdapter(LLMInterface):
30
+ class MistralAdapter(GenericAPIAdapter):
29
31
  """
30
32
  Adapter for Mistral AI API, for structured output generation and prompt display.
31
33
 
@@ -34,10 +36,6 @@ class MistralAdapter(LLMInterface):
34
36
  - show_prompt
35
37
  """
36
38
 
37
- name = "Mistral"
38
- model: str
39
- api_key: str
40
- max_completion_tokens: int
41
39
  default_instructor_mode = "mistral_tools"
42
40
 
43
41
  def __init__(
@@ -46,12 +44,19 @@ class MistralAdapter(LLMInterface):
46
44
  model: str,
47
45
  max_completion_tokens: int,
48
46
  endpoint: str = None,
47
+ transcription_model: str = None,
48
+ image_transcribe_model: str = None,
49
49
  instructor_mode: str = None,
50
50
  ):
51
- from mistralai import Mistral
52
-
53
- self.model = model
54
- self.max_completion_tokens = max_completion_tokens
51
+ super().__init__(
52
+ api_key=api_key,
53
+ model=model,
54
+ max_completion_tokens=max_completion_tokens,
55
+ name="Mistral",
56
+ endpoint=endpoint,
57
+ transcription_model=transcription_model,
58
+ image_transcribe_model=image_transcribe_model,
59
+ )
55
60
 
56
61
  self.instructor_mode = instructor_mode if instructor_mode else self.default_instructor_mode
57
62
 
@@ -60,7 +65,9 @@ class MistralAdapter(LLMInterface):
60
65
  mode=instructor.Mode(self.instructor_mode),
61
66
  api_key=get_llm_config().llm_api_key,
62
67
  )
68
+ self.mistral_client = Mistral(api_key=self.api_key)
63
69
 
70
+ @observe(as_type="generation")
64
71
  @retry(
65
72
  stop=stop_after_delay(128),
66
73
  wait=wait_exponential_jitter(8, 128),
@@ -119,3 +126,41 @@ class MistralAdapter(LLMInterface):
119
126
  logger.error(f"Schema validation failed: {str(e)}")
120
127
  logger.debug(f"Raw response: {e.raw_response}")
121
128
  raise ValueError(f"Response failed schema validation: {str(e)}")
129
+
130
+ @observe(as_type="transcription")
131
+ @retry(
132
+ stop=stop_after_delay(128),
133
+ wait=wait_exponential_jitter(2, 128),
134
+ retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
135
+ before_sleep=before_sleep_log(logger, logging.DEBUG),
136
+ reraise=True,
137
+ )
138
+ async def create_transcript(self, input) -> Optional[TranscriptionReturnType]:
139
+ """
140
+ Generate an audio transcript from a user query.
141
+
142
+ This method creates a transcript from the specified audio file.
143
+ The audio file is processed and the transcription is retrieved from the API.
144
+
145
+ Parameters:
146
+ -----------
147
+ - input: The path to the audio file that needs to be transcribed.
148
+
149
+ Returns:
150
+ --------
151
+ The generated transcription of the audio file.
152
+ """
153
+ transcription_model = self.transcription_model
154
+ if self.transcription_model.startswith("mistral"):
155
+ transcription_model = self.transcription_model.split("/")[-1]
156
+ file_name = input.split("/")[-1]
157
+ async with open_data_file(input, mode="rb") as f:
158
+ transcription_response = self.mistral_client.audio.transcriptions.complete(
159
+ model=transcription_model,
160
+ file={
161
+ "content": f,
162
+ "file_name": file_name,
163
+ },
164
+ )
165
+
166
+ return TranscriptionReturnType(transcription_response.text, transcription_response)
@@ -12,7 +12,6 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
12
12
  from cognee.infrastructure.files.utils.open_data_file import open_data_file
13
13
  from cognee.shared.logging_utils import get_logger
14
14
  from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
15
-
16
15
  from tenacity import (
17
16
  retry,
18
17
  stop_after_delay,
@@ -1,4 +1,3 @@
1
- import base64
2
1
  import litellm
3
2
  import instructor
4
3
  from typing import Type
@@ -16,8 +15,8 @@ from tenacity import (
16
15
  before_sleep_log,
17
16
  )
18
17
 
19
- from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
20
- LLMInterface,
18
+ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.generic_llm_api.adapter import (
19
+ GenericAPIAdapter,
21
20
  )
22
21
  from cognee.infrastructure.llm.exceptions import (
23
22
  ContentPolicyFilterError,
@@ -26,13 +25,16 @@ from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
26
25
  from cognee.infrastructure.files.utils.open_data_file import open_data_file
27
26
  from cognee.modules.observability.get_observe import get_observe
28
27
  from cognee.shared.logging_utils import get_logger
28
+ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.types import (
29
+ TranscriptionReturnType,
30
+ )
29
31
 
30
32
  logger = get_logger()
31
33
 
32
34
  observe = get_observe()
33
35
 
34
36
 
35
- class OpenAIAdapter(LLMInterface):
37
+ class OpenAIAdapter(GenericAPIAdapter):
36
38
  """
37
39
  Adapter for OpenAI's GPT-3, GPT-4 API.
38
40
 
@@ -53,12 +55,7 @@ class OpenAIAdapter(LLMInterface):
53
55
  - MAX_RETRIES
54
56
  """
55
57
 
56
- name = "OpenAI"
57
- model: str
58
- api_key: str
59
- api_version: str
60
58
  default_instructor_mode = "json_schema_mode"
61
-
62
59
  MAX_RETRIES = 5
63
60
 
64
61
  """Adapter for OpenAI's GPT-3, GPT=4 API"""
@@ -66,17 +63,29 @@ class OpenAIAdapter(LLMInterface):
66
63
  def __init__(
67
64
  self,
68
65
  api_key: str,
69
- endpoint: str,
70
- api_version: str,
71
66
  model: str,
72
- transcription_model: str,
73
67
  max_completion_tokens: int,
68
+ endpoint: str = None,
69
+ api_version: str = None,
70
+ transcription_model: str = None,
74
71
  instructor_mode: str = None,
75
72
  streaming: bool = False,
76
73
  fallback_model: str = None,
77
74
  fallback_api_key: str = None,
78
75
  fallback_endpoint: str = None,
79
76
  ):
77
+ super().__init__(
78
+ api_key=api_key,
79
+ model=model,
80
+ max_completion_tokens=max_completion_tokens,
81
+ name="OpenAI",
82
+ endpoint=endpoint,
83
+ api_version=api_version,
84
+ transcription_model=transcription_model,
85
+ fallback_model=fallback_model,
86
+ fallback_api_key=fallback_api_key,
87
+ fallback_endpoint=fallback_endpoint,
88
+ )
80
89
  self.instructor_mode = instructor_mode if instructor_mode else self.default_instructor_mode
81
90
  # TODO: With gpt5 series models OpenAI expects JSON_SCHEMA as a mode for structured outputs.
82
91
  # Make sure all new gpt models will work with this mode as well.
@@ -91,18 +100,8 @@ class OpenAIAdapter(LLMInterface):
91
100
  self.aclient = instructor.from_litellm(litellm.acompletion)
92
101
  self.client = instructor.from_litellm(litellm.completion)
93
102
 
94
- self.transcription_model = transcription_model
95
- self.model = model
96
- self.api_key = api_key
97
- self.endpoint = endpoint
98
- self.api_version = api_version
99
- self.max_completion_tokens = max_completion_tokens
100
103
  self.streaming = streaming
101
104
 
102
- self.fallback_model = fallback_model
103
- self.fallback_api_key = fallback_api_key
104
- self.fallback_endpoint = fallback_endpoint
105
-
106
105
  @observe(as_type="generation")
107
106
  @retry(
108
107
  stop=stop_after_delay(128),
@@ -198,7 +197,7 @@ class OpenAIAdapter(LLMInterface):
198
197
  f"The provided input contains content that is not aligned with our content policy: {text_input}"
199
198
  ) from error
200
199
 
201
- @observe
200
+ @observe(as_type="transcription")
202
201
  @retry(
203
202
  stop=stop_after_delay(128),
204
203
  wait=wait_exponential_jitter(2, 128),
@@ -206,58 +205,7 @@ class OpenAIAdapter(LLMInterface):
206
205
  before_sleep=before_sleep_log(logger, logging.DEBUG),
207
206
  reraise=True,
208
207
  )
209
- def create_structured_output(
210
- self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
211
- ) -> BaseModel:
212
- """
213
- Generate a response from a user query.
214
-
215
- This method creates structured output by sending a synchronous request to the OpenAI API
216
- using the provided parameters to generate a completion based on the user input and
217
- system prompt.
218
-
219
- Parameters:
220
- -----------
221
-
222
- - text_input (str): The input text provided by the user for generating a response.
223
- - system_prompt (str): The system's prompt to guide the model's response.
224
- - response_model (Type[BaseModel]): The expected model type for the response.
225
-
226
- Returns:
227
- --------
228
-
229
- - BaseModel: A structured output generated by the model, returned as an instance of
230
- BaseModel.
231
- """
232
-
233
- return self.client.chat.completions.create(
234
- model=self.model,
235
- messages=[
236
- {
237
- "role": "user",
238
- "content": f"""{text_input}""",
239
- },
240
- {
241
- "role": "system",
242
- "content": system_prompt,
243
- },
244
- ],
245
- api_key=self.api_key,
246
- api_base=self.endpoint,
247
- api_version=self.api_version,
248
- response_model=response_model,
249
- max_retries=self.MAX_RETRIES,
250
- **kwargs,
251
- )
252
-
253
- @retry(
254
- stop=stop_after_delay(128),
255
- wait=wait_exponential_jitter(2, 128),
256
- retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
257
- before_sleep=before_sleep_log(logger, logging.DEBUG),
258
- reraise=True,
259
- )
260
- async def create_transcript(self, input, **kwargs):
208
+ async def create_transcript(self, input, **kwargs) -> TranscriptionReturnType:
261
209
  """
262
210
  Generate an audio transcript from a user query.
263
211
 
@@ -286,60 +234,6 @@ class OpenAIAdapter(LLMInterface):
286
234
  max_retries=self.MAX_RETRIES,
287
235
  **kwargs,
288
236
  )
237
+ return TranscriptionReturnType(transcription.text, transcription)
289
238
 
290
- return transcription
291
-
292
- @retry(
293
- stop=stop_after_delay(128),
294
- wait=wait_exponential_jitter(2, 128),
295
- retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
296
- before_sleep=before_sleep_log(logger, logging.DEBUG),
297
- reraise=True,
298
- )
299
- async def transcribe_image(self, input, **kwargs) -> BaseModel:
300
- """
301
- Generate a transcription of an image from a user query.
302
-
303
- This method encodes the image and sends a request to the OpenAI API to obtain a
304
- description of the contents of the image.
305
-
306
- Parameters:
307
- -----------
308
-
309
- - input: The path to the image file that needs to be transcribed.
310
-
311
- Returns:
312
- --------
313
-
314
- - BaseModel: A structured output generated by the model, returned as an instance of
315
- BaseModel.
316
- """
317
- async with open_data_file(input, mode="rb") as image_file:
318
- encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
319
-
320
- return litellm.completion(
321
- model=self.model,
322
- messages=[
323
- {
324
- "role": "user",
325
- "content": [
326
- {
327
- "type": "text",
328
- "text": "What's in this image?",
329
- },
330
- {
331
- "type": "image_url",
332
- "image_url": {
333
- "url": f"data:image/jpeg;base64,{encoded_image}",
334
- },
335
- },
336
- ],
337
- }
338
- ],
339
- api_key=self.api_key,
340
- api_base=self.endpoint,
341
- api_version=self.api_version,
342
- max_completion_tokens=300,
343
- max_retries=self.MAX_RETRIES,
344
- **kwargs,
345
- )
239
+ # transcribe_image is inherited from GenericAPIAdapter
@@ -0,0 +1,10 @@
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class TranscriptionReturnType:
5
+ text: str
6
+ payload: BaseModel
7
+
8
+ def __init__(self, text: str, payload: BaseModel):
9
+ self.text = text
10
+ self.payload = payload
@@ -33,5 +33,4 @@ class DocumentChunk(DataPoint):
33
33
  cut_type: str
34
34
  is_part_of: Document
35
35
  contains: List[Union[Entity, Event, tuple[Edge, Entity]]] = None
36
-
37
36
  metadata: dict = {"index_fields": ["text"]}
@@ -9,6 +9,7 @@ class CognifyConfig(BaseSettings):
9
9
  classification_model: object = DefaultContentPrediction
10
10
  summarization_model: object = SummarizedContent
11
11
  triplet_embedding: bool = False
12
+ chunks_per_batch: Optional[int] = None
12
13
  model_config = SettingsConfigDict(env_file=".env", extra="allow")
13
14
 
14
15
  def to_dict(self) -> dict:
@@ -16,6 +17,7 @@ class CognifyConfig(BaseSettings):
16
17
  "classification_model": self.classification_model,
17
18
  "summarization_model": self.summarization_model,
18
19
  "triplet_embedding": self.triplet_embedding,
20
+ "chunks_per_batch": self.chunks_per_batch,
19
21
  }
20
22
 
21
23
 
@@ -13,7 +13,7 @@ class Data(Base):
13
13
  __tablename__ = "data"
14
14
 
15
15
  id = Column(UUID, primary_key=True, default=uuid4)
16
-
16
+ label = Column(String, nullable=True)
17
17
  name = Column(String)
18
18
  extension = Column(String)
19
19
  mime_type = Column(String)
@@ -36,6 +36,7 @@ class Data(Base):
36
36
  data_size = Column(Integer, nullable=True) # File size in bytes
37
37
  created_at = Column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
38
38
  updated_at = Column(DateTime(timezone=True), onupdate=lambda: datetime.now(timezone.utc))
39
+ last_accessed = Column(DateTime(timezone=True), nullable=True)
39
40
 
40
41
  datasets = relationship(
41
42
  "Dataset",
@@ -49,6 +50,7 @@ class Data(Base):
49
50
  return {
50
51
  "id": str(self.id),
51
52
  "name": self.name,
53
+ "label": self.label,
52
54
  "extension": self.extension,
53
55
  "mimeType": self.mime_type,
54
56
  "rawDataLocation": self.raw_data_location,
@@ -7,5 +7,4 @@ class Entity(DataPoint):
7
7
  name: str
8
8
  is_a: Optional[EntityType] = None
9
9
  description: str
10
-
11
10
  metadata: dict = {"index_fields": ["name"]}
@@ -15,3 +15,9 @@ async def setup():
15
15
  """
16
16
  await create_relational_db_and_tables()
17
17
  await create_pgvector_db_and_tables()
18
+
19
+
20
+ if __name__ == "__main__":
21
+ import asyncio
22
+
23
+ asyncio.run(setup())