cognee 0.2.4__py3-none-any.whl → 0.3.0__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/__init__.py +2 -0
- cognee/api/client.py +28 -3
- cognee/api/health.py +10 -13
- cognee/api/v1/add/add.py +3 -1
- cognee/api/v1/add/routers/get_add_router.py +12 -37
- cognee/api/v1/cloud/routers/__init__.py +1 -0
- cognee/api/v1/cloud/routers/get_checks_router.py +23 -0
- cognee/api/v1/cognify/code_graph_pipeline.py +9 -4
- cognee/api/v1/cognify/cognify.py +50 -3
- cognee/api/v1/cognify/routers/get_cognify_router.py +1 -1
- cognee/api/v1/datasets/routers/get_datasets_router.py +15 -4
- cognee/api/v1/memify/__init__.py +0 -0
- cognee/api/v1/memify/routers/__init__.py +1 -0
- cognee/api/v1/memify/routers/get_memify_router.py +100 -0
- cognee/api/v1/notebooks/routers/__init__.py +1 -0
- cognee/api/v1/notebooks/routers/get_notebooks_router.py +96 -0
- cognee/api/v1/search/routers/get_search_router.py +20 -1
- cognee/api/v1/search/search.py +11 -4
- cognee/api/v1/sync/__init__.py +17 -0
- cognee/api/v1/sync/routers/__init__.py +3 -0
- cognee/api/v1/sync/routers/get_sync_router.py +241 -0
- cognee/api/v1/sync/sync.py +877 -0
- cognee/api/v1/ui/__init__.py +1 -0
- cognee/api/v1/ui/ui.py +529 -0
- cognee/api/v1/users/routers/get_auth_router.py +13 -1
- cognee/base_config.py +10 -1
- cognee/cli/_cognee.py +93 -0
- cognee/infrastructure/databases/graph/config.py +10 -4
- cognee/infrastructure/databases/graph/kuzu/adapter.py +135 -0
- cognee/infrastructure/databases/graph/neo4j_driver/adapter.py +89 -0
- cognee/infrastructure/databases/relational/__init__.py +2 -0
- cognee/infrastructure/databases/relational/get_async_session.py +15 -0
- cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py +6 -1
- cognee/infrastructure/databases/relational/with_async_session.py +25 -0
- cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py +1 -1
- cognee/infrastructure/databases/vector/config.py +13 -6
- cognee/infrastructure/databases/vector/embeddings/FastembedEmbeddingEngine.py +1 -1
- cognee/infrastructure/databases/vector/embeddings/embedding_rate_limiter.py +2 -6
- cognee/infrastructure/databases/vector/embeddings/get_embedding_engine.py +4 -1
- cognee/infrastructure/files/storage/LocalFileStorage.py +9 -0
- cognee/infrastructure/files/storage/S3FileStorage.py +5 -0
- cognee/infrastructure/files/storage/StorageManager.py +7 -1
- cognee/infrastructure/files/storage/storage.py +16 -0
- cognee/infrastructure/llm/LLMGateway.py +18 -0
- cognee/infrastructure/llm/config.py +4 -2
- cognee/infrastructure/llm/prompts/extract_query_time.txt +15 -0
- cognee/infrastructure/llm/prompts/generate_event_entity_prompt.txt +25 -0
- cognee/infrastructure/llm/prompts/generate_event_graph_prompt.txt +30 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/extraction/__init__.py +2 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/extraction/extract_event_entities.py +44 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/extraction/knowledge_graph/__init__.py +1 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/extraction/knowledge_graph/extract_event_graph.py +46 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +25 -1
- cognee/infrastructure/utils/run_sync.py +8 -1
- cognee/modules/chunking/models/DocumentChunk.py +4 -3
- cognee/modules/cloud/exceptions/CloudApiKeyMissingError.py +15 -0
- cognee/modules/cloud/exceptions/CloudConnectionError.py +15 -0
- cognee/modules/cloud/exceptions/__init__.py +2 -0
- cognee/modules/cloud/operations/__init__.py +1 -0
- cognee/modules/cloud/operations/check_api_key.py +25 -0
- cognee/modules/data/deletion/prune_system.py +1 -1
- cognee/modules/data/methods/check_dataset_name.py +1 -1
- cognee/modules/data/methods/get_dataset_data.py +1 -1
- cognee/modules/data/methods/load_or_create_datasets.py +1 -1
- cognee/modules/engine/models/Event.py +16 -0
- cognee/modules/engine/models/Interval.py +8 -0
- cognee/modules/engine/models/Timestamp.py +13 -0
- cognee/modules/engine/models/__init__.py +3 -0
- cognee/modules/engine/utils/__init__.py +2 -0
- cognee/modules/engine/utils/generate_event_datapoint.py +46 -0
- cognee/modules/engine/utils/generate_timestamp_datapoint.py +51 -0
- cognee/modules/graph/cognee_graph/CogneeGraph.py +2 -2
- cognee/modules/graph/utils/__init__.py +1 -0
- cognee/modules/graph/utils/resolve_edges_to_text.py +71 -0
- cognee/modules/memify/__init__.py +1 -0
- cognee/modules/memify/memify.py +118 -0
- cognee/modules/notebooks/methods/__init__.py +5 -0
- cognee/modules/notebooks/methods/create_notebook.py +26 -0
- cognee/modules/notebooks/methods/delete_notebook.py +13 -0
- cognee/modules/notebooks/methods/get_notebook.py +21 -0
- cognee/modules/notebooks/methods/get_notebooks.py +18 -0
- cognee/modules/notebooks/methods/update_notebook.py +17 -0
- cognee/modules/notebooks/models/Notebook.py +53 -0
- cognee/modules/notebooks/models/__init__.py +1 -0
- cognee/modules/notebooks/operations/__init__.py +1 -0
- cognee/modules/notebooks/operations/run_in_local_sandbox.py +55 -0
- cognee/modules/pipelines/layers/reset_dataset_pipeline_run_status.py +19 -3
- cognee/modules/pipelines/operations/pipeline.py +1 -0
- cognee/modules/pipelines/operations/run_tasks.py +17 -41
- cognee/modules/retrieval/base_graph_retriever.py +18 -0
- cognee/modules/retrieval/base_retriever.py +1 -1
- cognee/modules/retrieval/code_retriever.py +8 -0
- cognee/modules/retrieval/coding_rules_retriever.py +31 -0
- cognee/modules/retrieval/completion_retriever.py +9 -3
- cognee/modules/retrieval/context_providers/TripletSearchContextProvider.py +1 -0
- cognee/modules/retrieval/graph_completion_context_extension_retriever.py +23 -14
- cognee/modules/retrieval/graph_completion_cot_retriever.py +21 -11
- cognee/modules/retrieval/graph_completion_retriever.py +32 -65
- cognee/modules/retrieval/graph_summary_completion_retriever.py +3 -1
- cognee/modules/retrieval/insights_retriever.py +14 -3
- cognee/modules/retrieval/summaries_retriever.py +1 -1
- cognee/modules/retrieval/temporal_retriever.py +152 -0
- cognee/modules/retrieval/utils/brute_force_triplet_search.py +7 -32
- cognee/modules/retrieval/utils/completion.py +10 -3
- cognee/modules/search/methods/get_search_type_tools.py +168 -0
- cognee/modules/search/methods/no_access_control_search.py +47 -0
- cognee/modules/search/methods/search.py +219 -139
- cognee/modules/search/types/SearchResult.py +21 -0
- cognee/modules/search/types/SearchType.py +2 -0
- cognee/modules/search/types/__init__.py +1 -0
- cognee/modules/search/utils/__init__.py +2 -0
- cognee/modules/search/utils/prepare_search_result.py +41 -0
- cognee/modules/search/utils/transform_context_to_graph.py +38 -0
- cognee/modules/sync/__init__.py +1 -0
- cognee/modules/sync/methods/__init__.py +23 -0
- cognee/modules/sync/methods/create_sync_operation.py +53 -0
- cognee/modules/sync/methods/get_sync_operation.py +107 -0
- cognee/modules/sync/methods/update_sync_operation.py +248 -0
- cognee/modules/sync/models/SyncOperation.py +142 -0
- cognee/modules/sync/models/__init__.py +3 -0
- cognee/modules/users/__init__.py +0 -1
- cognee/modules/users/methods/__init__.py +4 -1
- cognee/modules/users/methods/create_user.py +26 -1
- cognee/modules/users/methods/get_authenticated_user.py +36 -42
- cognee/modules/users/methods/get_default_user.py +3 -1
- cognee/modules/users/permissions/methods/get_specific_user_permission_datasets.py +2 -1
- cognee/root_dir.py +19 -0
- cognee/shared/logging_utils.py +1 -1
- cognee/tasks/codingagents/__init__.py +0 -0
- cognee/tasks/codingagents/coding_rule_associations.py +127 -0
- cognee/tasks/ingestion/save_data_item_to_storage.py +23 -0
- cognee/tasks/memify/__init__.py +2 -0
- cognee/tasks/memify/extract_subgraph.py +7 -0
- cognee/tasks/memify/extract_subgraph_chunks.py +11 -0
- cognee/tasks/repo_processor/get_repo_file_dependencies.py +52 -27
- cognee/tasks/temporal_graph/__init__.py +1 -0
- cognee/tasks/temporal_graph/add_entities_to_event.py +85 -0
- cognee/tasks/temporal_graph/enrich_events.py +34 -0
- cognee/tasks/temporal_graph/extract_events_and_entities.py +32 -0
- cognee/tasks/temporal_graph/extract_knowledge_graph_from_events.py +41 -0
- cognee/tasks/temporal_graph/models.py +49 -0
- cognee/tests/test_kuzu.py +4 -4
- cognee/tests/test_neo4j.py +4 -4
- cognee/tests/test_permissions.py +3 -3
- cognee/tests/test_relational_db_migration.py +7 -5
- cognee/tests/test_search_db.py +18 -24
- cognee/tests/test_temporal_graph.py +167 -0
- cognee/tests/unit/api/__init__.py +1 -0
- cognee/tests/unit/api/test_conditional_authentication_endpoints.py +246 -0
- cognee/tests/unit/modules/retrieval/chunks_retriever_test.py +18 -2
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_context_extension_test.py +13 -16
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_cot_test.py +11 -16
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_test.py +5 -4
- cognee/tests/unit/modules/retrieval/insights_retriever_test.py +4 -2
- cognee/tests/unit/modules/retrieval/rag_completion_retriever_test.py +18 -2
- cognee/tests/unit/modules/retrieval/temporal_retriever_test.py +225 -0
- cognee/tests/unit/modules/users/__init__.py +1 -0
- cognee/tests/unit/modules/users/test_conditional_authentication.py +277 -0
- cognee/tests/unit/processing/utils/utils_test.py +20 -1
- {cognee-0.2.4.dist-info → cognee-0.3.0.dist-info}/METADATA +8 -6
- {cognee-0.2.4.dist-info → cognee-0.3.0.dist-info}/RECORD +165 -90
- cognee/tests/unit/modules/search/search_methods_test.py +0 -225
- {cognee-0.2.4.dist-info → cognee-0.3.0.dist-info}/WHEEL +0 -0
- {cognee-0.2.4.dist-info → cognee-0.3.0.dist-info}/entry_points.txt +0 -0
- {cognee-0.2.4.dist-info → cognee-0.3.0.dist-info}/licenses/LICENSE +0 -0
- {cognee-0.2.4.dist-info → cognee-0.3.0.dist-info}/licenses/NOTICE.md +0 -0
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/extraction/__init__.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
from .knowledge_graph.extract_content_graph import extract_content_graph
|
|
2
|
+
from .knowledge_graph.extract_event_graph import extract_event_graph
|
|
2
3
|
from .extract_categories import extract_categories
|
|
3
4
|
from .extract_summary import extract_summary, extract_code_summary
|
|
5
|
+
from .extract_event_entities import extract_event_entities
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import List, Type
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
5
|
+
from cognee.infrastructure.llm.config import (
|
|
6
|
+
get_llm_config,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def extract_event_entities(content: str, response_model: Type[BaseModel]):
|
|
11
|
+
"""
|
|
12
|
+
Extracts event-related entities from the given content using an LLM with structured output.
|
|
13
|
+
|
|
14
|
+
This function loads an event entity extraction prompt from the LLM configuration,
|
|
15
|
+
renders it into a system prompt, and queries the LLM to produce structured entities
|
|
16
|
+
that conform to the specified response model.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
content (str): The input text from which to extract event entities.
|
|
20
|
+
response_model (Type[BaseModel]): A Pydantic model defining the structure of the expected output.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
BaseModel: An instance of the response_model populated with extracted event entities.
|
|
24
|
+
"""
|
|
25
|
+
llm_config = get_llm_config()
|
|
26
|
+
|
|
27
|
+
prompt_path = llm_config.event_entity_prompt_path
|
|
28
|
+
|
|
29
|
+
# Check if the prompt path is an absolute path or just a filename
|
|
30
|
+
if os.path.isabs(prompt_path):
|
|
31
|
+
# directory containing the file
|
|
32
|
+
base_directory = os.path.dirname(prompt_path)
|
|
33
|
+
# just the filename itself
|
|
34
|
+
prompt_path = os.path.basename(prompt_path)
|
|
35
|
+
else:
|
|
36
|
+
base_directory = None
|
|
37
|
+
|
|
38
|
+
system_prompt = LLMGateway.render_prompt(prompt_path, {}, base_directory=base_directory)
|
|
39
|
+
|
|
40
|
+
content_graph = await LLMGateway.acreate_structured_output(
|
|
41
|
+
content, system_prompt, response_model
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
return content_graph
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from pydantic import BaseModel
|
|
3
|
+
from typing import Type
|
|
4
|
+
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
5
|
+
|
|
6
|
+
from cognee.infrastructure.llm.config import (
|
|
7
|
+
get_llm_config,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def extract_event_graph(content: str, response_model: Type[BaseModel]):
|
|
12
|
+
"""
|
|
13
|
+
Extracts an event graph from the given content using an LLM with a structured output format.
|
|
14
|
+
|
|
15
|
+
This function loads a temporal graph extraction prompt from the LLM configuration,
|
|
16
|
+
renders it as a system prompt, and queries the LLM to produce a structured event
|
|
17
|
+
graph matching the specified response model.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
content (str): The input text from which to extract the event graph.
|
|
21
|
+
response_model (Type[BaseModel]): A Pydantic model defining the structure of the expected output.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
BaseModel: An instance of the response_model populated with the extracted event graph.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
llm_config = get_llm_config()
|
|
28
|
+
|
|
29
|
+
prompt_path = llm_config.temporal_graph_prompt_path
|
|
30
|
+
|
|
31
|
+
# Check if the prompt path is an absolute path or just a filename
|
|
32
|
+
if os.path.isabs(prompt_path):
|
|
33
|
+
# directory containing the file
|
|
34
|
+
base_directory = os.path.dirname(prompt_path)
|
|
35
|
+
# just the filename itself
|
|
36
|
+
prompt_path = os.path.basename(prompt_path)
|
|
37
|
+
else:
|
|
38
|
+
base_directory = None
|
|
39
|
+
|
|
40
|
+
system_prompt = LLMGateway.render_prompt(prompt_path, {}, base_directory=base_directory)
|
|
41
|
+
|
|
42
|
+
content_graph = await LLMGateway.acreate_structured_output(
|
|
43
|
+
content, system_prompt, response_model
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
return content_graph
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py
CHANGED
|
@@ -23,9 +23,12 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
23
23
|
sleep_and_retry_sync,
|
|
24
24
|
)
|
|
25
25
|
from cognee.modules.observability.get_observe import get_observe
|
|
26
|
+
from cognee.shared.logging_utils import get_logger
|
|
26
27
|
|
|
27
28
|
observe = get_observe()
|
|
28
29
|
|
|
30
|
+
logger = get_logger()
|
|
31
|
+
|
|
29
32
|
|
|
30
33
|
class OpenAIAdapter(LLMInterface):
|
|
31
34
|
"""
|
|
@@ -129,6 +132,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
129
132
|
api_version=self.api_version,
|
|
130
133
|
response_model=response_model,
|
|
131
134
|
max_retries=self.MAX_RETRIES,
|
|
135
|
+
extra_body={"reasoning_effort": "minimal"},
|
|
132
136
|
)
|
|
133
137
|
except (
|
|
134
138
|
ContentFilterFinishReasonError,
|
|
@@ -139,7 +143,27 @@ class OpenAIAdapter(LLMInterface):
|
|
|
139
143
|
isinstance(error, InstructorRetryException)
|
|
140
144
|
and "content management policy" not in str(error).lower()
|
|
141
145
|
):
|
|
142
|
-
|
|
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
|
+
)
|
|
143
167
|
|
|
144
168
|
if not (self.fallback_model and self.fallback_api_key):
|
|
145
169
|
raise ContentPolicyFilterError(
|
|
@@ -8,8 +8,15 @@ def run_sync(coro, timeout=None):
|
|
|
8
8
|
|
|
9
9
|
def runner():
|
|
10
10
|
nonlocal result, exception
|
|
11
|
+
|
|
11
12
|
try:
|
|
12
|
-
|
|
13
|
+
try:
|
|
14
|
+
running_loop = asyncio.get_running_loop()
|
|
15
|
+
|
|
16
|
+
result = asyncio.run_coroutine_threadsafe(coro, running_loop).result(timeout)
|
|
17
|
+
except RuntimeError:
|
|
18
|
+
result = asyncio.run(coro)
|
|
19
|
+
|
|
13
20
|
except Exception as e:
|
|
14
21
|
exception = e
|
|
15
22
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
from typing import List
|
|
1
|
+
from typing import List, Union
|
|
2
2
|
|
|
3
3
|
from cognee.infrastructure.engine import DataPoint
|
|
4
4
|
from cognee.modules.data.processing.document_types import Document
|
|
5
5
|
from cognee.modules.engine.models import Entity
|
|
6
|
+
from cognee.tasks.temporal_graph.models import Event
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class DocumentChunk(DataPoint):
|
|
@@ -20,7 +21,7 @@ class DocumentChunk(DataPoint):
|
|
|
20
21
|
- chunk_index: The index of the chunk in the original document.
|
|
21
22
|
- cut_type: The type of cut that defined this chunk.
|
|
22
23
|
- is_part_of: The document to which this chunk belongs.
|
|
23
|
-
- contains: A list of entities contained within the chunk (default is None).
|
|
24
|
+
- contains: A list of entities or events contained within the chunk (default is None).
|
|
24
25
|
- metadata: A dictionary to hold meta information related to the chunk, including index
|
|
25
26
|
fields.
|
|
26
27
|
"""
|
|
@@ -30,6 +31,6 @@ class DocumentChunk(DataPoint):
|
|
|
30
31
|
chunk_index: int
|
|
31
32
|
cut_type: str
|
|
32
33
|
is_part_of: Document
|
|
33
|
-
contains: List[Entity] = None
|
|
34
|
+
contains: List[Union[Entity, Event]] = None
|
|
34
35
|
|
|
35
36
|
metadata: dict = {"index_fields": ["text"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from fastapi import status
|
|
2
|
+
|
|
3
|
+
from cognee.exceptions.exceptions import CogneeConfigurationError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CloudApiKeyMissingError(CogneeConfigurationError):
|
|
7
|
+
"""Raised when the API key for the cloud service is not provided."""
|
|
8
|
+
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
message: str = "Failed to connect to the cloud service. Please add your API key to local instance.",
|
|
12
|
+
name: str = "CloudApiKeyMissingError",
|
|
13
|
+
status_code=status.HTTP_400_BAD_REQUEST,
|
|
14
|
+
):
|
|
15
|
+
super().__init__(message, name, status_code)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from fastapi import status
|
|
2
|
+
|
|
3
|
+
from cognee.exceptions.exceptions import CogneeConfigurationError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class CloudConnectionError(CogneeConfigurationError):
|
|
7
|
+
"""Raised when the connection to the cloud service fails."""
|
|
8
|
+
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
message: str = "Failed to connect to the cloud service. Please check your cloud API key in local instance.",
|
|
12
|
+
name: str = "CloudConnnectionError",
|
|
13
|
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
14
|
+
):
|
|
15
|
+
super().__init__(message, name, status_code)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .check_api_key import check_api_key
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import aiohttp
|
|
2
|
+
|
|
3
|
+
from cognee.modules.cloud.exceptions import CloudConnectionError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
async def check_api_key(auth_token: str):
|
|
7
|
+
cloud_base_url = "http://localhost:8001"
|
|
8
|
+
|
|
9
|
+
url = f"{cloud_base_url}/api/api-keys/check"
|
|
10
|
+
headers = {"X-Api-Key": auth_token}
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
async with aiohttp.ClientSession() as session:
|
|
14
|
+
async with session.post(url, headers=headers) as response:
|
|
15
|
+
if response.status == 200:
|
|
16
|
+
return
|
|
17
|
+
else:
|
|
18
|
+
error_text = await response.text()
|
|
19
|
+
|
|
20
|
+
raise CloudConnectionError(
|
|
21
|
+
f"Failed to connect to cloud instance: {response.status} - {error_text}"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
except Exception as e:
|
|
25
|
+
raise CloudConnectionError(f"Failed to connect to cloud instance: {str(e)}")
|
|
@@ -3,7 +3,7 @@ from cognee.infrastructure.databases.graph.get_graph_engine import get_graph_eng
|
|
|
3
3
|
from cognee.infrastructure.databases.relational import get_relational_engine
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
async def prune_system(graph=True, vector=True, metadata=
|
|
6
|
+
async def prune_system(graph=True, vector=True, metadata=True):
|
|
7
7
|
if graph:
|
|
8
8
|
graph_engine = await get_graph_engine()
|
|
9
9
|
await graph_engine.delete_graph()
|
|
@@ -2,7 +2,7 @@ from typing import List, Union
|
|
|
2
2
|
from uuid import UUID
|
|
3
3
|
|
|
4
4
|
from cognee.modules.data.models import Dataset
|
|
5
|
-
from cognee.modules.data.methods import create_authorized_dataset
|
|
5
|
+
from cognee.modules.data.methods.create_authorized_dataset import create_authorized_dataset
|
|
6
6
|
from cognee.modules.data.exceptions import DatasetNotFoundError
|
|
7
7
|
|
|
8
8
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from typing import Optional, Any
|
|
2
|
+
from pydantic import SkipValidation
|
|
3
|
+
from cognee.infrastructure.engine import DataPoint
|
|
4
|
+
from cognee.modules.engine.models.Timestamp import Timestamp
|
|
5
|
+
from cognee.modules.engine.models.Interval import Interval
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Event(DataPoint):
|
|
9
|
+
name: str
|
|
10
|
+
description: Optional[str] = None
|
|
11
|
+
at: Optional[Timestamp] = None
|
|
12
|
+
during: Optional[Interval] = None
|
|
13
|
+
location: Optional[str] = None
|
|
14
|
+
attributes: SkipValidation[Any] = None
|
|
15
|
+
|
|
16
|
+
metadata: dict = {"index_fields": ["name"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from pydantic import Field
|
|
2
|
+
from cognee.infrastructure.engine import DataPoint
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Timestamp(DataPoint):
|
|
6
|
+
time_at: int = Field(...)
|
|
7
|
+
year: int = Field(...)
|
|
8
|
+
month: int = Field(...)
|
|
9
|
+
day: int = Field(...)
|
|
10
|
+
hour: int = Field(...)
|
|
11
|
+
minute: int = Field(...)
|
|
12
|
+
second: int = Field(...)
|
|
13
|
+
timestamp_str: str = Field(...)
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
from .generate_node_id import generate_node_id
|
|
2
2
|
from .generate_node_name import generate_node_name
|
|
3
3
|
from .generate_edge_name import generate_edge_name
|
|
4
|
+
from .generate_event_datapoint import generate_event_datapoint
|
|
5
|
+
from .generate_timestamp_datapoint import generate_timestamp_datapoint
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from cognee.modules.engine.models import Interval, Event
|
|
2
|
+
from cognee.modules.engine.utils.generate_timestamp_datapoint import generate_timestamp_datapoint
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def generate_event_datapoint(event) -> Event:
|
|
6
|
+
"""
|
|
7
|
+
Generates an Event datapoint from a given event model, including temporal metadata if available.
|
|
8
|
+
|
|
9
|
+
The function maps the basic attributes (name, description, location) from the input event
|
|
10
|
+
and enriches them with temporal information. If start and end times are provided, an
|
|
11
|
+
Interval is created. If only one timestamp is available, it is added directly. Temporal
|
|
12
|
+
information is also appended to the event description for context.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
event: An event model instance containing attributes such as name, description,
|
|
16
|
+
location, time_from, and time_to.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
Event: A structured Event object with name, description, location, and enriched
|
|
20
|
+
temporal details.
|
|
21
|
+
"""
|
|
22
|
+
# Base event data
|
|
23
|
+
event_data = {
|
|
24
|
+
"name": event.name,
|
|
25
|
+
"description": event.description,
|
|
26
|
+
"location": event.location,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
# Create timestamps if they exist
|
|
30
|
+
time_from = generate_timestamp_datapoint(event.time_from) if event.time_from else None
|
|
31
|
+
time_to = generate_timestamp_datapoint(event.time_to) if event.time_to else None
|
|
32
|
+
|
|
33
|
+
# Add temporal information
|
|
34
|
+
if time_from and time_to:
|
|
35
|
+
event_data["during"] = Interval(time_from=time_from, time_to=time_to)
|
|
36
|
+
# Enrich description with temporal info
|
|
37
|
+
temporal_info = f"\n---\nTime data: {time_from.timestamp_str} to {time_to.timestamp_str}"
|
|
38
|
+
event_data["description"] = (event_data["description"] or "Event") + temporal_info
|
|
39
|
+
elif time_from or time_to:
|
|
40
|
+
timestamp = time_from or time_to
|
|
41
|
+
event_data["at"] = timestamp
|
|
42
|
+
# Enrich description with temporal info
|
|
43
|
+
temporal_info = f"\n---\nTime data: {timestamp.timestamp_str}"
|
|
44
|
+
event_data["description"] = (event_data["description"] or "Event") + temporal_info
|
|
45
|
+
|
|
46
|
+
return Event(**event_data)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from datetime import datetime, timezone
|
|
2
|
+
from cognee.modules.engine.models import Interval, Timestamp, Event
|
|
3
|
+
from cognee.modules.engine.utils import generate_node_id
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def generate_timestamp_datapoint(ts: Timestamp) -> Timestamp:
|
|
7
|
+
"""
|
|
8
|
+
Generates a normalized Timestamp datapoint from a given Timestamp model.
|
|
9
|
+
|
|
10
|
+
The function converts the provided timestamp into an integer representation,
|
|
11
|
+
constructs a human-readable string format, and creates a new Timestamp object
|
|
12
|
+
with a unique identifier.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
ts (Timestamp): The input Timestamp model containing date and time components.
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
Timestamp: A new Timestamp object with a generated ID, integer representation,
|
|
19
|
+
original components, and formatted string.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
time_at = date_to_int(ts)
|
|
23
|
+
timestamp_str = (
|
|
24
|
+
f"{ts.year:04d}-{ts.month:02d}-{ts.day:02d} {ts.hour:02d}:{ts.minute:02d}:{ts.second:02d}"
|
|
25
|
+
)
|
|
26
|
+
return Timestamp(
|
|
27
|
+
id=generate_node_id(str(time_at)),
|
|
28
|
+
time_at=time_at,
|
|
29
|
+
year=ts.year,
|
|
30
|
+
month=ts.month,
|
|
31
|
+
day=ts.day,
|
|
32
|
+
hour=ts.hour,
|
|
33
|
+
minute=ts.minute,
|
|
34
|
+
second=ts.second,
|
|
35
|
+
timestamp_str=timestamp_str,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def date_to_int(ts: Timestamp) -> int:
|
|
40
|
+
"""
|
|
41
|
+
Converts a Timestamp model into an integer representation in milliseconds since the Unix epoch (UTC).
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
ts (Timestamp): The input Timestamp model containing year, month, day, hour, minute, and second.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
int: The UTC timestamp in milliseconds since January 1, 1970.
|
|
48
|
+
"""
|
|
49
|
+
dt = datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, tzinfo=timezone.utc)
|
|
50
|
+
time = int(dt.timestamp() * 1000)
|
|
51
|
+
return time
|
|
@@ -76,7 +76,7 @@ class CogneeGraph(CogneeAbstractGraph):
|
|
|
76
76
|
start_time = time.time()
|
|
77
77
|
|
|
78
78
|
# Determine projection strategy
|
|
79
|
-
if node_type is not None and node_name
|
|
79
|
+
if node_type is not None and node_name not in [None, [], ""]:
|
|
80
80
|
nodes_data, edges_data = await adapter.get_nodeset_subgraph(
|
|
81
81
|
node_type=node_type, node_name=node_name
|
|
82
82
|
)
|
|
@@ -180,7 +180,7 @@ class CogneeGraph(CogneeAbstractGraph):
|
|
|
180
180
|
logger.error(f"Error mapping vector distances to edges: {str(ex)}")
|
|
181
181
|
raise ex
|
|
182
182
|
|
|
183
|
-
async def calculate_top_triplet_importances(self, k: int) -> List:
|
|
183
|
+
async def calculate_top_triplet_importances(self, k: int) -> List[Edge]:
|
|
184
184
|
def score(edge):
|
|
185
185
|
n1 = edge.node1.attributes.get("vector_distance", 1)
|
|
186
186
|
n2 = edge.node2.attributes.get("vector_distance", 1)
|
|
@@ -4,3 +4,4 @@ from .get_model_instance_from_graph import get_model_instance_from_graph
|
|
|
4
4
|
from .retrieve_existing_edges import retrieve_existing_edges
|
|
5
5
|
from .convert_node_to_data_point import convert_node_to_data_point
|
|
6
6
|
from .deduplicate_nodes_and_edges import deduplicate_nodes_and_edges
|
|
7
|
+
from .resolve_edges_to_text import resolve_edges_to_text
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
from cognee.modules.graph.cognee_graph.CogneeGraphElements import Edge
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
async def resolve_edges_to_text(retrieved_edges: List[Edge]) -> str:
|
|
6
|
+
"""
|
|
7
|
+
Converts retrieved graph edges into a human-readable string format.
|
|
8
|
+
|
|
9
|
+
Parameters:
|
|
10
|
+
-----------
|
|
11
|
+
|
|
12
|
+
- retrieved_edges (list): A list of edges retrieved from the graph.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
--------
|
|
16
|
+
|
|
17
|
+
- str: A formatted string representation of the nodes and their connections.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def _get_nodes(retrieved_edges: List[Edge]) -> dict:
|
|
21
|
+
def _get_title(text: str, first_n_words: int = 7, top_n_words: int = 3) -> str:
|
|
22
|
+
def _top_n_words(text, stop_words=None, top_n=3, separator=", "):
|
|
23
|
+
"""Concatenates the top N frequent words in text."""
|
|
24
|
+
if stop_words is None:
|
|
25
|
+
from cognee.modules.retrieval.utils.stop_words import DEFAULT_STOP_WORDS
|
|
26
|
+
|
|
27
|
+
stop_words = DEFAULT_STOP_WORDS
|
|
28
|
+
|
|
29
|
+
import string
|
|
30
|
+
|
|
31
|
+
words = [word.lower().strip(string.punctuation) for word in text.split()]
|
|
32
|
+
|
|
33
|
+
if stop_words:
|
|
34
|
+
words = [word for word in words if word and word not in stop_words]
|
|
35
|
+
|
|
36
|
+
from collections import Counter
|
|
37
|
+
|
|
38
|
+
top_words = [word for word, freq in Counter(words).most_common(top_n)]
|
|
39
|
+
|
|
40
|
+
return separator.join(top_words)
|
|
41
|
+
|
|
42
|
+
"""Creates a title, by combining first words with most frequent words from the text."""
|
|
43
|
+
first_words = text.split()[:first_n_words]
|
|
44
|
+
top_words = _top_n_words(text, top_n=first_n_words)
|
|
45
|
+
return f"{' '.join(first_words)}... [{top_words}]"
|
|
46
|
+
|
|
47
|
+
"""Creates a dictionary of nodes with their names and content."""
|
|
48
|
+
nodes = {}
|
|
49
|
+
for edge in retrieved_edges:
|
|
50
|
+
for node in (edge.node1, edge.node2):
|
|
51
|
+
if node.id not in nodes:
|
|
52
|
+
text = node.attributes.get("text")
|
|
53
|
+
if text:
|
|
54
|
+
name = _get_title(text)
|
|
55
|
+
content = text
|
|
56
|
+
else:
|
|
57
|
+
name = node.attributes.get("name", "Unnamed Node")
|
|
58
|
+
content = node.attributes.get("description", name)
|
|
59
|
+
nodes[node.id] = {"node": node, "name": name, "content": content}
|
|
60
|
+
return nodes
|
|
61
|
+
|
|
62
|
+
nodes = _get_nodes(retrieved_edges)
|
|
63
|
+
node_section = "\n".join(
|
|
64
|
+
f"Node: {info['name']}\n__node_content_start__\n{info['content']}\n__node_content_end__\n"
|
|
65
|
+
for info in nodes.values()
|
|
66
|
+
)
|
|
67
|
+
connection_section = "\n".join(
|
|
68
|
+
f"{nodes[edge.node1.id]['name']} --[{edge.attributes['relationship_type']}]--> {nodes[edge.node2.id]['name']}"
|
|
69
|
+
for edge in retrieved_edges
|
|
70
|
+
)
|
|
71
|
+
return f"Nodes:\n{node_section}\n\nConnections:\n{connection_section}"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .memify import memify
|