cognee 0.3.4.dev4__py3-none-any.whl → 0.3.5__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 (183) hide show
  1. cognee/api/client.py +16 -7
  2. cognee/api/health.py +5 -9
  3. cognee/api/v1/add/add.py +3 -1
  4. cognee/api/v1/cognify/cognify.py +44 -7
  5. cognee/api/v1/permissions/routers/get_permissions_router.py +8 -4
  6. cognee/api/v1/search/search.py +3 -0
  7. cognee/api/v1/ui/__init__.py +1 -1
  8. cognee/api/v1/ui/ui.py +215 -150
  9. cognee/api/v1/update/__init__.py +1 -0
  10. cognee/api/v1/update/routers/__init__.py +1 -0
  11. cognee/api/v1/update/routers/get_update_router.py +90 -0
  12. cognee/api/v1/update/update.py +100 -0
  13. cognee/base_config.py +5 -2
  14. cognee/cli/_cognee.py +28 -10
  15. cognee/cli/commands/delete_command.py +34 -2
  16. cognee/eval_framework/corpus_builder/task_getters/get_default_tasks_by_indices.py +2 -2
  17. cognee/eval_framework/evaluation/direct_llm_eval_adapter.py +3 -2
  18. cognee/eval_framework/modal_eval_dashboard.py +9 -1
  19. cognee/infrastructure/databases/graph/config.py +9 -9
  20. cognee/infrastructure/databases/graph/get_graph_engine.py +4 -21
  21. cognee/infrastructure/databases/graph/kuzu/adapter.py +60 -9
  22. cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py +3 -3
  23. cognee/infrastructure/databases/relational/config.py +4 -4
  24. cognee/infrastructure/databases/relational/create_relational_engine.py +11 -3
  25. cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py +7 -3
  26. cognee/infrastructure/databases/vector/config.py +7 -7
  27. cognee/infrastructure/databases/vector/create_vector_engine.py +7 -15
  28. cognee/infrastructure/databases/vector/embeddings/EmbeddingEngine.py +9 -0
  29. cognee/infrastructure/databases/vector/embeddings/FastembedEmbeddingEngine.py +11 -0
  30. cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +19 -2
  31. cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +11 -0
  32. cognee/infrastructure/databases/vector/embeddings/config.py +8 -0
  33. cognee/infrastructure/databases/vector/embeddings/get_embedding_engine.py +5 -0
  34. cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py +11 -10
  35. cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py +48 -38
  36. cognee/infrastructure/databases/vector/vector_db_interface.py +8 -4
  37. cognee/infrastructure/files/storage/S3FileStorage.py +15 -5
  38. cognee/infrastructure/files/storage/s3_config.py +1 -0
  39. cognee/infrastructure/files/utils/open_data_file.py +7 -14
  40. cognee/infrastructure/llm/LLMGateway.py +19 -117
  41. cognee/infrastructure/llm/config.py +28 -13
  42. cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/extract_categories.py +2 -1
  43. cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/extract_event_entities.py +3 -2
  44. cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/extract_summary.py +3 -2
  45. cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/knowledge_graph/extract_content_graph.py +2 -1
  46. cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/knowledge_graph/extract_event_graph.py +3 -2
  47. cognee/infrastructure/llm/prompts/read_query_prompt.py +3 -2
  48. cognee/infrastructure/llm/prompts/show_prompt.py +35 -0
  49. cognee/infrastructure/llm/prompts/test.txt +1 -0
  50. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/__init__.py +2 -2
  51. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/async_client.py +50 -397
  52. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/inlinedbaml.py +2 -3
  53. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/parser.py +8 -88
  54. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/runtime.py +78 -0
  55. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/stream_types.py +2 -99
  56. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/sync_client.py +49 -401
  57. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/type_builder.py +19 -882
  58. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/type_map.py +2 -34
  59. cognee/infrastructure/llm/structured_output_framework/baml/baml_client/types.py +2 -107
  60. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/acreate_structured_output.baml +26 -0
  61. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/__init__.py +1 -2
  62. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/acreate_structured_output.py +76 -0
  63. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/create_dynamic_baml_type.py +122 -0
  64. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/generators.baml +3 -3
  65. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +0 -32
  66. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +107 -98
  67. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +5 -6
  68. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +5 -6
  69. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/llm_interface.py +0 -26
  70. cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +17 -67
  71. cognee/infrastructure/llm/tokenizer/Gemini/adapter.py +8 -7
  72. cognee/infrastructure/llm/utils.py +4 -4
  73. cognee/infrastructure/loaders/LoaderEngine.py +5 -2
  74. cognee/infrastructure/loaders/external/__init__.py +7 -0
  75. cognee/infrastructure/loaders/external/advanced_pdf_loader.py +244 -0
  76. cognee/infrastructure/loaders/supported_loaders.py +7 -0
  77. cognee/modules/data/methods/create_authorized_dataset.py +9 -0
  78. cognee/modules/data/methods/get_authorized_dataset.py +1 -1
  79. cognee/modules/data/methods/get_authorized_dataset_by_name.py +11 -0
  80. cognee/modules/data/methods/get_deletion_counts.py +92 -0
  81. cognee/modules/graph/cognee_graph/CogneeGraph.py +1 -1
  82. cognee/modules/graph/utils/expand_with_nodes_and_edges.py +22 -8
  83. cognee/modules/graph/utils/retrieve_existing_edges.py +0 -2
  84. cognee/modules/ingestion/data_types/TextData.py +0 -1
  85. cognee/modules/observability/get_observe.py +14 -0
  86. cognee/modules/observability/observers.py +1 -0
  87. cognee/modules/ontology/base_ontology_resolver.py +42 -0
  88. cognee/modules/ontology/get_default_ontology_resolver.py +41 -0
  89. cognee/modules/ontology/matching_strategies.py +53 -0
  90. cognee/modules/ontology/models.py +20 -0
  91. cognee/modules/ontology/ontology_config.py +24 -0
  92. cognee/modules/ontology/ontology_env_config.py +45 -0
  93. cognee/modules/ontology/rdf_xml/{OntologyResolver.py → RDFLibOntologyResolver.py} +20 -28
  94. cognee/modules/pipelines/layers/resolve_authorized_user_dataset.py +21 -24
  95. cognee/modules/pipelines/layers/resolve_authorized_user_datasets.py +3 -3
  96. cognee/modules/retrieval/code_retriever.py +2 -1
  97. cognee/modules/retrieval/context_providers/TripletSearchContextProvider.py +1 -4
  98. cognee/modules/retrieval/graph_completion_cot_retriever.py +6 -5
  99. cognee/modules/retrieval/graph_completion_retriever.py +0 -3
  100. cognee/modules/retrieval/insights_retriever.py +1 -1
  101. cognee/modules/retrieval/jaccard_retrival.py +60 -0
  102. cognee/modules/retrieval/lexical_retriever.py +123 -0
  103. cognee/modules/retrieval/natural_language_retriever.py +2 -1
  104. cognee/modules/retrieval/temporal_retriever.py +3 -2
  105. cognee/modules/retrieval/utils/brute_force_triplet_search.py +2 -12
  106. cognee/modules/retrieval/utils/completion.py +4 -7
  107. cognee/modules/search/methods/get_search_type_tools.py +7 -0
  108. cognee/modules/search/methods/no_access_control_search.py +1 -1
  109. cognee/modules/search/methods/search.py +32 -13
  110. cognee/modules/search/types/SearchType.py +1 -0
  111. cognee/modules/users/permissions/methods/authorized_give_permission_on_datasets.py +12 -0
  112. cognee/modules/users/permissions/methods/check_permission_on_dataset.py +11 -0
  113. cognee/modules/users/permissions/methods/get_all_user_permission_datasets.py +10 -0
  114. cognee/modules/users/permissions/methods/get_document_ids_for_user.py +10 -0
  115. cognee/modules/users/permissions/methods/get_principal.py +9 -0
  116. cognee/modules/users/permissions/methods/get_principal_datasets.py +11 -0
  117. cognee/modules/users/permissions/methods/get_role.py +10 -0
  118. cognee/modules/users/permissions/methods/get_specific_user_permission_datasets.py +3 -3
  119. cognee/modules/users/permissions/methods/get_tenant.py +9 -0
  120. cognee/modules/users/permissions/methods/give_default_permission_to_role.py +9 -0
  121. cognee/modules/users/permissions/methods/give_default_permission_to_tenant.py +9 -0
  122. cognee/modules/users/permissions/methods/give_default_permission_to_user.py +9 -0
  123. cognee/modules/users/permissions/methods/give_permission_on_dataset.py +10 -0
  124. cognee/modules/users/roles/methods/add_user_to_role.py +11 -0
  125. cognee/modules/users/roles/methods/create_role.py +12 -1
  126. cognee/modules/users/tenants/methods/add_user_to_tenant.py +12 -0
  127. cognee/modules/users/tenants/methods/create_tenant.py +12 -1
  128. cognee/modules/visualization/cognee_network_visualization.py +13 -9
  129. cognee/shared/data_models.py +0 -1
  130. cognee/shared/utils.py +0 -32
  131. cognee/tasks/chunk_naive_llm_classifier/chunk_naive_llm_classifier.py +2 -2
  132. cognee/tasks/codingagents/coding_rule_associations.py +3 -2
  133. cognee/tasks/entity_completion/entity_extractors/llm_entity_extractor.py +3 -2
  134. cognee/tasks/graph/cascade_extract/utils/extract_content_nodes_and_relationship_names.py +3 -2
  135. cognee/tasks/graph/cascade_extract/utils/extract_edge_triplets.py +3 -2
  136. cognee/tasks/graph/cascade_extract/utils/extract_nodes.py +3 -2
  137. cognee/tasks/graph/extract_graph_from_code.py +2 -2
  138. cognee/tasks/graph/extract_graph_from_data.py +55 -12
  139. cognee/tasks/graph/extract_graph_from_data_v2.py +16 -4
  140. cognee/tasks/ingestion/migrate_relational_database.py +132 -41
  141. cognee/tasks/ingestion/resolve_data_directories.py +4 -1
  142. cognee/tasks/schema/ingest_database_schema.py +134 -0
  143. cognee/tasks/schema/models.py +40 -0
  144. cognee/tasks/storage/index_data_points.py +1 -1
  145. cognee/tasks/storage/index_graph_edges.py +3 -1
  146. cognee/tasks/summarization/summarize_code.py +2 -2
  147. cognee/tasks/summarization/summarize_text.py +2 -2
  148. cognee/tasks/temporal_graph/enrich_events.py +2 -2
  149. cognee/tasks/temporal_graph/extract_events_and_entities.py +2 -2
  150. cognee/tests/cli_tests/cli_unit_tests/test_cli_commands.py +13 -4
  151. cognee/tests/cli_tests/cli_unit_tests/test_cli_edge_cases.py +13 -3
  152. cognee/tests/test_advanced_pdf_loader.py +141 -0
  153. cognee/tests/test_chromadb.py +40 -0
  154. cognee/tests/test_cognee_server_start.py +6 -1
  155. cognee/tests/test_data/Quantum_computers.txt +9 -0
  156. cognee/tests/test_lancedb.py +211 -0
  157. cognee/tests/test_pgvector.py +40 -0
  158. cognee/tests/test_relational_db_migration.py +76 -0
  159. cognee/tests/unit/infrastructure/databases/test_index_graph_edges.py +2 -1
  160. cognee/tests/unit/modules/ontology/test_ontology_adapter.py +330 -13
  161. cognee/tests/unit/modules/retrieval/graph_completion_retriever_context_extension_test.py +0 -4
  162. cognee/tests/unit/modules/retrieval/graph_completion_retriever_cot_test.py +0 -4
  163. cognee/tests/unit/modules/retrieval/graph_completion_retriever_test.py +0 -4
  164. {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/METADATA +92 -96
  165. {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/RECORD +173 -159
  166. distributed/pyproject.toml +0 -1
  167. cognee/infrastructure/data/utils/extract_keywords.py +0 -48
  168. cognee/infrastructure/databases/hybrid/falkordb/FalkorDBAdapter.py +0 -1227
  169. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extract_categories.baml +0 -109
  170. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extract_content_graph.baml +0 -343
  171. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/extract_categories.py +0 -0
  172. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/extract_summary.py +0 -89
  173. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/knowledge_graph/__init__.py +0 -0
  174. cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/knowledge_graph/extract_content_graph.py +0 -44
  175. cognee/tasks/graph/infer_data_ontology.py +0 -309
  176. cognee/tests/test_falkordb.py +0 -174
  177. /cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/__init__.py +0 -0
  178. /cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/knowledge_graph/__init__.py +0 -0
  179. /cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/texts.json +0 -0
  180. {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/WHEEL +0 -0
  181. {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/entry_points.txt +0 -0
  182. {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/licenses/LICENSE +0 -0
  183. {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/licenses/NOTICE.md +0 -0
@@ -1,146 +1,155 @@
1
+ """Adapter for Generic API LLM provider API"""
2
+
1
3
  import litellm
4
+ import instructor
5
+ from typing import Type
2
6
  from pydantic import BaseModel
3
- from typing import Type, Optional
4
- from litellm import acompletion, JSONSchemaValidationError
7
+ from openai import ContentFilterFinishReasonError
8
+ from litellm.exceptions import ContentPolicyViolationError
9
+ from instructor.core import InstructorRetryException
5
10
 
6
- from cognee.shared.logging_utils import get_logger
7
- from cognee.modules.observability.get_observe import get_observe
8
- from cognee.infrastructure.llm.exceptions import MissingSystemPromptPathError
11
+ from cognee.infrastructure.llm.exceptions import ContentPolicyFilterError
9
12
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
10
13
  LLMInterface,
11
14
  )
12
- from cognee.infrastructure.llm.LLMGateway import LLMGateway
13
15
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.rate_limiter import (
14
16
  rate_limit_async,
15
17
  sleep_and_retry_async,
16
18
  )
17
19
 
18
- logger = get_logger()
19
- observe = get_observe()
20
-
21
20
 
22
21
  class GeminiAdapter(LLMInterface):
23
22
  """
24
- Handles interactions with a language model API.
23
+ Adapter for Gemini API LLM provider.
25
24
 
26
- Public methods include:
27
- - acreate_structured_output
28
- - show_prompt
25
+ This class initializes the API adapter with necessary credentials and configurations for
26
+ interacting with the gemini LLM models. It provides methods for creating structured outputs
27
+ based on user input and system prompts.
28
+
29
+ Public methods:
30
+ - acreate_structured_output(text_input: str, system_prompt: str, response_model:
31
+ Type[BaseModel]) -> BaseModel
29
32
  """
30
33
 
31
- MAX_RETRIES = 5
34
+ name: str
35
+ model: str
36
+ api_key: str
32
37
 
33
38
  def __init__(
34
39
  self,
40
+ endpoint,
35
41
  api_key: str,
36
42
  model: str,
43
+ api_version: str,
37
44
  max_completion_tokens: int,
38
- endpoint: Optional[str] = None,
39
- api_version: Optional[str] = None,
40
- streaming: bool = False,
41
- ) -> None:
42
- self.api_key = api_key
45
+ fallback_model: str = None,
46
+ fallback_api_key: str = None,
47
+ fallback_endpoint: str = None,
48
+ ):
43
49
  self.model = model
50
+ self.api_key = api_key
44
51
  self.endpoint = endpoint
45
52
  self.api_version = api_version
46
- self.streaming = streaming
47
53
  self.max_completion_tokens = max_completion_tokens
48
54
 
49
- @observe(as_type="generation")
55
+ self.fallback_model = fallback_model
56
+ self.fallback_api_key = fallback_api_key
57
+ self.fallback_endpoint = fallback_endpoint
58
+
59
+ self.aclient = instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON)
60
+
50
61
  @sleep_and_retry_async()
51
62
  @rate_limit_async
52
63
  async def acreate_structured_output(
53
64
  self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
54
65
  ) -> BaseModel:
55
66
  """
56
- Generate structured output from the language model based on the provided input and
57
- system prompt.
67
+ Generate a response from a user query.
58
68
 
59
- This method handles retries and raises a ValueError if the request fails or the response
60
- does not conform to the expected schema, logging errors accordingly.
69
+ This asynchronous method sends a user query and a system prompt to a language model and
70
+ retrieves the generated response. It handles API communication and retries up to a
71
+ specified limit in case of request failures.
61
72
 
62
73
  Parameters:
63
74
  -----------
64
75
 
65
- - text_input (str): The user input text to generate a response for.
66
- - system_prompt (str): The system's prompt or context to influence the language
67
- model's generation.
68
- - response_model (Type[BaseModel]): A model type indicating the expected format of
69
- the response.
76
+ - text_input (str): The input text from the user to generate a response for.
77
+ - system_prompt (str): A prompt that provides context or instructions for the
78
+ response generation.
79
+ - response_model (Type[BaseModel]): A Pydantic model that defines the structure of
80
+ the expected response.
70
81
 
71
82
  Returns:
72
83
  --------
73
84
 
74
- - BaseModel: Returns the generated response as an instance of the specified response
75
- model.
85
+ - BaseModel: An instance of the specified response model containing the structured
86
+ output from the language model.
76
87
  """
77
- try:
78
- if response_model is str:
79
- response_schema = {"type": "string"}
80
- else:
81
- response_schema = response_model
82
88
 
83
- messages = [
84
- {"role": "system", "content": system_prompt},
85
- {"role": "user", "content": text_input},
86
- ]
89
+ try:
90
+ return await self.aclient.chat.completions.create(
91
+ model=self.model,
92
+ messages=[
93
+ {
94
+ "role": "user",
95
+ "content": f"""{text_input}""",
96
+ },
97
+ {
98
+ "role": "system",
99
+ "content": system_prompt,
100
+ },
101
+ ],
102
+ api_key=self.api_key,
103
+ max_retries=5,
104
+ api_base=self.endpoint,
105
+ api_version=self.api_version,
106
+ response_model=response_model,
107
+ )
108
+ except (
109
+ ContentFilterFinishReasonError,
110
+ ContentPolicyViolationError,
111
+ InstructorRetryException,
112
+ ) as error:
113
+ if (
114
+ isinstance(error, InstructorRetryException)
115
+ and "content management policy" not in str(error).lower()
116
+ ):
117
+ raise error
118
+
119
+ if not (self.fallback_model and self.fallback_api_key and self.fallback_endpoint):
120
+ raise ContentPolicyFilterError(
121
+ f"The provided input contains content that is not aligned with our content policy: {text_input}"
122
+ )
87
123
 
88
124
  try:
89
- response = await acompletion(
90
- model=f"{self.model}",
91
- messages=messages,
92
- api_key=self.api_key,
93
- max_completion_tokens=self.max_completion_tokens,
94
- temperature=0.1,
95
- response_format=response_schema,
96
- timeout=100,
97
- num_retries=self.MAX_RETRIES,
125
+ return await self.aclient.chat.completions.create(
126
+ model=self.fallback_model,
127
+ messages=[
128
+ {
129
+ "role": "user",
130
+ "content": f"""{text_input}""",
131
+ },
132
+ {
133
+ "role": "system",
134
+ "content": system_prompt,
135
+ },
136
+ ],
137
+ max_retries=5,
138
+ api_key=self.fallback_api_key,
139
+ api_base=self.fallback_endpoint,
140
+ response_model=response_model,
98
141
  )
99
-
100
- if response.choices and response.choices[0].message.content:
101
- content = response.choices[0].message.content
102
- if response_model is str:
103
- return content
104
- return response_model.model_validate_json(content)
105
-
106
- except litellm.exceptions.BadRequestError as e:
107
- logger.error(f"Bad request error: {str(e)}")
108
- raise ValueError(f"Invalid request: {str(e)}")
109
-
110
- raise ValueError("Failed to get valid response after retries")
111
-
112
- except JSONSchemaValidationError as e:
113
- logger.error(f"Schema validation failed: {str(e)}")
114
- logger.debug(f"Raw response: {e.raw_response}")
115
- raise ValueError(f"Response failed schema validation: {str(e)}")
116
-
117
- def show_prompt(self, text_input: str, system_prompt: str) -> str:
118
- """
119
- Format and display the prompt for a user query.
120
-
121
- Raises an MissingQueryParameterError if no system prompt is provided.
122
-
123
- Parameters:
124
- -----------
125
-
126
- - text_input (str): The user input text to display along with the system prompt.
127
- - system_prompt (str): The path or content of the system prompt to be read and
128
- displayed.
129
-
130
- Returns:
131
- --------
132
-
133
- - str: Returns a formatted string containing the system prompt and user input.
134
- """
135
- if not text_input:
136
- text_input = "No user input provided."
137
- if not system_prompt:
138
- raise MissingSystemPromptPathError()
139
- system_prompt = LLMGateway.read_query_prompt(system_prompt)
140
-
141
- formatted_prompt = (
142
- f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
143
- if system_prompt
144
- else None
145
- )
146
- return formatted_prompt
142
+ except (
143
+ ContentFilterFinishReasonError,
144
+ ContentPolicyViolationError,
145
+ InstructorRetryException,
146
+ ) as error:
147
+ if (
148
+ isinstance(error, InstructorRetryException)
149
+ and "content management policy" not in str(error).lower()
150
+ ):
151
+ raise error
152
+ else:
153
+ raise ContentPolicyFilterError(
154
+ f"The provided input contains content that is not aligned with our content policy: {text_input}"
155
+ )
@@ -6,7 +6,7 @@ from typing import Type
6
6
  from pydantic import BaseModel
7
7
  from openai import ContentFilterFinishReasonError
8
8
  from litellm.exceptions import ContentPolicyViolationError
9
- from instructor.exceptions import InstructorRetryException
9
+ from instructor.core import InstructorRetryException
10
10
 
11
11
  from cognee.infrastructure.llm.exceptions import ContentPolicyFilterError
12
12
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
@@ -56,9 +56,7 @@ class GenericAPIAdapter(LLMInterface):
56
56
  self.fallback_api_key = fallback_api_key
57
57
  self.fallback_endpoint = fallback_endpoint
58
58
 
59
- self.aclient = instructor.from_litellm(
60
- litellm.acompletion, mode=instructor.Mode.JSON, api_key=api_key
61
- )
59
+ self.aclient = instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON)
62
60
 
63
61
  @sleep_and_retry_async()
64
62
  @rate_limit_async
@@ -102,6 +100,7 @@ class GenericAPIAdapter(LLMInterface):
102
100
  },
103
101
  ],
104
102
  max_retries=5,
103
+ api_key=self.api_key,
105
104
  api_base=self.endpoint,
106
105
  response_model=response_model,
107
106
  )
