cognee 0.5.0.dev0__py3-none-any.whl → 0.5.0.dev1__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.
- cognee/api/client.py +1 -5
- cognee/api/v1/add/add.py +2 -1
- cognee/api/v1/cognify/cognify.py +24 -16
- cognee/api/v1/cognify/routers/__init__.py +0 -1
- cognee/api/v1/cognify/routers/get_cognify_router.py +3 -1
- cognee/api/v1/datasets/routers/get_datasets_router.py +3 -3
- cognee/api/v1/ontologies/ontologies.py +12 -37
- cognee/api/v1/ontologies/routers/get_ontology_router.py +27 -25
- cognee/api/v1/search/search.py +4 -0
- cognee/api/v1/ui/node_setup.py +360 -0
- cognee/api/v1/ui/npm_utils.py +50 -0
- cognee/api/v1/ui/ui.py +38 -68
- cognee/context_global_variables.py +61 -16
- cognee/eval_framework/Dockerfile +29 -0
- cognee/eval_framework/answer_generation/answer_generation_executor.py +10 -0
- cognee/eval_framework/answer_generation/run_question_answering_module.py +1 -1
- cognee/eval_framework/corpus_builder/task_getters/get_cascade_graph_tasks.py +0 -2
- cognee/eval_framework/corpus_builder/task_getters/get_default_tasks_by_indices.py +4 -4
- cognee/eval_framework/eval_config.py +2 -2
- cognee/eval_framework/modal_run_eval.py +16 -28
- cognee/infrastructure/databases/dataset_database_handler/__init__.py +3 -0
- cognee/infrastructure/databases/dataset_database_handler/dataset_database_handler_interface.py +80 -0
- cognee/infrastructure/databases/dataset_database_handler/supported_dataset_database_handlers.py +18 -0
- cognee/infrastructure/databases/dataset_database_handler/use_dataset_database_handler.py +10 -0
- cognee/infrastructure/databases/graph/config.py +3 -0
- cognee/infrastructure/databases/graph/get_graph_engine.py +1 -0
- cognee/infrastructure/databases/graph/graph_db_interface.py +15 -0
- cognee/infrastructure/databases/graph/kuzu/KuzuDatasetDatabaseHandler.py +81 -0
- cognee/infrastructure/databases/graph/kuzu/adapter.py +228 -0
- cognee/infrastructure/databases/graph/neo4j_driver/Neo4jAuraDevDatasetDatabaseHandler.py +168 -0
- cognee/infrastructure/databases/graph/neo4j_driver/adapter.py +80 -1
- cognee/infrastructure/databases/utils/__init__.py +3 -0
- cognee/infrastructure/databases/utils/get_graph_dataset_database_handler.py +10 -0
- cognee/infrastructure/databases/utils/get_or_create_dataset_database.py +62 -48
- cognee/infrastructure/databases/utils/get_vector_dataset_database_handler.py +10 -0
- cognee/infrastructure/databases/utils/resolve_dataset_database_connection_info.py +30 -0
- cognee/infrastructure/databases/vector/config.py +2 -0
- cognee/infrastructure/databases/vector/create_vector_engine.py +1 -0
- cognee/infrastructure/databases/vector/embeddings/FastembedEmbeddingEngine.py +8 -6
- cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +9 -7
- cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +11 -10
- cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py +2 -0
- cognee/infrastructure/databases/vector/lancedb/LanceDBDatasetDatabaseHandler.py +50 -0
- cognee/infrastructure/databases/vector/vector_db_interface.py +35 -0
- cognee/infrastructure/files/storage/s3_config.py +2 -0
- cognee/infrastructure/llm/LLMGateway.py +5 -2
- cognee/infrastructure/llm/config.py +35 -0
- cognee/infrastructure/llm/extraction/knowledge_graph/extract_content_graph.py +2 -2
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/acreate_structured_output.py +23 -8
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +17 -16
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/bedrock/__init__.py +5 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/bedrock/adapter.py +153 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +40 -37
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +39 -36
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +19 -1
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/mistral/adapter.py +11 -9
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py +23 -21
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +42 -34
- cognee/memify_pipelines/create_triplet_embeddings.py +53 -0
- cognee/modules/cognify/config.py +2 -0
- cognee/modules/data/deletion/prune_system.py +52 -2
- cognee/modules/data/methods/delete_dataset.py +26 -0
- cognee/modules/engine/models/Triplet.py +9 -0
- cognee/modules/engine/models/__init__.py +1 -0
- cognee/modules/graph/cognee_graph/CogneeGraph.py +85 -37
- cognee/modules/graph/cognee_graph/CogneeGraphElements.py +8 -3
- cognee/modules/memify/memify.py +1 -7
- cognee/modules/pipelines/operations/pipeline.py +18 -2
- cognee/modules/retrieval/__init__.py +1 -1
- cognee/modules/retrieval/graph_completion_context_extension_retriever.py +4 -0
- cognee/modules/retrieval/graph_completion_cot_retriever.py +4 -0
- cognee/modules/retrieval/graph_completion_retriever.py +10 -0
- cognee/modules/retrieval/graph_summary_completion_retriever.py +4 -0
- cognee/modules/retrieval/register_retriever.py +10 -0
- cognee/modules/retrieval/registered_community_retrievers.py +1 -0
- cognee/modules/retrieval/temporal_retriever.py +4 -0
- cognee/modules/retrieval/triplet_retriever.py +182 -0
- cognee/modules/retrieval/utils/brute_force_triplet_search.py +42 -10
- cognee/modules/run_custom_pipeline/run_custom_pipeline.py +8 -1
- cognee/modules/search/methods/get_search_type_tools.py +54 -8
- cognee/modules/search/methods/no_access_control_search.py +4 -0
- cognee/modules/search/methods/search.py +21 -0
- cognee/modules/search/types/SearchType.py +1 -1
- cognee/modules/settings/get_settings.py +19 -0
- cognee/modules/users/methods/get_authenticated_user.py +2 -2
- cognee/modules/users/models/DatasetDatabase.py +15 -3
- cognee/shared/logging_utils.py +4 -0
- cognee/shared/rate_limiting.py +30 -0
- cognee/tasks/documents/__init__.py +0 -1
- cognee/tasks/graph/extract_graph_from_data.py +9 -10
- cognee/tasks/memify/get_triplet_datapoints.py +289 -0
- cognee/tasks/storage/add_data_points.py +142 -2
- cognee/tests/integration/retrieval/test_triplet_retriever.py +84 -0
- cognee/tests/integration/tasks/test_add_data_points.py +139 -0
- cognee/tests/integration/tasks/test_get_triplet_datapoints.py +69 -0
- cognee/tests/test_cognee_server_start.py +2 -4
- cognee/tests/test_conversation_history.py +23 -1
- cognee/tests/test_dataset_database_handler.py +137 -0
- cognee/tests/test_dataset_delete.py +76 -0
- cognee/tests/test_edge_centered_payload.py +170 -0
- cognee/tests/test_pipeline_cache.py +164 -0
- cognee/tests/test_search_db.py +37 -1
- cognee/tests/unit/api/test_ontology_endpoint.py +77 -89
- cognee/tests/unit/infrastructure/llm/test_llm_config.py +46 -0
- cognee/tests/unit/infrastructure/mock_embedding_engine.py +3 -7
- cognee/tests/unit/infrastructure/test_embedding_rate_limiting_realistic.py +0 -5
- cognee/tests/unit/modules/graph/cognee_graph_elements_test.py +2 -2
- cognee/tests/unit/modules/graph/cognee_graph_test.py +406 -0
- cognee/tests/unit/modules/memify_tasks/test_get_triplet_datapoints.py +214 -0
- cognee/tests/unit/modules/retrieval/test_brute_force_triplet_search.py +608 -0
- cognee/tests/unit/modules/retrieval/triplet_retriever_test.py +83 -0
- cognee/tests/unit/tasks/storage/test_add_data_points.py +288 -0
- {cognee-0.5.0.dev0.dist-info → cognee-0.5.0.dev1.dist-info}/METADATA +76 -89
- {cognee-0.5.0.dev0.dist-info → cognee-0.5.0.dev1.dist-info}/RECORD +118 -97
- {cognee-0.5.0.dev0.dist-info → cognee-0.5.0.dev1.dist-info}/WHEEL +1 -1
- cognee/api/v1/cognify/code_graph_pipeline.py +0 -119
- cognee/api/v1/cognify/routers/get_code_pipeline_router.py +0 -90
- cognee/infrastructure/databases/vector/embeddings/embedding_rate_limiter.py +0 -544
- cognee/modules/retrieval/code_retriever.py +0 -232
- cognee/tasks/code/enrich_dependency_graph_checker.py +0 -35
- cognee/tasks/code/get_local_dependencies_checker.py +0 -20
- cognee/tasks/code/get_repo_dependency_graph_checker.py +0 -35
- cognee/tasks/documents/check_permissions_on_dataset.py +0 -26
- cognee/tasks/repo_processor/__init__.py +0 -2
- cognee/tasks/repo_processor/get_local_dependencies.py +0 -335
- cognee/tasks/repo_processor/get_non_code_files.py +0 -158
- cognee/tasks/repo_processor/get_repo_file_dependencies.py +0 -243
- cognee/tests/test_delete_bmw_example.py +0 -60
- {cognee-0.5.0.dev0.dist-info → cognee-0.5.0.dev1.dist-info}/entry_points.txt +0 -0
- {cognee-0.5.0.dev0.dist-info → cognee-0.5.0.dev1.dist-info}/licenses/LICENSE +0 -0
- {cognee-0.5.0.dev0.dist-info → cognee-0.5.0.dev1.dist-info}/licenses/NOTICE.md +0 -0
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py
CHANGED
|
@@ -13,6 +13,7 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
13
13
|
LLMInterface,
|
|
14
14
|
)
|
|
15
15
|
import logging
|
|
16
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
16
17
|
from cognee.shared.logging_utils import get_logger
|
|
17
18
|
from tenacity import (
|
|
18
19
|
retry,
|
|
@@ -73,13 +74,13 @@ class GeminiAdapter(LLMInterface):
|
|
|
73
74
|
|
|
74
75
|
@retry(
|
|
75
76
|
stop=stop_after_delay(128),
|
|
76
|
-
wait=wait_exponential_jitter(
|
|
77
|
+
wait=wait_exponential_jitter(8, 128),
|
|
77
78
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
78
79
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
79
80
|
reraise=True,
|
|
80
81
|
)
|
|
81
82
|
async def acreate_structured_output(
|
|
82
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
83
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
83
84
|
) -> BaseModel:
|
|
84
85
|
"""
|
|
85
86
|
Generate a response from a user query.
|
|
@@ -105,24 +106,25 @@ class GeminiAdapter(LLMInterface):
|
|
|
105
106
|
"""
|
|
106
107
|
|
|
107
108
|
try:
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
109
|
+
async with llm_rate_limiter_context_manager():
|
|
110
|
+
return await self.aclient.chat.completions.create(
|
|
111
|
+
model=self.model,
|
|
112
|
+
messages=[
|
|
113
|
+
{
|
|
114
|
+
"role": "user",
|
|
115
|
+
"content": f"""{text_input}""",
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"role": "system",
|
|
119
|
+
"content": system_prompt,
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
api_key=self.api_key,
|
|
123
|
+
max_retries=2,
|
|
124
|
+
api_base=self.endpoint,
|
|
125
|
+
api_version=self.api_version,
|
|
126
|
+
response_model=response_model,
|
|
127
|
+
)
|
|
126
128
|
except (
|
|
127
129
|
ContentFilterFinishReasonError,
|
|
128
130
|
ContentPolicyViolationError,
|
|
@@ -140,23 +142,24 @@ class GeminiAdapter(LLMInterface):
|
|
|
140
142
|
)
|
|
141
143
|
|
|
142
144
|
try:
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
145
|
+
async with llm_rate_limiter_context_manager():
|
|
146
|
+
return await self.aclient.chat.completions.create(
|
|
147
|
+
model=self.fallback_model,
|
|
148
|
+
messages=[
|
|
149
|
+
{
|
|
150
|
+
"role": "user",
|
|
151
|
+
"content": f"""{text_input}""",
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
"role": "system",
|
|
155
|
+
"content": system_prompt,
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
max_retries=2,
|
|
159
|
+
api_key=self.fallback_api_key,
|
|
160
|
+
api_base=self.fallback_endpoint,
|
|
161
|
+
response_model=response_model,
|
|
162
|
+
)
|
|
160
163
|
except (
|
|
161
164
|
ContentFilterFinishReasonError,
|
|
162
165
|
ContentPolicyViolationError,
|
|
@@ -13,6 +13,7 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
13
13
|
LLMInterface,
|
|
14
14
|
)
|
|
15
15
|
import logging
|
|
16
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
16
17
|
from cognee.shared.logging_utils import get_logger
|
|
17
18
|
from tenacity import (
|
|
18
19
|
retry,
|
|
@@ -73,13 +74,13 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
73
74
|
|
|
74
75
|
@retry(
|
|
75
76
|
stop=stop_after_delay(128),
|
|
76
|
-
wait=wait_exponential_jitter(
|
|
77
|
+
wait=wait_exponential_jitter(8, 128),
|
|
77
78
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
78
79
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
79
80
|
reraise=True,
|
|
80
81
|
)
|
|
81
82
|
async def acreate_structured_output(
|
|
82
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
83
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
83
84
|
) -> BaseModel:
|
|
84
85
|
"""
|
|
85
86
|
Generate a response from a user query.
|
|
@@ -105,23 +106,24 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
105
106
|
"""
|
|
106
107
|
|
|
107
108
|
try:
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
109
|
+
async with llm_rate_limiter_context_manager():
|
|
110
|
+
return await self.aclient.chat.completions.create(
|
|
111
|
+
model=self.model,
|
|
112
|
+
messages=[
|
|
113
|
+
{
|
|
114
|
+
"role": "user",
|
|
115
|
+
"content": f"""{text_input}""",
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"role": "system",
|
|
119
|
+
"content": system_prompt,
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
max_retries=2,
|
|
123
|
+
api_key=self.api_key,
|
|
124
|
+
api_base=self.endpoint,
|
|
125
|
+
response_model=response_model,
|
|
126
|
+
)
|
|
125
127
|
except (
|
|
126
128
|
ContentFilterFinishReasonError,
|
|
127
129
|
ContentPolicyViolationError,
|
|
@@ -139,23 +141,24 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
139
141
|
) from error
|
|
140
142
|
|
|
141
143
|
try:
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
144
|
+
async with llm_rate_limiter_context_manager():
|
|
145
|
+
return await self.aclient.chat.completions.create(
|
|
146
|
+
model=self.fallback_model,
|
|
147
|
+
messages=[
|
|
148
|
+
{
|
|
149
|
+
"role": "user",
|
|
150
|
+
"content": f"""{text_input}""",
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"role": "system",
|
|
154
|
+
"content": system_prompt,
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
max_retries=2,
|
|
158
|
+
api_key=self.fallback_api_key,
|
|
159
|
+
api_base=self.fallback_endpoint,
|
|
160
|
+
response_model=response_model,
|
|
161
|
+
)
|
|
159
162
|
except (
|
|
160
163
|
ContentFilterFinishReasonError,
|
|
161
164
|
ContentPolicyViolationError,
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py
CHANGED
|
@@ -24,6 +24,7 @@ class LLMProvider(Enum):
|
|
|
24
24
|
- CUSTOM: Represents a custom provider option.
|
|
25
25
|
- GEMINI: Represents the Gemini provider.
|
|
26
26
|
- MISTRAL: Represents the Mistral AI provider.
|
|
27
|
+
- BEDROCK: Represents the AWS Bedrock provider.
|
|
27
28
|
"""
|
|
28
29
|
|
|
29
30
|
OPENAI = "openai"
|
|
@@ -32,6 +33,7 @@ class LLMProvider(Enum):
|
|
|
32
33
|
CUSTOM = "custom"
|
|
33
34
|
GEMINI = "gemini"
|
|
34
35
|
MISTRAL = "mistral"
|
|
36
|
+
BEDROCK = "bedrock"
|
|
35
37
|
|
|
36
38
|
|
|
37
39
|
def get_llm_client(raise_api_key_error: bool = True):
|
|
@@ -154,7 +156,7 @@ def get_llm_client(raise_api_key_error: bool = True):
|
|
|
154
156
|
)
|
|
155
157
|
|
|
156
158
|
elif provider == LLMProvider.MISTRAL:
|
|
157
|
-
if llm_config.llm_api_key is None:
|
|
159
|
+
if llm_config.llm_api_key is None and raise_api_key_error:
|
|
158
160
|
raise LLMAPIKeyNotSetError()
|
|
159
161
|
|
|
160
162
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.mistral.adapter import (
|
|
@@ -169,5 +171,21 @@ def get_llm_client(raise_api_key_error: bool = True):
|
|
|
169
171
|
instructor_mode=llm_config.llm_instructor_mode.lower(),
|
|
170
172
|
)
|
|
171
173
|
|
|
174
|
+
elif provider == LLMProvider.BEDROCK:
|
|
175
|
+
# if llm_config.llm_api_key is None and raise_api_key_error:
|
|
176
|
+
# raise LLMAPIKeyNotSetError()
|
|
177
|
+
|
|
178
|
+
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.bedrock.adapter import (
|
|
179
|
+
BedrockAdapter,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
return BedrockAdapter(
|
|
183
|
+
model=llm_config.llm_model,
|
|
184
|
+
api_key=llm_config.llm_api_key,
|
|
185
|
+
max_completion_tokens=max_completion_tokens,
|
|
186
|
+
streaming=llm_config.llm_streaming,
|
|
187
|
+
instructor_mode=llm_config.llm_instructor_mode.lower(),
|
|
188
|
+
)
|
|
189
|
+
|
|
172
190
|
else:
|
|
173
191
|
raise UnsupportedLLMProviderError(provider)
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/mistral/adapter.py
CHANGED
|
@@ -10,6 +10,7 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
10
10
|
LLMInterface,
|
|
11
11
|
)
|
|
12
12
|
from cognee.infrastructure.llm.config import get_llm_config
|
|
13
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
13
14
|
|
|
14
15
|
import logging
|
|
15
16
|
from tenacity import (
|
|
@@ -62,13 +63,13 @@ class MistralAdapter(LLMInterface):
|
|
|
62
63
|
|
|
63
64
|
@retry(
|
|
64
65
|
stop=stop_after_delay(128),
|
|
65
|
-
wait=wait_exponential_jitter(
|
|
66
|
+
wait=wait_exponential_jitter(8, 128),
|
|
66
67
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
67
68
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
68
69
|
reraise=True,
|
|
69
70
|
)
|
|
70
71
|
async def acreate_structured_output(
|
|
71
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
72
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
72
73
|
) -> BaseModel:
|
|
73
74
|
"""
|
|
74
75
|
Generate a response from the user query.
|
|
@@ -97,13 +98,14 @@ class MistralAdapter(LLMInterface):
|
|
|
97
98
|
},
|
|
98
99
|
]
|
|
99
100
|
try:
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
101
|
+
async with llm_rate_limiter_context_manager():
|
|
102
|
+
response = await self.aclient.chat.completions.create(
|
|
103
|
+
model=self.model,
|
|
104
|
+
max_tokens=self.max_completion_tokens,
|
|
105
|
+
max_retries=2,
|
|
106
|
+
messages=messages,
|
|
107
|
+
response_model=response_model,
|
|
108
|
+
)
|
|
107
109
|
if response.choices and response.choices[0].message.content:
|
|
108
110
|
content = response.choices[0].message.content
|
|
109
111
|
return response_model.model_validate_json(content)
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py
CHANGED
|
@@ -11,6 +11,8 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
11
11
|
)
|
|
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
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
15
|
+
|
|
14
16
|
from tenacity import (
|
|
15
17
|
retry,
|
|
16
18
|
stop_after_delay,
|
|
@@ -68,13 +70,13 @@ class OllamaAPIAdapter(LLMInterface):
|
|
|
68
70
|
|
|
69
71
|
@retry(
|
|
70
72
|
stop=stop_after_delay(128),
|
|
71
|
-
wait=wait_exponential_jitter(
|
|
73
|
+
wait=wait_exponential_jitter(8, 128),
|
|
72
74
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
73
75
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
74
76
|
reraise=True,
|
|
75
77
|
)
|
|
76
78
|
async def acreate_structured_output(
|
|
77
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
79
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
78
80
|
) -> BaseModel:
|
|
79
81
|
"""
|
|
80
82
|
Generate a structured output from the LLM using the provided text and system prompt.
|
|
@@ -95,33 +97,33 @@ class OllamaAPIAdapter(LLMInterface):
|
|
|
95
97
|
|
|
96
98
|
- BaseModel: A structured output that conforms to the specified response model.
|
|
97
99
|
"""
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
100
|
+
async with llm_rate_limiter_context_manager():
|
|
101
|
+
response = self.aclient.chat.completions.create(
|
|
102
|
+
model=self.model,
|
|
103
|
+
messages=[
|
|
104
|
+
{
|
|
105
|
+
"role": "user",
|
|
106
|
+
"content": f"{text_input}",
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"role": "system",
|
|
110
|
+
"content": system_prompt,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
max_retries=2,
|
|
114
|
+
response_model=response_model,
|
|
115
|
+
)
|
|
114
116
|
|
|
115
117
|
return response
|
|
116
118
|
|
|
117
119
|
@retry(
|
|
118
120
|
stop=stop_after_delay(128),
|
|
119
|
-
wait=wait_exponential_jitter(
|
|
121
|
+
wait=wait_exponential_jitter(8, 128),
|
|
120
122
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
121
123
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
122
124
|
reraise=True,
|
|
123
125
|
)
|
|
124
|
-
async def create_transcript(self, input_file: str) -> str:
|
|
126
|
+
async def create_transcript(self, input_file: str, **kwargs) -> str:
|
|
125
127
|
"""
|
|
126
128
|
Generate an audio transcript from a user query.
|
|
127
129
|
|
|
@@ -160,7 +162,7 @@ class OllamaAPIAdapter(LLMInterface):
|
|
|
160
162
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
161
163
|
reraise=True,
|
|
162
164
|
)
|
|
163
|
-
async def transcribe_image(self, input_file: str) -> str:
|
|
165
|
+
async def transcribe_image(self, input_file: str, **kwargs) -> str:
|
|
164
166
|
"""
|
|
165
167
|
Transcribe content from an image using base64 encoding.
|
|
166
168
|
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py
CHANGED
|
@@ -22,6 +22,7 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
22
22
|
from cognee.infrastructure.llm.exceptions import (
|
|
23
23
|
ContentPolicyFilterError,
|
|
24
24
|
)
|
|
25
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
25
26
|
from cognee.infrastructure.files.utils.open_data_file import open_data_file
|
|
26
27
|
from cognee.modules.observability.get_observe import get_observe
|
|
27
28
|
from cognee.shared.logging_utils import get_logger
|
|
@@ -105,13 +106,13 @@ class OpenAIAdapter(LLMInterface):
|
|
|
105
106
|
@observe(as_type="generation")
|
|
106
107
|
@retry(
|
|
107
108
|
stop=stop_after_delay(128),
|
|
108
|
-
wait=wait_exponential_jitter(
|
|
109
|
+
wait=wait_exponential_jitter(8, 128),
|
|
109
110
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
110
111
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
111
112
|
reraise=True,
|
|
112
113
|
)
|
|
113
114
|
async def acreate_structured_output(
|
|
114
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
115
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
115
116
|
) -> BaseModel:
|
|
116
117
|
"""
|
|
117
118
|
Generate a response from a user query.
|
|
@@ -135,34 +136,9 @@ class OpenAIAdapter(LLMInterface):
|
|
|
135
136
|
"""
|
|
136
137
|
|
|
137
138
|
try:
|
|
138
|
-
|
|
139
|
-
model=self.model,
|
|
140
|
-
messages=[
|
|
141
|
-
{
|
|
142
|
-
"role": "user",
|
|
143
|
-
"content": f"""{text_input}""",
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
"role": "system",
|
|
147
|
-
"content": system_prompt,
|
|
148
|
-
},
|
|
149
|
-
],
|
|
150
|
-
api_key=self.api_key,
|
|
151
|
-
api_base=self.endpoint,
|
|
152
|
-
api_version=self.api_version,
|
|
153
|
-
response_model=response_model,
|
|
154
|
-
max_retries=self.MAX_RETRIES,
|
|
155
|
-
)
|
|
156
|
-
except (
|
|
157
|
-
ContentFilterFinishReasonError,
|
|
158
|
-
ContentPolicyViolationError,
|
|
159
|
-
InstructorRetryException,
|
|
160
|
-
) as e:
|
|
161
|
-
if not (self.fallback_model and self.fallback_api_key):
|
|
162
|
-
raise e
|
|
163
|
-
try:
|
|
139
|
+
async with llm_rate_limiter_context_manager():
|
|
164
140
|
return await self.aclient.chat.completions.create(
|
|
165
|
-
model=self.
|
|
141
|
+
model=self.model,
|
|
166
142
|
messages=[
|
|
167
143
|
{
|
|
168
144
|
"role": "user",
|
|
@@ -173,11 +149,40 @@ class OpenAIAdapter(LLMInterface):
|
|
|
173
149
|
"content": system_prompt,
|
|
174
150
|
},
|
|
175
151
|
],
|
|
176
|
-
api_key=self.
|
|
177
|
-
|
|
152
|
+
api_key=self.api_key,
|
|
153
|
+
api_base=self.endpoint,
|
|
154
|
+
api_version=self.api_version,
|
|
178
155
|
response_model=response_model,
|
|
179
156
|
max_retries=self.MAX_RETRIES,
|
|
157
|
+
**kwargs,
|
|
180
158
|
)
|
|
159
|
+
except (
|
|
160
|
+
ContentFilterFinishReasonError,
|
|
161
|
+
ContentPolicyViolationError,
|
|
162
|
+
InstructorRetryException,
|
|
163
|
+
) as e:
|
|
164
|
+
if not (self.fallback_model and self.fallback_api_key):
|
|
165
|
+
raise e
|
|
166
|
+
try:
|
|
167
|
+
async with llm_rate_limiter_context_manager():
|
|
168
|
+
return await self.aclient.chat.completions.create(
|
|
169
|
+
model=self.fallback_model,
|
|
170
|
+
messages=[
|
|
171
|
+
{
|
|
172
|
+
"role": "user",
|
|
173
|
+
"content": f"""{text_input}""",
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
"role": "system",
|
|
177
|
+
"content": system_prompt,
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
api_key=self.fallback_api_key,
|
|
181
|
+
# api_base=self.fallback_endpoint,
|
|
182
|
+
response_model=response_model,
|
|
183
|
+
max_retries=self.MAX_RETRIES,
|
|
184
|
+
**kwargs,
|
|
185
|
+
)
|
|
181
186
|
except (
|
|
182
187
|
ContentFilterFinishReasonError,
|
|
183
188
|
ContentPolicyViolationError,
|
|
@@ -202,7 +207,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
202
207
|
reraise=True,
|
|
203
208
|
)
|
|
204
209
|
def create_structured_output(
|
|
205
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
210
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
206
211
|
) -> BaseModel:
|
|
207
212
|
"""
|
|
208
213
|
Generate a response from a user query.
|
|
@@ -242,6 +247,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
242
247
|
api_version=self.api_version,
|
|
243
248
|
response_model=response_model,
|
|
244
249
|
max_retries=self.MAX_RETRIES,
|
|
250
|
+
**kwargs,
|
|
245
251
|
)
|
|
246
252
|
|
|
247
253
|
@retry(
|
|
@@ -251,7 +257,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
251
257
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
252
258
|
reraise=True,
|
|
253
259
|
)
|
|
254
|
-
async def create_transcript(self, input):
|
|
260
|
+
async def create_transcript(self, input, **kwargs):
|
|
255
261
|
"""
|
|
256
262
|
Generate an audio transcript from a user query.
|
|
257
263
|
|
|
@@ -278,6 +284,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
278
284
|
api_base=self.endpoint,
|
|
279
285
|
api_version=self.api_version,
|
|
280
286
|
max_retries=self.MAX_RETRIES,
|
|
287
|
+
**kwargs,
|
|
281
288
|
)
|
|
282
289
|
|
|
283
290
|
return transcription
|
|
@@ -289,7 +296,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
289
296
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
290
297
|
reraise=True,
|
|
291
298
|
)
|
|
292
|
-
async def transcribe_image(self, input) -> BaseModel:
|
|
299
|
+
async def transcribe_image(self, input, **kwargs) -> BaseModel:
|
|
293
300
|
"""
|
|
294
301
|
Generate a transcription of an image from a user query.
|
|
295
302
|
|
|
@@ -334,4 +341,5 @@ class OpenAIAdapter(LLMInterface):
|
|
|
334
341
|
api_version=self.api_version,
|
|
335
342
|
max_completion_tokens=300,
|
|
336
343
|
max_retries=self.MAX_RETRIES,
|
|
344
|
+
**kwargs,
|
|
337
345
|
)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from cognee import memify
|
|
4
|
+
from cognee.context_global_variables import (
|
|
5
|
+
set_database_global_context_variables,
|
|
6
|
+
)
|
|
7
|
+
from cognee.exceptions import CogneeValidationError
|
|
8
|
+
from cognee.modules.data.methods import get_authorized_existing_datasets
|
|
9
|
+
from cognee.shared.logging_utils import get_logger
|
|
10
|
+
from cognee.modules.pipelines.tasks.task import Task
|
|
11
|
+
from cognee.modules.users.models import User
|
|
12
|
+
from cognee.tasks.memify.get_triplet_datapoints import get_triplet_datapoints
|
|
13
|
+
from cognee.tasks.storage import index_data_points
|
|
14
|
+
|
|
15
|
+
logger = get_logger("create_triplet_embeddings")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def create_triplet_embeddings(
|
|
19
|
+
user: User,
|
|
20
|
+
dataset: str = "main_dataset",
|
|
21
|
+
run_in_background: bool = False,
|
|
22
|
+
triplets_batch_size: int = 100,
|
|
23
|
+
) -> dict[str, Any]:
|
|
24
|
+
dataset_to_write = await get_authorized_existing_datasets(
|
|
25
|
+
user=user, datasets=[dataset], permission_type="write"
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if not dataset_to_write:
|
|
29
|
+
raise CogneeValidationError(
|
|
30
|
+
message=f"User does not have write access to dataset: {dataset}",
|
|
31
|
+
log=False,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
await set_database_global_context_variables(
|
|
35
|
+
dataset_to_write[0].id, dataset_to_write[0].owner_id
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
extraction_tasks = [Task(get_triplet_datapoints, triplets_batch_size=triplets_batch_size)]
|
|
39
|
+
|
|
40
|
+
enrichment_tasks = [
|
|
41
|
+
Task(index_data_points, task_config={"batch_size": triplets_batch_size}),
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
result = await memify(
|
|
45
|
+
extraction_tasks=extraction_tasks,
|
|
46
|
+
enrichment_tasks=enrichment_tasks,
|
|
47
|
+
dataset=dataset_to_write[0].id,
|
|
48
|
+
data=[{}],
|
|
49
|
+
user=user,
|
|
50
|
+
run_in_background=run_in_background,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
return result
|
cognee/modules/cognify/config.py
CHANGED
|
@@ -8,12 +8,14 @@ import os
|
|
|
8
8
|
class CognifyConfig(BaseSettings):
|
|
9
9
|
classification_model: object = DefaultContentPrediction
|
|
10
10
|
summarization_model: object = SummarizedContent
|
|
11
|
+
triplet_embedding: bool = False
|
|
11
12
|
model_config = SettingsConfigDict(env_file=".env", extra="allow")
|
|
12
13
|
|
|
13
14
|
def to_dict(self) -> dict:
|
|
14
15
|
return {
|
|
15
16
|
"classification_model": self.classification_model,
|
|
16
17
|
"summarization_model": self.summarization_model,
|
|
18
|
+
"triplet_embedding": self.triplet_embedding,
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
|