cognee 0.4.0__py3-none-any.whl → 0.5.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 +1 -0
- cognee/api/client.py +9 -5
- cognee/api/v1/add/add.py +2 -1
- cognee/api/v1/add/routers/get_add_router.py +3 -1
- cognee/api/v1/cognify/cognify.py +24 -16
- cognee/api/v1/cognify/routers/__init__.py +0 -1
- cognee/api/v1/cognify/routers/get_cognify_router.py +30 -1
- cognee/api/v1/datasets/routers/get_datasets_router.py +3 -3
- cognee/api/v1/ontologies/__init__.py +4 -0
- cognee/api/v1/ontologies/ontologies.py +158 -0
- cognee/api/v1/ontologies/routers/__init__.py +0 -0
- cognee/api/v1/ontologies/routers/get_ontology_router.py +109 -0
- cognee/api/v1/permissions/routers/get_permissions_router.py +41 -1
- cognee/api/v1/search/search.py +4 -0
- cognee/api/v1/ui/node_setup.py +360 -0
- cognee/api/v1/ui/npm_utils.py +50 -0
- cognee/api/v1/ui/ui.py +38 -68
- cognee/cli/commands/cognify_command.py +8 -1
- cognee/cli/config.py +1 -1
- cognee/context_global_variables.py +86 -9
- cognee/eval_framework/Dockerfile +29 -0
- cognee/eval_framework/answer_generation/answer_generation_executor.py +10 -0
- cognee/eval_framework/answer_generation/run_question_answering_module.py +1 -1
- cognee/eval_framework/corpus_builder/task_getters/get_cascade_graph_tasks.py +0 -2
- cognee/eval_framework/corpus_builder/task_getters/get_default_tasks_by_indices.py +4 -4
- cognee/eval_framework/eval_config.py +2 -2
- cognee/eval_framework/modal_run_eval.py +16 -28
- cognee/infrastructure/databases/cache/config.py +3 -1
- cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py +151 -0
- cognee/infrastructure/databases/cache/get_cache_engine.py +20 -10
- cognee/infrastructure/databases/dataset_database_handler/__init__.py +3 -0
- cognee/infrastructure/databases/dataset_database_handler/dataset_database_handler_interface.py +80 -0
- cognee/infrastructure/databases/dataset_database_handler/supported_dataset_database_handlers.py +18 -0
- cognee/infrastructure/databases/dataset_database_handler/use_dataset_database_handler.py +10 -0
- cognee/infrastructure/databases/exceptions/exceptions.py +16 -0
- cognee/infrastructure/databases/graph/config.py +7 -0
- cognee/infrastructure/databases/graph/get_graph_engine.py +3 -0
- cognee/infrastructure/databases/graph/graph_db_interface.py +15 -0
- cognee/infrastructure/databases/graph/kuzu/KuzuDatasetDatabaseHandler.py +81 -0
- cognee/infrastructure/databases/graph/kuzu/adapter.py +228 -0
- cognee/infrastructure/databases/graph/neo4j_driver/Neo4jAuraDevDatasetDatabaseHandler.py +168 -0
- cognee/infrastructure/databases/graph/neo4j_driver/adapter.py +80 -1
- cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py +9 -0
- cognee/infrastructure/databases/utils/__init__.py +3 -0
- cognee/infrastructure/databases/utils/get_graph_dataset_database_handler.py +10 -0
- cognee/infrastructure/databases/utils/get_or_create_dataset_database.py +66 -18
- cognee/infrastructure/databases/utils/get_vector_dataset_database_handler.py +10 -0
- cognee/infrastructure/databases/utils/resolve_dataset_database_connection_info.py +30 -0
- cognee/infrastructure/databases/vector/config.py +5 -0
- cognee/infrastructure/databases/vector/create_vector_engine.py +6 -1
- cognee/infrastructure/databases/vector/embeddings/FastembedEmbeddingEngine.py +8 -6
- cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +9 -7
- cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +11 -10
- cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py +2 -0
- cognee/infrastructure/databases/vector/lancedb/LanceDBDatasetDatabaseHandler.py +50 -0
- cognee/infrastructure/databases/vector/vector_db_interface.py +35 -0
- cognee/infrastructure/engine/models/Edge.py +13 -1
- cognee/infrastructure/files/storage/s3_config.py +2 -0
- cognee/infrastructure/files/utils/guess_file_type.py +4 -0
- cognee/infrastructure/llm/LLMGateway.py +5 -2
- cognee/infrastructure/llm/config.py +37 -0
- cognee/infrastructure/llm/extraction/knowledge_graph/extract_content_graph.py +2 -2
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/acreate_structured_output.py +23 -8
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +22 -18
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/bedrock/__init__.py +5 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/bedrock/adapter.py +153 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +47 -38
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +46 -37
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +20 -10
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/mistral/adapter.py +23 -11
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py +36 -23
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +47 -36
- cognee/infrastructure/loaders/LoaderEngine.py +1 -0
- cognee/infrastructure/loaders/core/__init__.py +2 -1
- cognee/infrastructure/loaders/core/csv_loader.py +93 -0
- cognee/infrastructure/loaders/core/text_loader.py +1 -2
- cognee/infrastructure/loaders/external/advanced_pdf_loader.py +0 -9
- cognee/infrastructure/loaders/supported_loaders.py +2 -1
- cognee/memify_pipelines/create_triplet_embeddings.py +53 -0
- cognee/memify_pipelines/persist_sessions_in_knowledge_graph.py +55 -0
- cognee/modules/chunking/CsvChunker.py +35 -0
- cognee/modules/chunking/models/DocumentChunk.py +2 -1
- cognee/modules/chunking/text_chunker_with_overlap.py +124 -0
- cognee/modules/cognify/config.py +2 -0
- cognee/modules/data/deletion/prune_system.py +52 -2
- cognee/modules/data/methods/__init__.py +1 -0
- cognee/modules/data/methods/create_dataset.py +4 -2
- cognee/modules/data/methods/delete_dataset.py +26 -0
- cognee/modules/data/methods/get_dataset_ids.py +5 -1
- cognee/modules/data/methods/get_unique_data_id.py +68 -0
- cognee/modules/data/methods/get_unique_dataset_id.py +66 -4
- cognee/modules/data/models/Dataset.py +2 -0
- cognee/modules/data/processing/document_types/CsvDocument.py +33 -0
- cognee/modules/data/processing/document_types/__init__.py +1 -0
- cognee/modules/engine/models/Triplet.py +9 -0
- cognee/modules/engine/models/__init__.py +1 -0
- cognee/modules/graph/cognee_graph/CogneeGraph.py +89 -39
- cognee/modules/graph/cognee_graph/CogneeGraphElements.py +8 -3
- cognee/modules/graph/utils/expand_with_nodes_and_edges.py +19 -2
- cognee/modules/graph/utils/resolve_edges_to_text.py +48 -49
- cognee/modules/ingestion/identify.py +4 -4
- cognee/modules/memify/memify.py +1 -7
- cognee/modules/notebooks/operations/run_in_local_sandbox.py +3 -0
- cognee/modules/ontology/rdf_xml/RDFLibOntologyResolver.py +55 -23
- cognee/modules/pipelines/operations/pipeline.py +18 -2
- cognee/modules/pipelines/operations/run_tasks_data_item.py +1 -1
- cognee/modules/retrieval/EntityCompletionRetriever.py +10 -3
- cognee/modules/retrieval/__init__.py +1 -1
- cognee/modules/retrieval/base_graph_retriever.py +7 -3
- cognee/modules/retrieval/base_retriever.py +7 -3
- cognee/modules/retrieval/completion_retriever.py +11 -4
- cognee/modules/retrieval/graph_completion_context_extension_retriever.py +10 -2
- cognee/modules/retrieval/graph_completion_cot_retriever.py +18 -51
- cognee/modules/retrieval/graph_completion_retriever.py +14 -1
- cognee/modules/retrieval/graph_summary_completion_retriever.py +4 -0
- cognee/modules/retrieval/register_retriever.py +10 -0
- cognee/modules/retrieval/registered_community_retrievers.py +1 -0
- cognee/modules/retrieval/temporal_retriever.py +13 -2
- cognee/modules/retrieval/triplet_retriever.py +182 -0
- cognee/modules/retrieval/utils/brute_force_triplet_search.py +43 -11
- cognee/modules/retrieval/utils/completion.py +2 -22
- cognee/modules/run_custom_pipeline/__init__.py +1 -0
- cognee/modules/run_custom_pipeline/run_custom_pipeline.py +76 -0
- cognee/modules/search/methods/get_search_type_tools.py +54 -8
- cognee/modules/search/methods/no_access_control_search.py +4 -0
- cognee/modules/search/methods/search.py +26 -3
- cognee/modules/search/types/SearchType.py +1 -1
- cognee/modules/settings/get_settings.py +19 -0
- cognee/modules/users/methods/create_user.py +12 -27
- cognee/modules/users/methods/get_authenticated_user.py +3 -2
- cognee/modules/users/methods/get_default_user.py +4 -2
- cognee/modules/users/methods/get_user.py +1 -1
- cognee/modules/users/methods/get_user_by_email.py +1 -1
- cognee/modules/users/models/DatasetDatabase.py +24 -3
- cognee/modules/users/models/Tenant.py +6 -7
- cognee/modules/users/models/User.py +6 -5
- cognee/modules/users/models/UserTenant.py +12 -0
- cognee/modules/users/models/__init__.py +1 -0
- cognee/modules/users/permissions/methods/get_all_user_permission_datasets.py +13 -13
- cognee/modules/users/roles/methods/add_user_to_role.py +3 -1
- cognee/modules/users/tenants/methods/__init__.py +1 -0
- cognee/modules/users/tenants/methods/add_user_to_tenant.py +21 -12
- cognee/modules/users/tenants/methods/create_tenant.py +22 -8
- cognee/modules/users/tenants/methods/select_tenant.py +62 -0
- cognee/shared/logging_utils.py +6 -0
- cognee/shared/rate_limiting.py +30 -0
- cognee/tasks/chunks/__init__.py +1 -0
- cognee/tasks/chunks/chunk_by_row.py +94 -0
- cognee/tasks/documents/__init__.py +0 -1
- cognee/tasks/documents/classify_documents.py +2 -0
- cognee/tasks/feedback/generate_improved_answers.py +3 -3
- cognee/tasks/graph/extract_graph_from_data.py +9 -10
- cognee/tasks/ingestion/ingest_data.py +1 -1
- cognee/tasks/memify/__init__.py +2 -0
- cognee/tasks/memify/cognify_session.py +41 -0
- cognee/tasks/memify/extract_user_sessions.py +73 -0
- cognee/tasks/memify/get_triplet_datapoints.py +289 -0
- cognee/tasks/storage/add_data_points.py +142 -2
- cognee/tasks/storage/index_data_points.py +33 -22
- cognee/tasks/storage/index_graph_edges.py +37 -57
- cognee/tests/integration/documents/CsvDocument_test.py +70 -0
- cognee/tests/integration/retrieval/test_triplet_retriever.py +84 -0
- cognee/tests/integration/tasks/test_add_data_points.py +139 -0
- cognee/tests/integration/tasks/test_get_triplet_datapoints.py +69 -0
- cognee/tests/integration/web_url_crawler/test_default_url_crawler.py +1 -1
- cognee/tests/integration/web_url_crawler/test_tavily_crawler.py +1 -1
- cognee/tests/integration/web_url_crawler/test_url_adding_e2e.py +13 -27
- cognee/tests/tasks/entity_extraction/entity_extraction_test.py +1 -1
- cognee/tests/test_add_docling_document.py +2 -2
- cognee/tests/test_cognee_server_start.py +84 -3
- cognee/tests/test_conversation_history.py +68 -5
- cognee/tests/test_data/example_with_header.csv +3 -0
- cognee/tests/test_dataset_database_handler.py +137 -0
- cognee/tests/test_dataset_delete.py +76 -0
- cognee/tests/test_edge_centered_payload.py +170 -0
- cognee/tests/test_edge_ingestion.py +27 -0
- cognee/tests/test_feedback_enrichment.py +1 -1
- cognee/tests/test_library.py +6 -4
- cognee/tests/test_load.py +62 -0
- cognee/tests/test_multi_tenancy.py +165 -0
- cognee/tests/test_parallel_databases.py +2 -0
- cognee/tests/test_pipeline_cache.py +164 -0
- cognee/tests/test_relational_db_migration.py +54 -2
- cognee/tests/test_search_db.py +44 -2
- cognee/tests/unit/api/test_conditional_authentication_endpoints.py +12 -3
- cognee/tests/unit/api/test_ontology_endpoint.py +252 -0
- cognee/tests/unit/infrastructure/databases/cache/test_cache_config.py +5 -0
- cognee/tests/unit/infrastructure/databases/test_index_data_points.py +27 -0
- cognee/tests/unit/infrastructure/databases/test_index_graph_edges.py +14 -16
- cognee/tests/unit/infrastructure/llm/test_llm_config.py +46 -0
- cognee/tests/unit/infrastructure/mock_embedding_engine.py +3 -7
- cognee/tests/unit/infrastructure/test_embedding_rate_limiting_realistic.py +0 -5
- cognee/tests/unit/modules/chunking/test_text_chunker.py +248 -0
- cognee/tests/unit/modules/chunking/test_text_chunker_with_overlap.py +324 -0
- cognee/tests/unit/modules/graph/cognee_graph_elements_test.py +2 -2
- cognee/tests/unit/modules/graph/cognee_graph_test.py +406 -0
- cognee/tests/unit/modules/memify_tasks/test_cognify_session.py +111 -0
- cognee/tests/unit/modules/memify_tasks/test_extract_user_sessions.py +175 -0
- cognee/tests/unit/modules/memify_tasks/test_get_triplet_datapoints.py +214 -0
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_cot_test.py +0 -51
- cognee/tests/unit/modules/retrieval/rag_completion_retriever_test.py +1 -0
- cognee/tests/unit/modules/retrieval/structured_output_test.py +204 -0
- cognee/tests/unit/modules/retrieval/summaries_retriever_test.py +1 -1
- cognee/tests/unit/modules/retrieval/temporal_retriever_test.py +0 -1
- cognee/tests/unit/modules/retrieval/test_brute_force_triplet_search.py +608 -0
- cognee/tests/unit/modules/retrieval/triplet_retriever_test.py +83 -0
- cognee/tests/unit/modules/users/test_conditional_authentication.py +0 -63
- cognee/tests/unit/processing/chunks/chunk_by_row_test.py +52 -0
- cognee/tests/unit/tasks/storage/test_add_data_points.py +288 -0
- {cognee-0.4.0.dist-info → cognee-0.5.0.dist-info}/METADATA +11 -6
- {cognee-0.4.0.dist-info → cognee-0.5.0.dist-info}/RECORD +215 -163
- {cognee-0.4.0.dist-info → cognee-0.5.0.dist-info}/WHEEL +1 -1
- {cognee-0.4.0.dist-info → cognee-0.5.0.dist-info}/entry_points.txt +0 -1
- cognee/api/v1/cognify/code_graph_pipeline.py +0 -119
- cognee/api/v1/cognify/routers/get_code_pipeline_router.py +0 -90
- cognee/infrastructure/databases/vector/embeddings/embedding_rate_limiter.py +0 -544
- cognee/modules/retrieval/code_retriever.py +0 -232
- cognee/tasks/code/enrich_dependency_graph_checker.py +0 -35
- cognee/tasks/code/get_local_dependencies_checker.py +0 -20
- cognee/tasks/code/get_repo_dependency_graph_checker.py +0 -35
- cognee/tasks/documents/check_permissions_on_dataset.py +0 -26
- cognee/tasks/repo_processor/__init__.py +0 -2
- cognee/tasks/repo_processor/get_local_dependencies.py +0 -335
- cognee/tasks/repo_processor/get_non_code_files.py +0 -158
- cognee/tasks/repo_processor/get_repo_file_dependencies.py +0 -243
- {cognee-0.4.0.dist-info → cognee-0.5.0.dist-info}/licenses/LICENSE +0 -0
- {cognee-0.4.0.dist-info → cognee-0.5.0.dist-info}/licenses/NOTICE.md +0 -0
|
@@ -38,6 +38,7 @@ class LLMConfig(BaseSettings):
|
|
|
38
38
|
"""
|
|
39
39
|
|
|
40
40
|
structured_output_framework: str = "instructor"
|
|
41
|
+
llm_instructor_mode: str = ""
|
|
41
42
|
llm_provider: str = "openai"
|
|
42
43
|
llm_model: str = "openai/gpt-5-mini"
|
|
43
44
|
llm_endpoint: str = ""
|
|
@@ -73,6 +74,41 @@ class LLMConfig(BaseSettings):
|
|
|
73
74
|
|
|
74
75
|
model_config = SettingsConfigDict(env_file=".env", extra="allow")
|
|
75
76
|
|
|
77
|
+
@model_validator(mode="after")
|
|
78
|
+
def strip_quotes_from_strings(self) -> "LLMConfig":
|
|
79
|
+
"""
|
|
80
|
+
Strip surrounding quotes from specific string fields that often come from
|
|
81
|
+
environment variables with extra quotes (e.g., via Docker's --env-file).
|
|
82
|
+
|
|
83
|
+
Only applies to known config keys where quotes are invalid or cause issues.
|
|
84
|
+
"""
|
|
85
|
+
string_fields_to_strip = [
|
|
86
|
+
"llm_api_key",
|
|
87
|
+
"llm_endpoint",
|
|
88
|
+
"llm_api_version",
|
|
89
|
+
"baml_llm_api_key",
|
|
90
|
+
"baml_llm_endpoint",
|
|
91
|
+
"baml_llm_api_version",
|
|
92
|
+
"fallback_api_key",
|
|
93
|
+
"fallback_endpoint",
|
|
94
|
+
"fallback_model",
|
|
95
|
+
"llm_provider",
|
|
96
|
+
"llm_model",
|
|
97
|
+
"baml_llm_provider",
|
|
98
|
+
"baml_llm_model",
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
cls = self.__class__
|
|
102
|
+
for field_name in string_fields_to_strip:
|
|
103
|
+
if field_name not in cls.model_fields:
|
|
104
|
+
continue
|
|
105
|
+
value = getattr(self, field_name, None)
|
|
106
|
+
if isinstance(value, str) and len(value) >= 2:
|
|
107
|
+
if value[0] == value[-1] and value[0] in ("'", '"'):
|
|
108
|
+
setattr(self, field_name, value[1:-1])
|
|
109
|
+
|
|
110
|
+
return self
|
|
111
|
+
|
|
76
112
|
def model_post_init(self, __context) -> None:
|
|
77
113
|
"""Initialize the BAML registry after the model is created."""
|
|
78
114
|
# Check if BAML is selected as structured output framework but not available
|
|
@@ -181,6 +217,7 @@ class LLMConfig(BaseSettings):
|
|
|
181
217
|
instance.
|
|
182
218
|
"""
|
|
183
219
|
return {
|
|
220
|
+
"llm_instructor_mode": self.llm_instructor_mode.lower(),
|
|
184
221
|
"provider": self.llm_provider,
|
|
185
222
|
"model": self.llm_model,
|
|
186
223
|
"endpoint": self.llm_endpoint,
|
|
@@ -10,7 +10,7 @@ from cognee.infrastructure.llm.config import (
|
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
async def extract_content_graph(
|
|
13
|
-
content: str, response_model: Type[BaseModel], custom_prompt: Optional[str] = None
|
|
13
|
+
content: str, response_model: Type[BaseModel], custom_prompt: Optional[str] = None, **kwargs
|
|
14
14
|
):
|
|
15
15
|
if custom_prompt:
|
|
16
16
|
system_prompt = custom_prompt
|
|
@@ -30,7 +30,7 @@ async def extract_content_graph(
|
|
|
30
30
|
system_prompt = render_prompt(prompt_path, {}, base_directory=base_directory)
|
|
31
31
|
|
|
32
32
|
content_graph = await LLMGateway.acreate_structured_output(
|
|
33
|
-
content, system_prompt, response_model
|
|
33
|
+
content, system_prompt, response_model, **kwargs
|
|
34
34
|
)
|
|
35
35
|
|
|
36
36
|
return content_graph
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
from typing import Type
|
|
3
|
-
from
|
|
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
|
+
)
|
|
4
11
|
|
|
12
|
+
from cognee.shared.logging_utils import get_logger
|
|
5
13
|
from cognee.infrastructure.llm.config import get_llm_config
|
|
6
14
|
from cognee.infrastructure.llm.structured_output_framework.baml.baml_src.extraction.create_dynamic_baml_type import (
|
|
7
15
|
create_dynamic_baml_type,
|
|
@@ -10,12 +18,18 @@ from cognee.infrastructure.llm.structured_output_framework.baml.baml_client.type
|
|
|
10
18
|
TypeBuilder,
|
|
11
19
|
)
|
|
12
20
|
from cognee.infrastructure.llm.structured_output_framework.baml.baml_client import b
|
|
13
|
-
from
|
|
14
|
-
|
|
21
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
22
|
+
import logging
|
|
15
23
|
|
|
16
24
|
logger = get_logger()
|
|
17
25
|
|
|
18
26
|
|
|
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
|
+
)
|
|
19
33
|
async def acreate_structured_output(
|
|
20
34
|
text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
21
35
|
):
|
|
@@ -45,11 +59,12 @@ async def acreate_structured_output(
|
|
|
45
59
|
tb = TypeBuilder()
|
|
46
60
|
type_builder = create_dynamic_baml_type(tb, tb.ResponseModel, response_model)
|
|
47
61
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
async with llm_rate_limiter_context_manager():
|
|
63
|
+
result = await b.AcreateStructuredOutput(
|
|
64
|
+
text_input=text_input,
|
|
65
|
+
system_prompt=system_prompt,
|
|
66
|
+
baml_options={"client_registry": config.baml_registry, "tb": type_builder},
|
|
67
|
+
)
|
|
53
68
|
|
|
54
69
|
# Transform BAML response to proper pydantic reponse model
|
|
55
70
|
if response_model is str:
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py
CHANGED
|
@@ -15,6 +15,7 @@ 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
|
|
18
19
|
from cognee.infrastructure.llm.config import get_llm_config
|
|
19
20
|
|
|
20
21
|
logger = get_logger()
|
|
@@ -28,13 +29,16 @@ class AnthropicAdapter(LLMInterface):
|
|
|
28
29
|
|
|
29
30
|
name = "Anthropic"
|
|
30
31
|
model: str
|
|
32
|
+
default_instructor_mode = "anthropic_tools"
|
|
31
33
|
|
|
32
|
-
def __init__(self, max_completion_tokens: int, model: str = None):
|
|
34
|
+
def __init__(self, max_completion_tokens: int, model: str = None, instructor_mode: str = None):
|
|
33
35
|
import anthropic
|
|
34
36
|
|
|
37
|
+
self.instructor_mode = instructor_mode if instructor_mode else self.default_instructor_mode
|
|
38
|
+
|
|
35
39
|
self.aclient = instructor.patch(
|
|
36
40
|
create=anthropic.AsyncAnthropic(api_key=get_llm_config().llm_api_key).messages.create,
|
|
37
|
-
mode=instructor.Mode.
|
|
41
|
+
mode=instructor.Mode(self.instructor_mode),
|
|
38
42
|
)
|
|
39
43
|
|
|
40
44
|
self.model = model
|
|
@@ -42,13 +46,13 @@ class AnthropicAdapter(LLMInterface):
|
|
|
42
46
|
|
|
43
47
|
@retry(
|
|
44
48
|
stop=stop_after_delay(128),
|
|
45
|
-
wait=wait_exponential_jitter(
|
|
49
|
+
wait=wait_exponential_jitter(8, 128),
|
|
46
50
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
47
51
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
48
52
|
reraise=True,
|
|
49
53
|
)
|
|
50
54
|
async def acreate_structured_output(
|
|
51
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
55
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
52
56
|
) -> BaseModel:
|
|
53
57
|
"""
|
|
54
58
|
Generate a response from a user query.
|
|
@@ -66,17 +70,17 @@ class AnthropicAdapter(LLMInterface):
|
|
|
66
70
|
|
|
67
71
|
- BaseModel: An instance of BaseModel containing the structured response.
|
|
68
72
|
"""
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
73
|
+
async with llm_rate_limiter_context_manager():
|
|
74
|
+
return await self.aclient(
|
|
75
|
+
model=self.model,
|
|
76
|
+
max_tokens=4096,
|
|
77
|
+
max_retries=2,
|
|
78
|
+
messages=[
|
|
79
|
+
{
|
|
80
|
+
"role": "user",
|
|
81
|
+
"content": f"""Use the given format to extract information
|
|
82
|
+
from the following input: {text_input}. {system_prompt}""",
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
response_model=response_model,
|
|
86
|
+
)
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/bedrock/adapter.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import litellm
|
|
2
|
+
import instructor
|
|
3
|
+
from typing import Type
|
|
4
|
+
from pydantic import BaseModel
|
|
5
|
+
from litellm.exceptions import ContentPolicyViolationError
|
|
6
|
+
from instructor.exceptions import InstructorRetryException
|
|
7
|
+
|
|
8
|
+
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
9
|
+
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
|
|
10
|
+
LLMInterface,
|
|
11
|
+
)
|
|
12
|
+
from cognee.infrastructure.llm.exceptions import (
|
|
13
|
+
ContentPolicyFilterError,
|
|
14
|
+
MissingSystemPromptPathError,
|
|
15
|
+
)
|
|
16
|
+
from cognee.infrastructure.files.storage.s3_config import get_s3_config
|
|
17
|
+
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.rate_limiter import (
|
|
18
|
+
rate_limit_async,
|
|
19
|
+
rate_limit_sync,
|
|
20
|
+
sleep_and_retry_async,
|
|
21
|
+
sleep_and_retry_sync,
|
|
22
|
+
)
|
|
23
|
+
from cognee.modules.observability.get_observe import get_observe
|
|
24
|
+
|
|
25
|
+
observe = get_observe()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class BedrockAdapter(LLMInterface):
|
|
29
|
+
"""
|
|
30
|
+
Adapter for AWS Bedrock API with support for three authentication methods:
|
|
31
|
+
1. API Key (Bearer Token)
|
|
32
|
+
2. AWS Credentials (access key + secret key)
|
|
33
|
+
3. AWS Profile (boto3 credential chain)
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
name = "Bedrock"
|
|
37
|
+
model: str
|
|
38
|
+
api_key: str
|
|
39
|
+
default_instructor_mode = "json_schema_mode"
|
|
40
|
+
|
|
41
|
+
MAX_RETRIES = 5
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
model: str,
|
|
46
|
+
api_key: str = None,
|
|
47
|
+
max_completion_tokens: int = 16384,
|
|
48
|
+
streaming: bool = False,
|
|
49
|
+
instructor_mode: str = None,
|
|
50
|
+
):
|
|
51
|
+
self.instructor_mode = instructor_mode if instructor_mode else self.default_instructor_mode
|
|
52
|
+
|
|
53
|
+
self.aclient = instructor.from_litellm(
|
|
54
|
+
litellm.acompletion, mode=instructor.Mode(self.instructor_mode)
|
|
55
|
+
)
|
|
56
|
+
self.client = instructor.from_litellm(litellm.completion)
|
|
57
|
+
self.model = model
|
|
58
|
+
self.api_key = api_key
|
|
59
|
+
self.max_completion_tokens = max_completion_tokens
|
|
60
|
+
self.streaming = streaming
|
|
61
|
+
|
|
62
|
+
def _create_bedrock_request(
|
|
63
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
64
|
+
) -> dict:
|
|
65
|
+
"""Create Bedrock request with authentication."""
|
|
66
|
+
|
|
67
|
+
request_params = {
|
|
68
|
+
"model": self.model,
|
|
69
|
+
"custom_llm_provider": "bedrock",
|
|
70
|
+
"drop_params": True,
|
|
71
|
+
"messages": [
|
|
72
|
+
{"role": "user", "content": text_input},
|
|
73
|
+
{"role": "system", "content": system_prompt},
|
|
74
|
+
],
|
|
75
|
+
"response_model": response_model,
|
|
76
|
+
"max_retries": self.MAX_RETRIES,
|
|
77
|
+
"max_completion_tokens": self.max_completion_tokens,
|
|
78
|
+
"stream": self.streaming,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
s3_config = get_s3_config()
|
|
82
|
+
|
|
83
|
+
# Add authentication parameters
|
|
84
|
+
if self.api_key:
|
|
85
|
+
request_params["api_key"] = self.api_key
|
|
86
|
+
elif s3_config.aws_access_key_id and s3_config.aws_secret_access_key:
|
|
87
|
+
request_params["aws_access_key_id"] = s3_config.aws_access_key_id
|
|
88
|
+
request_params["aws_secret_access_key"] = s3_config.aws_secret_access_key
|
|
89
|
+
if s3_config.aws_session_token:
|
|
90
|
+
request_params["aws_session_token"] = s3_config.aws_session_token
|
|
91
|
+
elif s3_config.aws_profile_name:
|
|
92
|
+
request_params["aws_profile_name"] = s3_config.aws_profile_name
|
|
93
|
+
|
|
94
|
+
if s3_config.aws_region:
|
|
95
|
+
request_params["aws_region_name"] = s3_config.aws_region
|
|
96
|
+
|
|
97
|
+
# Add optional parameters
|
|
98
|
+
if s3_config.aws_bedrock_runtime_endpoint:
|
|
99
|
+
request_params["aws_bedrock_runtime_endpoint"] = s3_config.aws_bedrock_runtime_endpoint
|
|
100
|
+
|
|
101
|
+
return request_params
|
|
102
|
+
|
|
103
|
+
@observe(as_type="generation")
|
|
104
|
+
@sleep_and_retry_async()
|
|
105
|
+
@rate_limit_async
|
|
106
|
+
async def acreate_structured_output(
|
|
107
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
108
|
+
) -> BaseModel:
|
|
109
|
+
"""Generate structured output from AWS Bedrock API."""
|
|
110
|
+
|
|
111
|
+
try:
|
|
112
|
+
request_params = self._create_bedrock_request(text_input, system_prompt, response_model)
|
|
113
|
+
return await self.aclient.chat.completions.create(**request_params)
|
|
114
|
+
|
|
115
|
+
except (
|
|
116
|
+
ContentPolicyViolationError,
|
|
117
|
+
InstructorRetryException,
|
|
118
|
+
) as error:
|
|
119
|
+
if (
|
|
120
|
+
isinstance(error, InstructorRetryException)
|
|
121
|
+
and "content management policy" not in str(error).lower()
|
|
122
|
+
):
|
|
123
|
+
raise error
|
|
124
|
+
|
|
125
|
+
raise ContentPolicyFilterError(
|
|
126
|
+
f"The provided input contains content that is not aligned with our content policy: {text_input}"
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
@observe
|
|
130
|
+
@sleep_and_retry_sync()
|
|
131
|
+
@rate_limit_sync
|
|
132
|
+
def create_structured_output(
|
|
133
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
134
|
+
) -> BaseModel:
|
|
135
|
+
"""Generate structured output from AWS Bedrock API (synchronous)."""
|
|
136
|
+
|
|
137
|
+
request_params = self._create_bedrock_request(text_input, system_prompt, response_model)
|
|
138
|
+
return self.client.chat.completions.create(**request_params)
|
|
139
|
+
|
|
140
|
+
def show_prompt(self, text_input: str, system_prompt: str) -> str:
|
|
141
|
+
"""Format and display the prompt for a user query."""
|
|
142
|
+
if not text_input:
|
|
143
|
+
text_input = "No user input provided."
|
|
144
|
+
if not system_prompt:
|
|
145
|
+
raise MissingSystemPromptPathError()
|
|
146
|
+
system_prompt = LLMGateway.read_query_prompt(system_prompt)
|
|
147
|
+
|
|
148
|
+
formatted_prompt = (
|
|
149
|
+
f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
|
|
150
|
+
if system_prompt
|
|
151
|
+
else None
|
|
152
|
+
)
|
|
153
|
+
return formatted_prompt
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py
CHANGED
|
@@ -13,6 +13,7 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
13
13
|
LLMInterface,
|
|
14
14
|
)
|
|
15
15
|
import logging
|
|
16
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
16
17
|
from cognee.shared.logging_utils import get_logger
|
|
17
18
|
from tenacity import (
|
|
18
19
|
retry,
|
|
@@ -41,6 +42,7 @@ class GeminiAdapter(LLMInterface):
|
|
|
41
42
|
name: str
|
|
42
43
|
model: str
|
|
43
44
|
api_key: str
|
|
45
|
+
default_instructor_mode = "json_mode"
|
|
44
46
|
|
|
45
47
|
def __init__(
|
|
46
48
|
self,
|
|
@@ -49,6 +51,7 @@ class GeminiAdapter(LLMInterface):
|
|
|
49
51
|
model: str,
|
|
50
52
|
api_version: str,
|
|
51
53
|
max_completion_tokens: int,
|
|
54
|
+
instructor_mode: str = None,
|
|
52
55
|
fallback_model: str = None,
|
|
53
56
|
fallback_api_key: str = None,
|
|
54
57
|
fallback_endpoint: str = None,
|
|
@@ -63,17 +66,21 @@ class GeminiAdapter(LLMInterface):
|
|
|
63
66
|
self.fallback_api_key = fallback_api_key
|
|
64
67
|
self.fallback_endpoint = fallback_endpoint
|
|
65
68
|
|
|
66
|
-
self.
|
|
69
|
+
self.instructor_mode = instructor_mode if instructor_mode else self.default_instructor_mode
|
|
70
|
+
|
|
71
|
+
self.aclient = instructor.from_litellm(
|
|
72
|
+
litellm.acompletion, mode=instructor.Mode(self.instructor_mode)
|
|
73
|
+
)
|
|
67
74
|
|
|
68
75
|
@retry(
|
|
69
76
|
stop=stop_after_delay(128),
|
|
70
|
-
wait=wait_exponential_jitter(
|
|
77
|
+
wait=wait_exponential_jitter(8, 128),
|
|
71
78
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
72
79
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
73
80
|
reraise=True,
|
|
74
81
|
)
|
|
75
82
|
async def acreate_structured_output(
|
|
76
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
83
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
77
84
|
) -> BaseModel:
|
|
78
85
|
"""
|
|
79
86
|
Generate a response from a user query.
|
|
@@ -99,24 +106,25 @@ class GeminiAdapter(LLMInterface):
|
|
|
99
106
|
"""
|
|
100
107
|
|
|
101
108
|
try:
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
109
|
+
async with llm_rate_limiter_context_manager():
|
|
110
|
+
return await self.aclient.chat.completions.create(
|
|
111
|
+
model=self.model,
|
|
112
|
+
messages=[
|
|
113
|
+
{
|
|
114
|
+
"role": "user",
|
|
115
|
+
"content": f"""{text_input}""",
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"role": "system",
|
|
119
|
+
"content": system_prompt,
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
api_key=self.api_key,
|
|
123
|
+
max_retries=2,
|
|
124
|
+
api_base=self.endpoint,
|
|
125
|
+
api_version=self.api_version,
|
|
126
|
+
response_model=response_model,
|
|
127
|
+
)
|
|
120
128
|
except (
|
|
121
129
|
ContentFilterFinishReasonError,
|
|
122
130
|
ContentPolicyViolationError,
|
|
@@ -134,23 +142,24 @@ class GeminiAdapter(LLMInterface):
|
|
|
134
142
|
)
|
|
135
143
|
|
|
136
144
|
try:
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
145
|
+
async with llm_rate_limiter_context_manager():
|
|
146
|
+
return await self.aclient.chat.completions.create(
|
|
147
|
+
model=self.fallback_model,
|
|
148
|
+
messages=[
|
|
149
|
+
{
|
|
150
|
+
"role": "user",
|
|
151
|
+
"content": f"""{text_input}""",
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
"role": "system",
|
|
155
|
+
"content": system_prompt,
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
max_retries=2,
|
|
159
|
+
api_key=self.fallback_api_key,
|
|
160
|
+
api_base=self.fallback_endpoint,
|
|
161
|
+
response_model=response_model,
|
|
162
|
+
)
|
|
154
163
|
except (
|
|
155
164
|
ContentFilterFinishReasonError,
|
|
156
165
|
ContentPolicyViolationError,
|
|
@@ -13,6 +13,7 @@ from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.ll
|
|
|
13
13
|
LLMInterface,
|
|
14
14
|
)
|
|
15
15
|
import logging
|
|
16
|
+
from cognee.shared.rate_limiting import llm_rate_limiter_context_manager
|
|
16
17
|
from cognee.shared.logging_utils import get_logger
|
|
17
18
|
from tenacity import (
|
|
18
19
|
retry,
|
|
@@ -41,6 +42,7 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
41
42
|
name: str
|
|
42
43
|
model: str
|
|
43
44
|
api_key: str
|
|
45
|
+
default_instructor_mode = "json_mode"
|
|
44
46
|
|
|
45
47
|
def __init__(
|
|
46
48
|
self,
|
|
@@ -49,6 +51,7 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
49
51
|
model: str,
|
|
50
52
|
name: str,
|
|
51
53
|
max_completion_tokens: int,
|
|
54
|
+
instructor_mode: str = None,
|
|
52
55
|
fallback_model: str = None,
|
|
53
56
|
fallback_api_key: str = None,
|
|
54
57
|
fallback_endpoint: str = None,
|
|
@@ -63,17 +66,21 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
63
66
|
self.fallback_api_key = fallback_api_key
|
|
64
67
|
self.fallback_endpoint = fallback_endpoint
|
|
65
68
|
|
|
66
|
-
self.
|
|
69
|
+
self.instructor_mode = instructor_mode if instructor_mode else self.default_instructor_mode
|
|
70
|
+
|
|
71
|
+
self.aclient = instructor.from_litellm(
|
|
72
|
+
litellm.acompletion, mode=instructor.Mode(self.instructor_mode)
|
|
73
|
+
)
|
|
67
74
|
|
|
68
75
|
@retry(
|
|
69
76
|
stop=stop_after_delay(128),
|
|
70
|
-
wait=wait_exponential_jitter(
|
|
77
|
+
wait=wait_exponential_jitter(8, 128),
|
|
71
78
|
retry=retry_if_not_exception_type(litellm.exceptions.NotFoundError),
|
|
72
79
|
before_sleep=before_sleep_log(logger, logging.DEBUG),
|
|
73
80
|
reraise=True,
|
|
74
81
|
)
|
|
75
82
|
async def acreate_structured_output(
|
|
76
|
-
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
83
|
+
self, text_input: str, system_prompt: str, response_model: Type[BaseModel], **kwargs
|
|
77
84
|
) -> BaseModel:
|
|
78
85
|
"""
|
|
79
86
|
Generate a response from a user query.
|
|
@@ -99,23 +106,24 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
99
106
|
"""
|
|
100
107
|
|
|
101
108
|
try:
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
109
|
+
async with llm_rate_limiter_context_manager():
|
|
110
|
+
return await self.aclient.chat.completions.create(
|
|
111
|
+
model=self.model,
|
|
112
|
+
messages=[
|
|
113
|
+
{
|
|
114
|
+
"role": "user",
|
|
115
|
+
"content": f"""{text_input}""",
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"role": "system",
|
|
119
|
+
"content": system_prompt,
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
max_retries=2,
|
|
123
|
+
api_key=self.api_key,
|
|
124
|
+
api_base=self.endpoint,
|
|
125
|
+
response_model=response_model,
|
|
126
|
+
)
|
|
119
127
|
except (
|
|
120
128
|
ContentFilterFinishReasonError,
|
|
121
129
|
ContentPolicyViolationError,
|
|
@@ -133,23 +141,24 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
133
141
|
) from error
|
|
134
142
|
|
|
135
143
|
try:
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
144
|
+
async with llm_rate_limiter_context_manager():
|
|
145
|
+
return await self.aclient.chat.completions.create(
|
|
146
|
+
model=self.fallback_model,
|
|
147
|
+
messages=[
|
|
148
|
+
{
|
|
149
|
+
"role": "user",
|
|
150
|
+
"content": f"""{text_input}""",
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"role": "system",
|
|
154
|
+
"content": system_prompt,
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
max_retries=2,
|
|
158
|
+
api_key=self.fallback_api_key,
|
|
159
|
+
api_base=self.fallback_endpoint,
|
|
160
|
+
response_model=response_model,
|
|
161
|
+
)
|
|
153
162
|
except (
|
|
154
163
|
ContentFilterFinishReasonError,
|
|
155
164
|
ContentPolicyViolationError,
|