@@ -119,7 +118,7 @@ class GenericAPIAdapter(LLMInterface):
119
118
  if not (self.fallback_model and self.fallback_api_key and self.fallback_endpoint):
120
119
  raise ContentPolicyFilterError(
121
120
  f"The provided input contains content that is not aligned with our content policy: {text_input}"
122
- )
121
+ ) from error
123
122
 
124
123
  try:
125
124
  return await self.aclient.chat.completions.create(
@@ -152,4 +151,4 @@ class GenericAPIAdapter(LLMInterface):
152
151
  else:
153
152
  raise ContentPolicyFilterError(
154
153
  f"The provided input contains content that is not aligned with our content policy: {text_input}"
155
- )
154
+ ) from error
@@ -32,7 +32,7 @@ class LLMProvider(Enum):
32
32
  GEMINI = "gemini"
33
33
 
34
34
 
35
- def get_llm_client():
35
+ def get_llm_client(raise_api_key_error: bool = True):
36
36
  """
37
37
  Get the LLM client based on the configuration using Enums.
38
38
 
@@ -65,7 +65,7 @@ def get_llm_client():
65
65
  )
66
66
 
67
67
  if provider == LLMProvider.OPENAI:
68
- if llm_config.llm_api_key is None:
68
+ if llm_config.llm_api_key is None and raise_api_key_error:
69
69
  raise LLMAPIKeyNotSetError()
70
70
 
71
71
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.openai.adapter import (
@@ -86,7 +86,7 @@ def get_llm_client():
86
86
  )
87
87
 
88
88
  elif provider == LLMProvider.OLLAMA:
89
- if llm_config.llm_api_key is None:
89
+ if llm_config.llm_api_key is None and raise_api_key_error:
90
90
  raise LLMAPIKeyNotSetError()
91
91
 
92
92
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.generic_llm_api.adapter import (
@@ -111,7 +111,7 @@ def get_llm_client():
111
111
  )
112
112
 
113
113
  elif provider == LLMProvider.CUSTOM:
114
- if llm_config.llm_api_key is None:
114
+ if llm_config.llm_api_key is None and raise_api_key_error:
115
115
  raise LLMAPIKeyNotSetError()
116
116
 
117
117
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.generic_llm_api.adapter import (
@@ -130,7 +130,7 @@ def get_llm_client():
130
130
  )
131
131
 
132
132
  elif provider == LLMProvider.GEMINI:
133
- if llm_config.llm_api_key is None:
133
+ if llm_config.llm_api_key is None and raise_api_key_error:
134
134
  raise LLMAPIKeyNotSetError()
135
135
 
136
136
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.gemini.adapter import (
@@ -143,7 +143,6 @@ def get_llm_client():
143
143
  max_completion_tokens=max_completion_tokens,
144
144
  endpoint=llm_config.llm_endpoint,
145
145
  api_version=llm_config.llm_api_version,
146
- streaming=llm_config.llm_streaming,
147
146
  )
148
147
 
149
148
  else:
@@ -36,29 +36,3 @@ class LLMInterface(Protocol):
36
36
  output.
37
37
  """
