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