cognee 0.2.3.dev1__py3-none-any.whl → 0.2.4__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/__main__.py +4 -0
- cognee/api/v1/add/add.py +18 -6
- cognee/api/v1/cognify/code_graph_pipeline.py +7 -1
- cognee/api/v1/cognify/cognify.py +22 -107
- cognee/api/v1/cognify/routers/get_cognify_router.py +11 -3
- cognee/api/v1/datasets/routers/get_datasets_router.py +1 -1
- cognee/api/v1/responses/default_tools.py +4 -0
- cognee/api/v1/responses/dispatch_function.py +6 -1
- cognee/api/v1/responses/models.py +1 -1
- cognee/api/v1/search/search.py +6 -0
- cognee/cli/__init__.py +10 -0
- cognee/cli/_cognee.py +180 -0
- cognee/cli/commands/__init__.py +1 -0
- cognee/cli/commands/add_command.py +80 -0
- cognee/cli/commands/cognify_command.py +128 -0
- cognee/cli/commands/config_command.py +225 -0
- cognee/cli/commands/delete_command.py +80 -0
- cognee/cli/commands/search_command.py +149 -0
- cognee/cli/config.py +33 -0
- cognee/cli/debug.py +21 -0
- cognee/cli/echo.py +45 -0
- cognee/cli/exceptions.py +23 -0
- cognee/cli/minimal_cli.py +97 -0
- cognee/cli/reference.py +26 -0
- cognee/cli/suppress_logging.py +12 -0
- cognee/eval_framework/corpus_builder/corpus_builder_executor.py +2 -2
- cognee/eval_framework/eval_config.py +1 -1
- cognee/infrastructure/databases/graph/get_graph_engine.py +4 -9
- cognee/infrastructure/databases/graph/kuzu/adapter.py +64 -2
- cognee/infrastructure/databases/graph/neo4j_driver/adapter.py +49 -0
- cognee/infrastructure/databases/vector/embeddings/FastembedEmbeddingEngine.py +5 -3
- cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +16 -7
- cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +5 -5
- cognee/infrastructure/databases/vector/embeddings/config.py +2 -2
- cognee/infrastructure/databases/vector/embeddings/get_embedding_engine.py +6 -6
- cognee/infrastructure/files/utils/get_data_file_path.py +14 -9
- cognee/infrastructure/files/utils/get_file_metadata.py +2 -1
- cognee/infrastructure/llm/LLMGateway.py +14 -5
- cognee/infrastructure/llm/config.py +5 -5
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/knowledge_graph/extract_content_graph.py +16 -5
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/extraction/knowledge_graph/extract_content_graph.py +19 -15
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +3 -3
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +3 -3
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +2 -2
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +14 -8
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py +6 -4
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +3 -3
- cognee/infrastructure/llm/tokenizer/Gemini/adapter.py +2 -2
- cognee/infrastructure/llm/tokenizer/HuggingFace/adapter.py +3 -3
- cognee/infrastructure/llm/tokenizer/Mistral/adapter.py +3 -3
- cognee/infrastructure/llm/tokenizer/TikToken/adapter.py +6 -6
- cognee/infrastructure/llm/utils.py +7 -7
- cognee/modules/data/methods/__init__.py +2 -0
- cognee/modules/data/methods/create_authorized_dataset.py +19 -0
- cognee/modules/data/methods/get_authorized_dataset.py +11 -5
- cognee/modules/data/methods/get_authorized_dataset_by_name.py +16 -0
- cognee/modules/data/methods/load_or_create_datasets.py +2 -20
- cognee/modules/graph/methods/get_formatted_graph_data.py +3 -2
- cognee/modules/pipelines/__init__.py +1 -1
- cognee/modules/pipelines/exceptions/tasks.py +18 -0
- cognee/modules/pipelines/layers/__init__.py +1 -0
- cognee/modules/pipelines/layers/check_pipeline_run_qualification.py +59 -0
- cognee/modules/pipelines/layers/pipeline_execution_mode.py +127 -0
- cognee/modules/pipelines/layers/reset_dataset_pipeline_run_status.py +12 -0
- cognee/modules/pipelines/layers/resolve_authorized_user_dataset.py +34 -0
- cognee/modules/pipelines/layers/resolve_authorized_user_datasets.py +55 -0
- cognee/modules/pipelines/layers/setup_and_check_environment.py +41 -0
- cognee/modules/pipelines/layers/validate_pipeline_tasks.py +20 -0
- cognee/modules/pipelines/methods/__init__.py +2 -0
- cognee/modules/pipelines/methods/get_pipeline_runs_by_dataset.py +34 -0
- cognee/modules/pipelines/methods/reset_pipeline_run_status.py +16 -0
- cognee/modules/pipelines/operations/__init__.py +0 -1
- cognee/modules/pipelines/operations/log_pipeline_run_initiated.py +1 -1
- cognee/modules/pipelines/operations/pipeline.py +23 -138
- cognee/modules/retrieval/base_feedback.py +11 -0
- cognee/modules/retrieval/cypher_search_retriever.py +1 -9
- cognee/modules/retrieval/graph_completion_context_extension_retriever.py +9 -2
- cognee/modules/retrieval/graph_completion_cot_retriever.py +13 -6
- cognee/modules/retrieval/graph_completion_retriever.py +89 -5
- cognee/modules/retrieval/graph_summary_completion_retriever.py +2 -0
- cognee/modules/retrieval/natural_language_retriever.py +0 -4
- cognee/modules/retrieval/user_qa_feedback.py +83 -0
- cognee/modules/retrieval/utils/extract_uuid_from_node.py +18 -0
- cognee/modules/retrieval/utils/models.py +40 -0
- cognee/modules/search/methods/search.py +46 -5
- cognee/modules/search/types/SearchType.py +1 -0
- cognee/modules/settings/get_settings.py +2 -2
- cognee/shared/CodeGraphEntities.py +1 -0
- cognee/shared/logging_utils.py +142 -31
- cognee/shared/utils.py +0 -1
- cognee/tasks/graph/extract_graph_from_data.py +6 -2
- cognee/tasks/repo_processor/get_local_dependencies.py +2 -0
- cognee/tasks/repo_processor/get_repo_file_dependencies.py +120 -48
- cognee/tasks/storage/add_data_points.py +33 -3
- cognee/tests/integration/cli/__init__.py +3 -0
- cognee/tests/integration/cli/test_cli_integration.py +331 -0
- cognee/tests/integration/documents/PdfDocument_test.py +2 -2
- cognee/tests/integration/documents/TextDocument_test.py +2 -4
- cognee/tests/integration/documents/UnstructuredDocument_test.py +5 -8
- cognee/tests/{test_deletion.py → test_delete_hard.py} +0 -37
- cognee/tests/test_delete_soft.py +85 -0
- cognee/tests/test_kuzu.py +2 -2
- cognee/tests/test_neo4j.py +2 -2
- cognee/tests/test_search_db.py +126 -7
- cognee/tests/unit/cli/__init__.py +3 -0
- cognee/tests/unit/cli/test_cli_commands.py +483 -0
- cognee/tests/unit/cli/test_cli_edge_cases.py +625 -0
- cognee/tests/unit/cli/test_cli_main.py +173 -0
- cognee/tests/unit/cli/test_cli_runner.py +62 -0
- cognee/tests/unit/cli/test_cli_utils.py +127 -0
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_context_extension_test.py +3 -3
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_cot_test.py +3 -3
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_test.py +3 -3
- cognee/tests/unit/modules/search/search_methods_test.py +2 -0
- {cognee-0.2.3.dev1.dist-info → cognee-0.2.4.dist-info}/METADATA +7 -5
- {cognee-0.2.3.dev1.dist-info → cognee-0.2.4.dist-info}/RECORD +120 -83
- cognee-0.2.4.dist-info/entry_points.txt +2 -0
- cognee/infrastructure/databases/graph/networkx/__init__.py +0 -0
- cognee/infrastructure/databases/graph/networkx/adapter.py +0 -1017
- cognee/infrastructure/pipeline/models/Operation.py +0 -60
- cognee/infrastructure/pipeline/models/__init__.py +0 -0
- cognee/notebooks/github_analysis_step_by_step.ipynb +0 -37
- cognee/tests/tasks/descriptive_metrics/networkx_metrics_test.py +0 -7
- {cognee-0.2.3.dev1.dist-info → cognee-0.2.4.dist-info}/WHEEL +0 -0
- {cognee-0.2.3.dev1.dist-info → cognee-0.2.4.dist-info}/licenses/LICENSE +0 -0
- {cognee-0.2.3.dev1.dist-info → cognee-0.2.4.dist-info}/licenses/NOTICE.md +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Type
|
|
1
|
+
from typing import Type, Optional
|
|
2
2
|
from pydantic import BaseModel
|
|
3
3
|
from cognee.infrastructure.llm.config import get_llm_config
|
|
4
4
|
from cognee.shared.logging_utils import get_logger, setup_logging
|
|
@@ -6,7 +6,10 @@ from cognee.infrastructure.llm.structured_output_framework.baml.baml_client.asyn
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
async def extract_content_graph(
|
|
9
|
-
content: str,
|
|
9
|
+
content: str,
|
|
10
|
+
response_model: Type[BaseModel],
|
|
11
|
+
mode: str = "simple",
|
|
12
|
+
custom_prompt: Optional[str] = None,
|
|
10
13
|
):
|
|
11
14
|
config = get_llm_config()
|
|
12
15
|
setup_logging()
|
|
@@ -26,8 +29,16 @@ async def extract_content_graph(
|
|
|
26
29
|
# return graph
|
|
27
30
|
|
|
28
31
|
# else:
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
if custom_prompt:
|
|
33
|
+
graph = await b.ExtractContentGraphGeneric(
|
|
34
|
+
content,
|
|
35
|
+
mode="custom",
|
|
36
|
+
custom_prompt_content=custom_prompt,
|
|
37
|
+
baml_options={"client_registry": config.baml_registry},
|
|
38
|
+
)
|
|
39
|
+
else:
|
|
40
|
+
graph = await b.ExtractContentGraphGeneric(
|
|
41
|
+
content, mode=mode, baml_options={"client_registry": config.baml_registry}
|
|
42
|
+
)
|
|
32
43
|
|
|
33
44
|
return graph
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import os
|
|
2
|
-
from typing import Type
|
|
2
|
+
from typing import Type, Optional
|
|
3
3
|
from pydantic import BaseModel
|
|
4
4
|
|
|
5
5
|
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
@@ -8,21 +8,25 @@ from cognee.infrastructure.llm.config import (
|
|
|
8
8
|
)
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
async def extract_content_graph(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
# Check if the prompt path is an absolute path or just a filename
|
|
17
|
-
if os.path.isabs(prompt_path):
|
|
18
|
-
# directory containing the file
|
|
19
|
-
base_directory = os.path.dirname(prompt_path)
|
|
20
|
-
# just the filename itself
|
|
21
|
-
prompt_path = os.path.basename(prompt_path)
|
|
11
|
+
async def extract_content_graph(
|
|
12
|
+
content: str, response_model: Type[BaseModel], custom_prompt: Optional[str] = None
|
|
13
|
+
):
|
|
14
|
+
if custom_prompt:
|
|
15
|
+
system_prompt = custom_prompt
|
|
22
16
|
else:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
llm_config = get_llm_config()
|
|
18
|
+
prompt_path = llm_config.graph_prompt_path
|
|
19
|
+
|
|
20
|
+
# Check if the prompt path is an absolute path or just a filename
|
|
21
|
+
if os.path.isabs(prompt_path):
|
|
22
|
+
# directory containing the file
|
|
23
|
+
base_directory = os.path.dirname(prompt_path)
|
|
24
|
+
# just the filename itself
|
|
25
|
+
prompt_path = os.path.basename(prompt_path)
|
|
26
|
+
else:
|
|
27
|
+
base_directory = None
|
|
28
|
+
|
|
29
|
+
system_prompt = LLMGateway.render_prompt(prompt_path, {}, base_directory=base_directory)
|
|
26
30
|
|
|
27
31
|
content_graph = await LLMGateway.acreate_structured_output(
|
|
28
32
|
content, system_prompt, response_model
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py
CHANGED
|
@@ -23,7 +23,7 @@ class AnthropicAdapter(LLMInterface):
|
|
|
23
23
|
name = "Anthropic"
|
|
24
24
|
model: str
|
|
25
25
|
|
|
26
|
-
def __init__(self,
|
|
26
|
+
def __init__(self, max_completion_tokens: int, model: str = None):
|
|
27
27
|
import anthropic
|
|
28
28
|
|
|
29
29
|
self.aclient = instructor.patch(
|
|
@@ -31,7 +31,7 @@ class AnthropicAdapter(LLMInterface):
|
|
|
31
31
|
)
|
|
32
32
|
|
|
33
33
|
self.model = model
|
|
34
|
-
self.
|
|
34
|
+
self.max_completion_tokens = max_completion_tokens
|
|
35
35
|
|
|
36
36
|
@sleep_and_retry_async()
|
|
37
37
|
@rate_limit_async
|
|
@@ -57,7 +57,7 @@ class AnthropicAdapter(LLMInterface):
|
|
|
57
57
|
|
|
58
58
|
return await self.aclient(
|
|
59
59
|
model=self.model,
|
|
60
|
-
|
|
60
|
+
max_completion_tokens=4096,
|
|
61
61
|
max_retries=5,
|
|
62
62
|
messages=[
|
|
63
63
|
{
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py
CHANGED
|
@@ -34,7 +34,7 @@ class GeminiAdapter(LLMInterface):
|
|
|
34
34
|
self,
|
|
35
35
|
api_key: str,
|
|
36
36
|
model: str,
|
|
37
|
-
|
|
37
|
+
max_completion_tokens: int,
|
|
38
38
|
endpoint: Optional[str] = None,
|
|
39
39
|
api_version: Optional[str] = None,
|
|
40
40
|
streaming: bool = False,
|
|
@@ -44,7 +44,7 @@ class GeminiAdapter(LLMInterface):
|
|
|
44
44
|
self.endpoint = endpoint
|
|
45
45
|
self.api_version = api_version
|
|
46
46
|
self.streaming = streaming
|
|
47
|
-
self.
|
|
47
|
+
self.max_completion_tokens = max_completion_tokens
|
|
48
48
|
|
|
49
49
|
@observe(as_type="generation")
|
|
50
50
|
@sleep_and_retry_async()
|
|
@@ -90,7 +90,7 @@ class GeminiAdapter(LLMInterface):
|
|
|
90
90
|
model=f"{self.model}",
|
|
91
91
|
messages=messages,
|
|
92
92
|
api_key=self.api_key,
|
|
93
|
-
|
|
93
|
+
max_completion_tokens=self.max_completion_tokens,
|
|
94
94
|
temperature=0.1,
|
|
95
95
|
response_format=response_schema,
|
|
96
96
|
timeout=100,
|
|
@@ -41,7 +41,7 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
41
41
|
api_key: str,
|
|
42
42
|
model: str,
|
|
43
43
|
name: str,
|
|
44
|
-
|
|
44
|
+
max_completion_tokens: int,
|
|
45
45
|
fallback_model: str = None,
|
|
46
46
|
fallback_api_key: str = None,
|
|
47
47
|
fallback_endpoint: str = None,
|
|
@@ -50,7 +50,7 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
50
50
|
self.model = model
|
|
51
51
|
self.api_key = api_key
|
|
52
52
|
self.endpoint = endpoint
|
|
53
|
-
self.
|
|
53
|
+
self.max_completion_tokens = max_completion_tokens
|
|
54
54
|
|
|
55
55
|
self.fallback_model = fallback_model
|
|
56
56
|
self.fallback_api_key = fallback_api_key
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py
CHANGED
|
@@ -54,11 +54,15 @@ def get_llm_client():
|
|
|
54
54
|
# Check if max_token value is defined in liteLLM for given model
|
|
55
55
|
# if not use value from cognee configuration
|
|
56
56
|
from cognee.infrastructure.llm.utils import (
|
|
57
|
-
|
|
57
|
+
get_model_max_completion_tokens,
|
|
58
58
|
) # imported here to avoid circular imports
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
model_max_completion_tokens = get_model_max_completion_tokens(llm_config.llm_model)
|
|
61
|
+
max_completion_tokens = (
|
|
62
|
+
model_max_completion_tokens
|
|
63
|
+
if model_max_completion_tokens
|
|
64
|
+
else llm_config.llm_max_completion_tokens
|
|
65
|
+
)
|
|
62
66
|
|
|
63
67
|
if provider == LLMProvider.OPENAI:
|
|
64
68
|
if llm_config.llm_api_key is None:
|
|
@@ -74,7 +78,7 @@ def get_llm_client():
|
|
|
74
78
|
api_version=llm_config.llm_api_version,
|
|
75
79
|
model=llm_config.llm_model,
|
|
76
80
|
transcription_model=llm_config.transcription_model,
|
|
77
|
-
|
|
81
|
+
max_completion_tokens=max_completion_tokens,
|
|
78
82
|
streaming=llm_config.llm_streaming,
|
|
79
83
|
fallback_api_key=llm_config.fallback_api_key,
|
|
80
84
|
fallback_endpoint=llm_config.fallback_endpoint,
|
|
@@ -94,7 +98,7 @@ def get_llm_client():
|
|
|
94
98
|
llm_config.llm_api_key,
|
|
95
99
|
llm_config.llm_model,
|
|
96
100
|
"Ollama",
|
|
97
|
-
|
|
101
|
+
max_completion_tokens=max_completion_tokens,
|
|
98
102
|
)
|
|
99
103
|
|
|
100
104
|
elif provider == LLMProvider.ANTHROPIC:
|
|
@@ -102,7 +106,9 @@ def get_llm_client():
|
|
|
102
106
|
AnthropicAdapter,
|
|
103
107
|
)
|
|
104
108
|
|
|
105
|
-
return AnthropicAdapter(
|
|
109
|
+
return AnthropicAdapter(
|
|
110
|
+
max_completion_tokens=max_completion_tokens, model=llm_config.llm_model
|
|
111
|
+
)
|
|
106
112
|
|
|
107
113
|
elif provider == LLMProvider.CUSTOM:
|
|
108
114
|
if llm_config.llm_api_key is None:
|
|
@@ -117,7 +123,7 @@ def get_llm_client():
|
|
|
117
123
|
llm_config.llm_api_key,
|
|
118
124
|
llm_config.llm_model,
|
|
119
125
|
"Custom",
|
|
120
|
-
|
|
126
|
+
max_completion_tokens=max_completion_tokens,
|
|
121
127
|
fallback_api_key=llm_config.fallback_api_key,
|
|
122
128
|
fallback_endpoint=llm_config.fallback_endpoint,
|
|
123
129
|
fallback_model=llm_config.fallback_model,
|
|
@@ -134,7 +140,7 @@ def get_llm_client():
|
|
|
134
140
|
return GeminiAdapter(
|
|
135
141
|
api_key=llm_config.llm_api_key,
|
|
136
142
|
model=llm_config.llm_model,
|
|
137
|
-
|
|
143
|
+
max_completion_tokens=max_completion_tokens,
|
|
138
144
|
endpoint=llm_config.llm_endpoint,
|
|
139
145
|
api_version=llm_config.llm_api_version,
|
|
140
146
|
streaming=llm_config.llm_streaming,
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py
CHANGED
|
@@ -30,16 +30,18 @@ class OllamaAPIAdapter(LLMInterface):
|
|
|
30
30
|
- model
|
|
31
31
|
- api_key
|
|
32
32
|
- endpoint
|
|
33
|
-
-
|
|
33
|
+
- max_completion_tokens
|
|
34
34
|
- aclient
|
|
35
35
|
"""
|
|
36
36
|
|
|
37
|
-
def __init__(
|
|
37
|
+
def __init__(
|
|
38
|
+
self, endpoint: str, api_key: str, model: str, name: str, max_completion_tokens: int
|
|
39
|
+
):
|
|
38
40
|
self.name = name
|
|
39
41
|
self.model = model
|
|
40
42
|
self.api_key = api_key
|
|
41
43
|
self.endpoint = endpoint
|
|
42
|
-
self.
|
|
44
|
+
self.max_completion_tokens = max_completion_tokens
|
|
43
45
|
|
|
44
46
|
self.aclient = instructor.from_openai(
|
|
45
47
|
OpenAI(base_url=self.endpoint, api_key=self.api_key), mode=instructor.Mode.JSON
|
|
@@ -159,7 +161,7 @@ class OllamaAPIAdapter(LLMInterface):
|
|
|
159
161
|
],
|
|
160
162
|
}
|
|
161
163
|
],
|
|
162
|
-
|
|
164
|
+
max_completion_tokens=300,
|
|
163
165
|
)
|
|
164
166
|
|
|
165
167
|
# Ensure response is valid before accessing .choices[0].message.content
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py
CHANGED
|
@@ -64,7 +64,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
64
64
|
api_version: str,
|
|
65
65
|
model: str,
|
|
66
66
|
transcription_model: str,
|
|
67
|
-
|
|
67
|
+
max_completion_tokens: int,
|
|
68
68
|
streaming: bool = False,
|
|
69
69
|
fallback_model: str = None,
|
|
70
70
|
fallback_api_key: str = None,
|
|
@@ -77,7 +77,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
77
77
|
self.api_key = api_key
|
|
78
78
|
self.endpoint = endpoint
|
|
79
79
|
self.api_version = api_version
|
|
80
|
-
self.
|
|
80
|
+
self.max_completion_tokens = max_completion_tokens
|
|
81
81
|
self.streaming = streaming
|
|
82
82
|
|
|
83
83
|
self.fallback_model = fallback_model
|
|
@@ -301,7 +301,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
301
301
|
api_key=self.api_key,
|
|
302
302
|
api_base=self.endpoint,
|
|
303
303
|
api_version=self.api_version,
|
|
304
|
-
|
|
304
|
+
max_completion_tokens=300,
|
|
305
305
|
max_retries=self.MAX_RETRIES,
|
|
306
306
|
)
|
|
307
307
|
|
|
@@ -17,10 +17,10 @@ class GeminiTokenizer(TokenizerInterface):
|
|
|
17
17
|
def __init__(
|
|
18
18
|
self,
|
|
19
19
|
model: str,
|
|
20
|
-
|
|
20
|
+
max_completion_tokens: int = 3072,
|
|
21
21
|
):
|
|
22
22
|
self.model = model
|
|
23
|
-
self.
|
|
23
|
+
self.max_completion_tokens = max_completion_tokens
|
|
24
24
|
|
|
25
25
|
# Get LLM API key from config
|
|
26
26
|
from cognee.infrastructure.databases.vector.embeddings.config import get_embedding_config
|
|
@@ -14,17 +14,17 @@ class HuggingFaceTokenizer(TokenizerInterface):
|
|
|
14
14
|
|
|
15
15
|
Instance variables include:
|
|
16
16
|
- model: str
|
|
17
|
-
-
|
|
17
|
+
- max_completion_tokens: int
|
|
18
18
|
- tokenizer: AutoTokenizer
|
|
19
19
|
"""
|
|
20
20
|
|
|
21
21
|
def __init__(
|
|
22
22
|
self,
|
|
23
23
|
model: str,
|
|
24
|
-
|
|
24
|
+
max_completion_tokens: int = 512,
|
|
25
25
|
):
|
|
26
26
|
self.model = model
|
|
27
|
-
self.
|
|
27
|
+
self.max_completion_tokens = max_completion_tokens
|
|
28
28
|
|
|
29
29
|
# Import here to make it an optional dependency
|
|
30
30
|
from transformers import AutoTokenizer
|
|
@@ -16,17 +16,17 @@ class MistralTokenizer(TokenizerInterface):
|
|
|
16
16
|
|
|
17
17
|
Instance variables include:
|
|
18
18
|
- model: str
|
|
19
|
-
-
|
|
19
|
+
- max_completion_tokens: int
|
|
20
20
|
- tokenizer: MistralTokenizer
|
|
21
21
|
"""
|
|
22
22
|
|
|
23
23
|
def __init__(
|
|
24
24
|
self,
|
|
25
25
|
model: str,
|
|
26
|
-
|
|
26
|
+
max_completion_tokens: int = 3072,
|
|
27
27
|
):
|
|
28
28
|
self.model = model
|
|
29
|
-
self.
|
|
29
|
+
self.max_completion_tokens = max_completion_tokens
|
|
30
30
|
|
|
31
31
|
# Import here to make it an optional dependency
|
|
32
32
|
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
|
|
@@ -13,10 +13,10 @@ class TikTokenTokenizer(TokenizerInterface):
|
|
|
13
13
|
def __init__(
|
|
14
14
|
self,
|
|
15
15
|
model: Optional[str] = None,
|
|
16
|
-
|
|
16
|
+
max_completion_tokens: int = 8191,
|
|
17
17
|
):
|
|
18
18
|
self.model = model
|
|
19
|
-
self.
|
|
19
|
+
self.max_completion_tokens = max_completion_tokens
|
|
20
20
|
# Initialize TikToken for GPT based on model
|
|
21
21
|
if model:
|
|
22
22
|
self.tokenizer = tiktoken.encoding_for_model(self.model)
|
|
@@ -93,9 +93,9 @@ class TikTokenTokenizer(TokenizerInterface):
|
|
|
93
93
|
num_tokens = len(self.tokenizer.encode(text))
|
|
94
94
|
return num_tokens
|
|
95
95
|
|
|
96
|
-
def
|
|
96
|
+
def trim_text_to_max_completion_tokens(self, text: str) -> str:
|
|
97
97
|
"""
|
|
98
|
-
Trim the text so that the number of tokens does not exceed
|
|
98
|
+
Trim the text so that the number of tokens does not exceed max_completion_tokens.
|
|
99
99
|
|
|
100
100
|
Parameters:
|
|
101
101
|
-----------
|
|
@@ -111,13 +111,13 @@ class TikTokenTokenizer(TokenizerInterface):
|
|
|
111
111
|
num_tokens = self.count_tokens(text)
|
|
112
112
|
|
|
113
113
|
# If the number of tokens is within the limit, return the text as is
|
|
114
|
-
if num_tokens <= self.
|
|
114
|
+
if num_tokens <= self.max_completion_tokens:
|
|
115
115
|
return text
|
|
116
116
|
|
|
117
117
|
# If the number exceeds the limit, trim the text
|
|
118
118
|
# This is a simple trim, it may cut words in half; consider using word boundaries for a cleaner cut
|
|
119
119
|
encoded_text = self.tokenizer.encode(text)
|
|
120
|
-
trimmed_encoded_text = encoded_text[: self.
|
|
120
|
+
trimmed_encoded_text = encoded_text[: self.max_completion_tokens]
|
|
121
121
|
# Decoding the trimmed text
|
|
122
122
|
trimmed_text = self.tokenizer.decode(trimmed_encoded_text)
|
|
123
123
|
return trimmed_text
|
|
@@ -32,13 +32,13 @@ def get_max_chunk_tokens():
|
|
|
32
32
|
|
|
33
33
|
# We need to make sure chunk size won't take more than half of LLM max context token size
|
|
34
34
|
# but it also can't be bigger than the embedding engine max token size
|
|
35
|
-
llm_cutoff_point = llm_client.
|
|
36
|
-
max_chunk_tokens = min(embedding_engine.
|
|
35
|
+
llm_cutoff_point = llm_client.max_completion_tokens // 2 # Round down the division
|
|
36
|
+
max_chunk_tokens = min(embedding_engine.max_completion_tokens, llm_cutoff_point)
|
|
37
37
|
|
|
38
38
|
return max_chunk_tokens
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
def
|
|
41
|
+
def get_model_max_completion_tokens(model_name: str):
|
|
42
42
|
"""
|
|
43
43
|
Retrieve the maximum token limit for a specified model name if it exists.
|
|
44
44
|
|
|
@@ -56,15 +56,15 @@ def get_model_max_tokens(model_name: str):
|
|
|
56
56
|
|
|
57
57
|
Number of max tokens of model, or None if model is unknown
|
|
58
58
|
"""
|
|
59
|
-
|
|
59
|
+
max_completion_tokens = None
|
|
60
60
|
|
|
61
61
|
if model_name in litellm.model_cost:
|
|
62
|
-
|
|
63
|
-
logger.debug(f"Max input tokens for {model_name}: {
|
|
62
|
+
max_completion_tokens = litellm.model_cost[model_name]["max_tokens"]
|
|
63
|
+
logger.debug(f"Max input tokens for {model_name}: {max_completion_tokens}")
|
|
64
64
|
else:
|
|
65
65
|
logger.info("Model not found in LiteLLM's model_cost.")
|
|
66
66
|
|
|
67
|
-
return
|
|
67
|
+
return max_completion_tokens
|
|
68
68
|
|
|
69
69
|
|
|
70
70
|
async def test_llm_connection():
|
|
@@ -7,6 +7,7 @@ from .get_datasets import get_datasets
|
|
|
7
7
|
from .get_datasets_by_name import get_datasets_by_name
|
|
8
8
|
from .get_dataset_data import get_dataset_data
|
|
9
9
|
from .get_authorized_dataset import get_authorized_dataset
|
|
10
|
+
from .get_authorized_dataset_by_name import get_authorized_dataset_by_name
|
|
10
11
|
from .get_data import get_data
|
|
11
12
|
from .get_unique_dataset_id import get_unique_dataset_id
|
|
12
13
|
from .get_authorized_existing_datasets import get_authorized_existing_datasets
|
|
@@ -18,6 +19,7 @@ from .delete_data import delete_data
|
|
|
18
19
|
|
|
19
20
|
# Create
|
|
20
21
|
from .load_or_create_datasets import load_or_create_datasets
|
|
22
|
+
from .create_authorized_dataset import create_authorized_dataset
|
|
21
23
|
|
|
22
24
|
# Check
|
|
23
25
|
from .check_dataset_name import check_dataset_name
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from cognee.infrastructure.databases.relational import get_relational_engine
|
|
2
|
+
from cognee.modules.users.models import User
|
|
3
|
+
from cognee.modules.data.models import Dataset
|
|
4
|
+
from cognee.modules.users.permissions.methods import give_permission_on_dataset
|
|
5
|
+
from .create_dataset import create_dataset
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
async def create_authorized_dataset(dataset_name: str, user: User) -> Dataset:
|
|
9
|
+
db_engine = get_relational_engine()
|
|
10
|
+
|
|
11
|
+
async with db_engine.get_async_session() as session:
|
|
12
|
+
new_dataset = await create_dataset(dataset_name, user, session)
|
|
13
|
+
|
|
14
|
+
await give_permission_on_dataset(user, new_dataset.id, "read")
|
|
15
|
+
await give_permission_on_dataset(user, new_dataset.id, "write")
|
|
16
|
+
await give_permission_on_dataset(user, new_dataset.id, "delete")
|
|
17
|
+
await give_permission_on_dataset(user, new_dataset.id, "share")
|
|
18
|
+
|
|
19
|
+
return new_dataset
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
from typing import Optional
|
|
2
1
|
from uuid import UUID
|
|
3
|
-
from
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from cognee.modules.users.models import User
|
|
5
|
+
from cognee.modules.data.methods.get_authorized_existing_datasets import (
|
|
6
|
+
get_authorized_existing_datasets,
|
|
7
|
+
)
|
|
4
8
|
from ..models import Dataset
|
|
5
9
|
|
|
6
10
|
|
|
7
11
|
async def get_authorized_dataset(
|
|
8
|
-
|
|
12
|
+
user: User, dataset_id: UUID, permission_type="read"
|
|
9
13
|
) -> Optional[Dataset]:
|
|
10
14
|
"""
|
|
11
15
|
Get a specific dataset with permissions for a user.
|
|
@@ -18,6 +22,8 @@ async def get_authorized_dataset(
|
|
|
18
22
|
Returns:
|
|
19
23
|
Optional[Dataset]: dataset with permissions
|
|
20
24
|
"""
|
|
21
|
-
|
|
25
|
+
authorized_datasets = await get_authorized_existing_datasets(
|
|
26
|
+
[dataset_id], permission_type, user
|
|
27
|
+
)
|
|
22
28
|
|
|
23
|
-
return
|
|
29
|
+
return authorized_datasets[0] if authorized_datasets else None
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
from cognee.modules.users.models import User
|
|
4
|
+
from cognee.modules.data.methods.get_authorized_existing_datasets import (
|
|
5
|
+
get_authorized_existing_datasets,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
from ..models import Dataset
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def get_authorized_dataset_by_name(
|
|
12
|
+
dataset_name: str, user: User, permission_type: str
|
|
13
|
+
) -> Optional[Dataset]:
|
|
14
|
+
authorized_datasets = await get_authorized_existing_datasets([], permission_type, user)
|
|
15
|
+
|
|
16
|
+
return next((dataset for dataset in authorized_datasets if dataset.name == dataset_name), None)
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
from typing import List, Union
|
|
2
2
|
from uuid import UUID
|
|
3
3
|
|
|
4
|
-
from cognee.infrastructure.databases.relational import get_relational_engine
|
|
5
4
|
from cognee.modules.data.models import Dataset
|
|
6
|
-
from cognee.modules.data.methods import
|
|
7
|
-
from cognee.modules.data.methods import get_unique_dataset_id
|
|
5
|
+
from cognee.modules.data.methods import create_authorized_dataset
|
|
8
6
|
from cognee.modules.data.exceptions import DatasetNotFoundError
|
|
9
|
-
from cognee.modules.users.permissions.methods import give_permission_on_dataset
|
|
10
7
|
|
|
11
8
|
|
|
12
9
|
async def load_or_create_datasets(
|
|
@@ -34,22 +31,7 @@ async def load_or_create_datasets(
|
|
|
34
31
|
if isinstance(identifier, UUID):
|
|
35
32
|
raise DatasetNotFoundError(f"Dataset with given UUID does not exist: {identifier}")
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
new_dataset = Dataset(
|
|
39
|
-
id=await get_unique_dataset_id(dataset_name=identifier, user=user),
|
|
40
|
-
name=identifier,
|
|
41
|
-
owner_id=user.id,
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
# Save dataset to database
|
|
45
|
-
db_engine = get_relational_engine()
|
|
46
|
-
async with db_engine.get_async_session() as session:
|
|
47
|
-
await create_dataset(identifier, user, session)
|
|
48
|
-
|
|
49
|
-
await give_permission_on_dataset(user, new_dataset.id, "read")
|
|
50
|
-
await give_permission_on_dataset(user, new_dataset.id, "write")
|
|
51
|
-
await give_permission_on_dataset(user, new_dataset.id, "delete")
|
|
52
|
-
await give_permission_on_dataset(user, new_dataset.id, "share")
|
|
34
|
+
new_dataset = await create_authorized_dataset(identifier, user)
|
|
53
35
|
|
|
54
36
|
result.append(new_dataset)
|
|
55
37
|
|
|
@@ -3,10 +3,11 @@ from cognee.infrastructure.databases.graph import get_graph_engine
|
|
|
3
3
|
from cognee.context_global_variables import set_database_global_context_variables
|
|
4
4
|
from cognee.modules.data.exceptions.exceptions import DatasetNotFoundError
|
|
5
5
|
from cognee.modules.data.methods import get_authorized_dataset
|
|
6
|
+
from cognee.modules.users.models import User
|
|
6
7
|
|
|
7
8
|
|
|
8
|
-
async def get_formatted_graph_data(dataset_id: UUID,
|
|
9
|
-
dataset = await get_authorized_dataset(
|
|
9
|
+
async def get_formatted_graph_data(dataset_id: UUID, user: User):
|
|
10
|
+
dataset = await get_authorized_dataset(user, dataset_id)
|
|
10
11
|
if not dataset:
|
|
11
12
|
raise DatasetNotFoundError(message="Dataset not found.")
|
|
12
13
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from fastapi import status
|
|
2
|
+
from cognee.exceptions import CogneeValidationError
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class WrongTaskTypeError(CogneeValidationError):
|
|
6
|
+
"""
|
|
7
|
+
Raised when the tasks argument is not a list of Task class instances.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
message: str = "tasks argument must be a list, containing Task class instances.",
|
|
13
|
+
name: str = "WrongTaskTypeError",
|
|
14
|
+
status_code=status.HTTP_400_BAD_REQUEST,
|
|
15
|
+
):
|
|
16
|
+
self.message = message
|
|
17
|
+
self.name = name
|
|
18
|
+
self.status_code = status_code
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .validate_pipeline_tasks import validate_pipeline_tasks
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
from typing import Union, Optional
|
|
2
|
+
from cognee.modules.data.models import Dataset
|
|
3
|
+
from cognee.modules.data.models import Data
|
|
4
|
+
from cognee.modules.pipelines.models import PipelineRunStatus
|
|
5
|
+
from cognee.modules.pipelines.operations.get_pipeline_status import get_pipeline_status
|
|
6
|
+
from cognee.modules.pipelines.methods import get_pipeline_run_by_dataset
|
|
7
|
+
from cognee.shared.logging_utils import get_logger
|
|
8
|
+
|
|
9
|
+
from cognee.modules.pipelines.models.PipelineRunInfo import (
|
|
10
|
+
PipelineRunCompleted,
|
|
11
|
+
PipelineRunStarted,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
logger = get_logger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
async def check_pipeline_run_qualification(
|
|
18
|
+
dataset: Dataset, data: list[Data], pipeline_name: str
|
|
19
|
+
) -> Optional[Union[PipelineRunStarted, PipelineRunCompleted]]:
|
|
20
|
+
"""
|
|
21
|
+
Function used to determine if pipeline is currently being processed or was already processed.
|
|
22
|
+
In case pipeline was or is being processed return value is returned and current pipline execution should be stopped.
|
|
23
|
+
In case pipeline is not or was not processed there will be no return value and pipeline processing can start.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
dataset: Dataset object
|
|
27
|
+
data: List of Data
|
|
28
|
+
pipeline_name: pipeline name
|
|
29
|
+
|
|
30
|
+
Returns: Pipeline state if it is being processed or was already processed
|
|
31
|
+
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
# async with update_status_lock: TODO: Add UI lock to prevent multiple backend requests
|
|
35
|
+
if isinstance(dataset, Dataset):
|
|
36
|
+
task_status = await get_pipeline_status([dataset.id], pipeline_name)
|
|
37
|
+
else:
|
|
38
|
+
task_status = {}
|
|
39
|
+
|
|
40
|
+
if str(dataset.id) in task_status:
|
|
41
|
+
if task_status[str(dataset.id)] == PipelineRunStatus.DATASET_PROCESSING_STARTED:
|
|
42
|
+
logger.info("Dataset %s is already being processed.", dataset.id)
|
|
43
|
+
pipeline_run = await get_pipeline_run_by_dataset(dataset.id, pipeline_name)
|
|
44
|
+
return PipelineRunStarted(
|
|
45
|
+
pipeline_run_id=pipeline_run.pipeline_run_id,
|
|
46
|
+
dataset_id=dataset.id,
|
|
47
|
+
dataset_name=dataset.name,
|
|
48
|
+
payload=data,
|
|
49
|
+
)
|
|
50
|
+
elif task_status[str(dataset.id)] == PipelineRunStatus.DATASET_PROCESSING_COMPLETED:
|
|
51
|
+
logger.info("Dataset %s is already processed.", dataset.id)
|
|
52
|
+
pipeline_run = await get_pipeline_run_by_dataset(dataset.id, pipeline_name)
|
|
53
|
+
return PipelineRunCompleted(
|
|
54
|
+
pipeline_run_id=pipeline_run.pipeline_run_id,
|
|
55
|
+
dataset_id=dataset.id,
|
|
56
|
+
dataset_name=dataset.name,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return
|