38
38
  raise NotImplementedError
39
-
40
- def show_prompt(self, text_input: str, system_prompt: str) -> str:
41
- """
42
- Format and display the prompt for a user query.
43
-
44
- Parameters:
45
- -----------
46
-
47
- - text_input (str): Input text from the user to be included in the prompt.
48
- - system_prompt (str): The system prompt that will be shown alongside the user
49
- input.
50
-
51
- Returns:
52
- --------
53
-
54
- - str: The formatted prompt string combining system prompt and user input.
55
- """
56
- if not text_input:
57
- text_input = "No user input provided."
58
- if not system_prompt:
59
- raise ValueError("No system prompt path provided.")
60
- system_prompt = LLMGateway.read_query_prompt(system_prompt)
61
-
62
- formatted_prompt = f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
63
-
64
- return formatted_prompt
@@ -5,15 +5,13 @@ from typing import Type
5
5
  from pydantic import BaseModel
6
6
  from openai import ContentFilterFinishReasonError
7
7
  from litellm.exceptions import ContentPolicyViolationError
8
- from instructor.exceptions import InstructorRetryException
8
+ from instructor.core import InstructorRetryException
9
9
 
10
- from cognee.infrastructure.llm.LLMGateway import LLMGateway
11
10
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
12
11
  LLMInterface,
13
12
  )
14
13
  from cognee.infrastructure.llm.exceptions import (
15
14
  ContentPolicyFilterError,
16
- MissingSystemPromptPathError,
17
15
  )
18
16
  from cognee.infrastructure.files.utils.open_data_file import open_data_file
19
17
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.rate_limiter import (
@@ -73,8 +71,19 @@ class OpenAIAdapter(LLMInterface):
73
71
  fallback_api_key: str = None,
74
72
  fallback_endpoint: str = None,
75
73
  ):
76
- self.aclient = instructor.from_litellm(litellm.acompletion)
77
- self.client = instructor.from_litellm(litellm.completion)
74
+ # TODO: With gpt5 series models OpenAI expects JSON_SCHEMA as a mode for structured outputs.
75
+ # Make sure all new gpt models will work with this mode as well.
76
+ if "gpt-5" in model:
77
+ self.aclient = instructor.from_litellm(
78
+ litellm.acompletion, mode=instructor.Mode.JSON_SCHEMA
79
+ )
80
+ self.client = instructor.from_litellm(
81
+ litellm.completion, mode=instructor.Mode.JSON_SCHEMA
82
+ )
83
+ else:
84
+ self.aclient = instructor.from_litellm(litellm.acompletion)
85
+ self.client = instructor.from_litellm(litellm.completion)
86
+
78
87
  self.transcription_model = transcription_model
79
88
  self.model = model
80
89
  self.api_key = api_key
@@ -132,43 +141,16 @@ class OpenAIAdapter(LLMInterface):
132
141
  api_version=self.api_version,
133
142
  response_model=response_model,
134
143
  max_retries=self.MAX_RETRIES,
135
- extra_body={"reasoning_effort": "minimal"},
136
144
  )
137
145
  except (
138
146
  ContentFilterFinishReasonError,
139
147
  ContentPolicyViolationError,
140
148
  InstructorRetryException,
141
- ) as error:
142
- if (
143
- isinstance(error, InstructorRetryException)
144
- and "content management policy" not in str(error).lower()
145
- ):
146
- logger.debug(
147
- "LLM Model does not support reasoning_effort parameter, trying call without the parameter."
148
- )
149
- return await self.aclient.chat.completions.create(
150
- model=self.model,
151
- messages=[
152
- {
153
- "role": "user",
154
- "content": f"""{text_input}""",
155
- },
156
- {
157
- "role": "system",
158
- "content": system_prompt,
159
- },
160
- ],
161
- api_key=self.api_key,
162
- api_base=self.endpoint,
163
- api_version=self.api_version,
164
- response_model=response_model,
165
- max_retries=self.MAX_RETRIES,
166
- )
167
-
149
+ ) as e:
168
150
  if not (self.fallback_model and self.fallback_api_key):
169
151
  raise ContentPolicyFilterError(
170
152
  f"The provided input contains content that is not aligned with our content policy: {text_input}"
171
- )
153
+ ) from e
172
154
 
173
155
  try:
174
156
  return await self.aclient.chat.completions.create(
@@ -201,7 +183,7 @@ class OpenAIAdapter(LLMInterface):
201
183
  else:
202
184
  raise ContentPolicyFilterError(
203
185
  f"The provided input contains content that is not aligned with our content policy: {text_input}"
204
- )
186
+ ) from error
205
187
 
206
188
  @observe
207
189
  @sleep_and_retry_sync()
@@ -328,35 +310,3 @@ class OpenAIAdapter(LLMInterface):
328
310
  max_completion_tokens=300,
329
311
  max_retries=self.MAX_RETRIES,
330
312
  )
331
-
332
- def show_prompt(self, text_input: str, system_prompt: str) -> str:
333
- """
334
- Format and display the prompt for a user query.
335
-
336
- This method formats the prompt using the provided user input and system prompt,
337
- returning a string representation. Raises MissingSystemPromptPathError if the system prompt is not
338
- provided.
339
-
340
- Parameters:
341
- -----------
342
-
343
- - text_input (str): The input text provided by the user.
344
- - system_prompt (str): The system's prompt to guide the model's response.
345
-
346
- Returns:
347
- --------
348
-
349
- - str: A formatted string representing the user input and system prompt.
350
- """
351
- if not text_input:
352
- text_input = "No user input provided."
353
- if not system_prompt:
354
- raise MissingSystemPromptPathError()
355
- system_prompt = LLMGateway.read_query_prompt(system_prompt)
356
-
357
- formatted_prompt = (
358
- f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
359
- if system_prompt
360
- else None
361
- )
362
- return formatted_prompt
@@ -3,6 +3,7 @@ from typing import List, Any
3
3
  from ..tokenizer_interface import TokenizerInterface
4
4
 
5
5
 
6
+ # NOTE: DEPRECATED as to count tokens you need to send an API request to Google it is too slow to use with Cognee
6
7
  class GeminiTokenizer(TokenizerInterface):
7
8
  """
8
9
  Implements a tokenizer interface for the Gemini model, managing token extraction and
@@ -16,10 +17,10 @@ class GeminiTokenizer(TokenizerInterface):
16
17
 
17
18
  def __init__(
18
19
  self,
19
- model: str,
20
+ llm_model: str,
20
21
  max_completion_tokens: int = 3072,
21
22
  ):
22
- self.model = model
23
+ self.llm_model = llm_model
23
24
  self.max_completion_tokens = max_completion_tokens
24
25
 
25
26
  # Get LLM API key from config
@@ -28,12 +29,11 @@ class GeminiTokenizer(TokenizerInterface):
28
29
  get_llm_config,
29
30
  )
30
31
 
31
- config = get_embedding_config()
32
32
  llm_config = get_llm_config()
33
33
 
34
- import google.generativeai as genai
34
+ from google import genai
35
35
 
36
- genai.configure(api_key=config.embedding_api_key or llm_config.llm_api_key)
36
+ self.client = genai.Client(api_key=llm_config.llm_api_key)
37
37
 
38
38
  def extract_tokens(self, text: str) -> List[Any]:
39
39
  """
@@ -77,6 +77,7 @@ class GeminiTokenizer(TokenizerInterface):
77
77
 
78
78
  - int: The number of tokens in the given text.
79
79
  """
80
- import google.generativeai as genai
81
80
 
82
- return len(genai.embed_content(model=f"models/{self.model}", content=text))
81
+ tokens_response = self.client.models.count_tokens(model=self.llm_model, contents=text)
82
+
83
+ return tokens_response.total_tokens
@@ -3,6 +3,7 @@ import litellm
3
3
  from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.get_llm_client import (
4
4
  get_llm_client,
5
5
  )
6
+ from cognee.infrastructure.llm.LLMGateway import LLMGateway
6
7
  from cognee.shared.logging_utils import get_logger
7
8
 
8
9
  logger = get_logger()
@@ -28,7 +29,7 @@ def get_max_chunk_tokens():
28
29
 
29
30
  # Calculate max chunk size based on the following formula
30
31
  embedding_engine = get_vector_engine().embedding_engine
31
- llm_client = get_llm_client()
32
+ llm_client = get_llm_client(raise_api_key_error=False)
32
33
 
33
34
  # We need to make sure chunk size won't take more than half of LLM max context token size
34
35
  # but it also can't be bigger than the embedding engine max token size
@@ -62,7 +63,7 @@ def get_model_max_completion_tokens(model_name: str):
62
63
  max_completion_tokens = litellm.model_cost[model_name]["max_tokens"]
63
64
  logger.debug(f"Max input tokens for {model_name}: {max_completion_tokens}")
64
65
  else:
65
- logger.info("Model not found in LiteLLM's model_cost.")
66
+ logger.debug("Model not found in LiteLLM's model_cost.")
66
67
 
67
68
  return max_completion_tokens
68
69
 
@@ -76,8 +77,7 @@ async def test_llm_connection():
76
77
  the connection attempt and re-raise the exception for further handling.
77
78
  """
78
79
  try:
79
- llm_adapter = get_llm_client()
80
- await llm_adapter.acreate_structured_output(
80
+ await LLMGateway.acreate_structured_output(
81
81
  text_input="test",
82
82
  system_prompt='Respond to me with the following string: "test"',
83
83
  response_model=str,
@@ -27,6 +27,7 @@ class LoaderEngine:
27
27
 
28
28
  self.default_loader_priority = [
29
29
  "text_loader",
30
+ "advanced_pdf_loader",
30
31
  "pypdf_loader",
31
32
  "image_loader",
32
33
  "audio_loader",
@@ -86,7 +87,7 @@ class LoaderEngine:
86
87
  if loader.can_handle(extension=file_info.extension, mime_type=file_info.mime):
87
88
  return loader
88
89
  else:
89
- raise ValueError(f"Loader does not exist: {loader_name}")
90
+ logger.info(f"Skipping {loader_name}: Preferred Loader not registered")
90
91
 
91
92
  # Try default priority order
92
93
  for loader_name in self.default_loader_priority:
@@ -95,7 +96,9 @@ class LoaderEngine:
95
96
  if loader.can_handle(extension=file_info.extension, mime_type=file_info.mime):
96
97
  return loader
97
98
  else:
98
- raise ValueError(f"Loader does not exist: {loader_name}")
99
+ logger.info(
100
+ f"Skipping {loader_name}: Loader not registered (in default priority list)."
101
+ )
99
102
 
100
103
  return None
101
104
 
@@ -20,3 +20,10 @@ try:
20
20
  __all__.append("UnstructuredLoader")
21
21
  except ImportError:
22
22
  pass
23
+
24
+ try:
25
+ from .advanced_pdf_loader import AdvancedPdfLoader
26
+
27
+ __all__.append("AdvancedPdfLoader")
28
+ except ImportError:
29
+ pass