cognee 0.3.4.dev4__py3-none-any.whl → 0.3.5__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 +16 -7
- cognee/api/health.py +5 -9
- cognee/api/v1/add/add.py +3 -1
- cognee/api/v1/cognify/cognify.py +44 -7
- cognee/api/v1/permissions/routers/get_permissions_router.py +8 -4
- cognee/api/v1/search/search.py +3 -0
- cognee/api/v1/ui/__init__.py +1 -1
- cognee/api/v1/ui/ui.py +215 -150
- cognee/api/v1/update/__init__.py +1 -0
- cognee/api/v1/update/routers/__init__.py +1 -0
- cognee/api/v1/update/routers/get_update_router.py +90 -0
- cognee/api/v1/update/update.py +100 -0
- cognee/base_config.py +5 -2
- cognee/cli/_cognee.py +28 -10
- cognee/cli/commands/delete_command.py +34 -2
- cognee/eval_framework/corpus_builder/task_getters/get_default_tasks_by_indices.py +2 -2
- cognee/eval_framework/evaluation/direct_llm_eval_adapter.py +3 -2
- cognee/eval_framework/modal_eval_dashboard.py +9 -1
- cognee/infrastructure/databases/graph/config.py +9 -9
- cognee/infrastructure/databases/graph/get_graph_engine.py +4 -21
- cognee/infrastructure/databases/graph/kuzu/adapter.py +60 -9
- cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py +3 -3
- cognee/infrastructure/databases/relational/config.py +4 -4
- cognee/infrastructure/databases/relational/create_relational_engine.py +11 -3
- cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py +7 -3
- cognee/infrastructure/databases/vector/config.py +7 -7
- cognee/infrastructure/databases/vector/create_vector_engine.py +7 -15
- cognee/infrastructure/databases/vector/embeddings/EmbeddingEngine.py +9 -0
- cognee/infrastructure/databases/vector/embeddings/FastembedEmbeddingEngine.py +11 -0
- cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +19 -2
- cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +11 -0
- cognee/infrastructure/databases/vector/embeddings/config.py +8 -0
- cognee/infrastructure/databases/vector/embeddings/get_embedding_engine.py +5 -0
- cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py +11 -10
- cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py +48 -38
- cognee/infrastructure/databases/vector/vector_db_interface.py +8 -4
- cognee/infrastructure/files/storage/S3FileStorage.py +15 -5
- cognee/infrastructure/files/storage/s3_config.py +1 -0
- cognee/infrastructure/files/utils/open_data_file.py +7 -14
- cognee/infrastructure/llm/LLMGateway.py +19 -117
- cognee/infrastructure/llm/config.py +28 -13
- cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/extract_categories.py +2 -1
- cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/extract_event_entities.py +3 -2
- cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/extract_summary.py +3 -2
- cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/knowledge_graph/extract_content_graph.py +2 -1
- cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/knowledge_graph/extract_event_graph.py +3 -2
- cognee/infrastructure/llm/prompts/read_query_prompt.py +3 -2
- cognee/infrastructure/llm/prompts/show_prompt.py +35 -0
- cognee/infrastructure/llm/prompts/test.txt +1 -0
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/__init__.py +2 -2
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/async_client.py +50 -397
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/inlinedbaml.py +2 -3
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/parser.py +8 -88
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/runtime.py +78 -0
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/stream_types.py +2 -99
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/sync_client.py +49 -401
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/type_builder.py +19 -882
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/type_map.py +2 -34
- cognee/infrastructure/llm/structured_output_framework/baml/baml_client/types.py +2 -107
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/acreate_structured_output.baml +26 -0
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/__init__.py +1 -2
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/acreate_structured_output.py +76 -0
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/create_dynamic_baml_type.py +122 -0
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/generators.baml +3 -3
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +0 -32
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +107 -98
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +5 -6
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +5 -6
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/llm_interface.py +0 -26
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +17 -67
- cognee/infrastructure/llm/tokenizer/Gemini/adapter.py +8 -7
- cognee/infrastructure/llm/utils.py +4 -4
- cognee/infrastructure/loaders/LoaderEngine.py +5 -2
- cognee/infrastructure/loaders/external/__init__.py +7 -0
- cognee/infrastructure/loaders/external/advanced_pdf_loader.py +244 -0
- cognee/infrastructure/loaders/supported_loaders.py +7 -0
- cognee/modules/data/methods/create_authorized_dataset.py +9 -0
- cognee/modules/data/methods/get_authorized_dataset.py +1 -1
- cognee/modules/data/methods/get_authorized_dataset_by_name.py +11 -0
- cognee/modules/data/methods/get_deletion_counts.py +92 -0
- cognee/modules/graph/cognee_graph/CogneeGraph.py +1 -1
- cognee/modules/graph/utils/expand_with_nodes_and_edges.py +22 -8
- cognee/modules/graph/utils/retrieve_existing_edges.py +0 -2
- cognee/modules/ingestion/data_types/TextData.py +0 -1
- cognee/modules/observability/get_observe.py +14 -0
- cognee/modules/observability/observers.py +1 -0
- cognee/modules/ontology/base_ontology_resolver.py +42 -0
- cognee/modules/ontology/get_default_ontology_resolver.py +41 -0
- cognee/modules/ontology/matching_strategies.py +53 -0
- cognee/modules/ontology/models.py +20 -0
- cognee/modules/ontology/ontology_config.py +24 -0
- cognee/modules/ontology/ontology_env_config.py +45 -0
- cognee/modules/ontology/rdf_xml/{OntologyResolver.py → RDFLibOntologyResolver.py} +20 -28
- cognee/modules/pipelines/layers/resolve_authorized_user_dataset.py +21 -24
- cognee/modules/pipelines/layers/resolve_authorized_user_datasets.py +3 -3
- cognee/modules/retrieval/code_retriever.py +2 -1
- cognee/modules/retrieval/context_providers/TripletSearchContextProvider.py +1 -4
- cognee/modules/retrieval/graph_completion_cot_retriever.py +6 -5
- cognee/modules/retrieval/graph_completion_retriever.py +0 -3
- cognee/modules/retrieval/insights_retriever.py +1 -1
- cognee/modules/retrieval/jaccard_retrival.py +60 -0
- cognee/modules/retrieval/lexical_retriever.py +123 -0
- cognee/modules/retrieval/natural_language_retriever.py +2 -1
- cognee/modules/retrieval/temporal_retriever.py +3 -2
- cognee/modules/retrieval/utils/brute_force_triplet_search.py +2 -12
- cognee/modules/retrieval/utils/completion.py +4 -7
- cognee/modules/search/methods/get_search_type_tools.py +7 -0
- cognee/modules/search/methods/no_access_control_search.py +1 -1
- cognee/modules/search/methods/search.py +32 -13
- cognee/modules/search/types/SearchType.py +1 -0
- cognee/modules/users/permissions/methods/authorized_give_permission_on_datasets.py +12 -0
- cognee/modules/users/permissions/methods/check_permission_on_dataset.py +11 -0
- cognee/modules/users/permissions/methods/get_all_user_permission_datasets.py +10 -0
- cognee/modules/users/permissions/methods/get_document_ids_for_user.py +10 -0
- cognee/modules/users/permissions/methods/get_principal.py +9 -0
- cognee/modules/users/permissions/methods/get_principal_datasets.py +11 -0
- cognee/modules/users/permissions/methods/get_role.py +10 -0
- cognee/modules/users/permissions/methods/get_specific_user_permission_datasets.py +3 -3
- cognee/modules/users/permissions/methods/get_tenant.py +9 -0
- cognee/modules/users/permissions/methods/give_default_permission_to_role.py +9 -0
- cognee/modules/users/permissions/methods/give_default_permission_to_tenant.py +9 -0
- cognee/modules/users/permissions/methods/give_default_permission_to_user.py +9 -0
- cognee/modules/users/permissions/methods/give_permission_on_dataset.py +10 -0
- cognee/modules/users/roles/methods/add_user_to_role.py +11 -0
- cognee/modules/users/roles/methods/create_role.py +12 -1
- cognee/modules/users/tenants/methods/add_user_to_tenant.py +12 -0
- cognee/modules/users/tenants/methods/create_tenant.py +12 -1
- cognee/modules/visualization/cognee_network_visualization.py +13 -9
- cognee/shared/data_models.py +0 -1
- cognee/shared/utils.py +0 -32
- cognee/tasks/chunk_naive_llm_classifier/chunk_naive_llm_classifier.py +2 -2
- cognee/tasks/codingagents/coding_rule_associations.py +3 -2
- cognee/tasks/entity_completion/entity_extractors/llm_entity_extractor.py +3 -2
- cognee/tasks/graph/cascade_extract/utils/extract_content_nodes_and_relationship_names.py +3 -2
- cognee/tasks/graph/cascade_extract/utils/extract_edge_triplets.py +3 -2
- cognee/tasks/graph/cascade_extract/utils/extract_nodes.py +3 -2
- cognee/tasks/graph/extract_graph_from_code.py +2 -2
- cognee/tasks/graph/extract_graph_from_data.py +55 -12
- cognee/tasks/graph/extract_graph_from_data_v2.py +16 -4
- cognee/tasks/ingestion/migrate_relational_database.py +132 -41
- cognee/tasks/ingestion/resolve_data_directories.py +4 -1
- cognee/tasks/schema/ingest_database_schema.py +134 -0
- cognee/tasks/schema/models.py +40 -0
- cognee/tasks/storage/index_data_points.py +1 -1
- cognee/tasks/storage/index_graph_edges.py +3 -1
- cognee/tasks/summarization/summarize_code.py +2 -2
- cognee/tasks/summarization/summarize_text.py +2 -2
- cognee/tasks/temporal_graph/enrich_events.py +2 -2
- cognee/tasks/temporal_graph/extract_events_and_entities.py +2 -2
- cognee/tests/cli_tests/cli_unit_tests/test_cli_commands.py +13 -4
- cognee/tests/cli_tests/cli_unit_tests/test_cli_edge_cases.py +13 -3
- cognee/tests/test_advanced_pdf_loader.py +141 -0
- cognee/tests/test_chromadb.py +40 -0
- cognee/tests/test_cognee_server_start.py +6 -1
- cognee/tests/test_data/Quantum_computers.txt +9 -0
- cognee/tests/test_lancedb.py +211 -0
- cognee/tests/test_pgvector.py +40 -0
- cognee/tests/test_relational_db_migration.py +76 -0
- cognee/tests/unit/infrastructure/databases/test_index_graph_edges.py +2 -1
- cognee/tests/unit/modules/ontology/test_ontology_adapter.py +330 -13
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_context_extension_test.py +0 -4
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_cot_test.py +0 -4
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_test.py +0 -4
- {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/METADATA +92 -96
- {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/RECORD +173 -159
- distributed/pyproject.toml +0 -1
- cognee/infrastructure/data/utils/extract_keywords.py +0 -48
- cognee/infrastructure/databases/hybrid/falkordb/FalkorDBAdapter.py +0 -1227
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extract_categories.baml +0 -109
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extract_content_graph.baml +0 -343
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/extract_categories.py +0 -0
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/extract_summary.py +0 -89
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/knowledge_graph/__init__.py +0 -0
- cognee/infrastructure/llm/structured_output_framework/baml/baml_src/extraction/knowledge_graph/extract_content_graph.py +0 -44
- cognee/tasks/graph/infer_data_ontology.py +0 -309
- cognee/tests/test_falkordb.py +0 -174
- /cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/__init__.py +0 -0
- /cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/knowledge_graph/__init__.py +0 -0
- /cognee/infrastructure/llm/{structured_output_framework/litellm_instructor/extraction → extraction}/texts.json +0 -0
- {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/WHEEL +0 -0
- {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/entry_points.txt +0 -0
- {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/licenses/LICENSE +0 -0
- {cognee-0.3.4.dev4.dist-info → cognee-0.3.5.dist-info}/licenses/NOTICE.md +0 -0
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py
CHANGED
|
@@ -1,146 +1,155 @@
|
|
|
1
|
+
"""Adapter for Generic API LLM provider API"""
|
|
2
|
+
|
|
1
3
|
import litellm
|
|
4
|
+
import instructor
|
|
5
|
+
from typing import Type
|
|
2
6
|
from pydantic import BaseModel
|
|
3
|
-
from
|
|
4
|
-
from litellm import
|
|
7
|
+
from openai import ContentFilterFinishReasonError
|
|
8
|
+
from litellm.exceptions import ContentPolicyViolationError
|
|
9
|
+
from instructor.core import InstructorRetryException
|
|
5
10
|
|
|
6
|
-
from cognee.
|
|
7
|
-
from cognee.modules.observability.get_observe import get_observe
|
|
8
|
-
from cognee.infrastructure.llm.exceptions import MissingSystemPromptPathError
|
|
11
|
+
from cognee.infrastructure.llm.exceptions import ContentPolicyFilterError
|
|
9
12
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
|
|
10
13
|
LLMInterface,
|
|
11
14
|
)
|
|
12
|
-
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
13
15
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.rate_limiter import (
|
|
14
16
|
rate_limit_async,
|
|
15
17
|
sleep_and_retry_async,
|
|
16
18
|
)
|
|
17
19
|
|
|
18
|
-
logger = get_logger()
|
|
19
|
-
observe = get_observe()
|
|
20
|
-
|
|
21
20
|
|
|
22
21
|
class GeminiAdapter(LLMInterface):
|
|
23
22
|
"""
|
|
24
|
-
|
|
23
|
+
Adapter for Gemini API LLM provider.
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
This class initializes the API adapter with necessary credentials and configurations for
|
|
26
|
+
interacting with the gemini LLM models. It provides methods for creating structured outputs
|
|
27
|
+
based on user input and system prompts.
|
|
28
|
+
|
|
29
|
+
Public methods:
|
|
30
|
+
- acreate_structured_output(text_input: str, system_prompt: str, response_model:
|
|
31
|
+
Type[BaseModel]) -> BaseModel
|
|
29
32
|
"""
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
name: str
|
|
35
|
+
model: str
|
|
36
|
+
api_key: str
|
|
32
37
|
|
|
33
38
|
def __init__(
|
|
34
39
|
self,
|
|
40
|
+
endpoint,
|
|
35
41
|
api_key: str,
|
|
36
42
|
model: str,
|
|
43
|
+
api_version: str,
|
|
37
44
|
max_completion_tokens: int,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
)
|
|
42
|
-
self.api_key = api_key
|
|
45
|
+
fallback_model: str = None,
|
|
46
|
+
fallback_api_key: str = None,
|
|
47
|
+
fallback_endpoint: str = None,
|
|
48
|
+
):
|
|
43
49
|
self.model = model
|
|
50
|
+
self.api_key = api_key
|
|
44
51
|
self.endpoint = endpoint
|
|
45
52
|
self.api_version = api_version
|
|
46
|
-
self.streaming = streaming
|
|
47
53
|
self.max_completion_tokens = max_completion_tokens
|
|
48
54
|
|
|
49
|
-
|
|
55
|
+
self.fallback_model = fallback_model
|
|
56
|
+
self.fallback_api_key = fallback_api_key
|
|
57
|
+
self.fallback_endpoint = fallback_endpoint
|
|
58
|
+
|
|
59
|
+
self.aclient = instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON)
|
|
60
|
+
|
|
50
61
|
@sleep_and_retry_async()
|
|
51
62
|
@rate_limit_async
|
|
52
63
|
async def acreate_structured_output(
|
|
53
64
|
self, text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
54
65
|
) -> BaseModel:
|
|
55
66
|
"""
|
|
56
|
-
Generate
|
|
57
|
-
system prompt.
|
|
67
|
+
Generate a response from a user query.
|
|
58
68
|
|
|
59
|
-
This method
|
|
60
|
-
|
|
69
|
+
This asynchronous method sends a user query and a system prompt to a language model and
|
|
70
|
+
retrieves the generated response. It handles API communication and retries up to a
|
|
71
|
+
specified limit in case of request failures.
|
|
61
72
|
|
|
62
73
|
Parameters:
|
|
63
74
|
-----------
|
|
64
75
|
|
|
65
|
-
- text_input (str): The
|
|
66
|
-
- system_prompt (str):
|
|
67
|
-
|
|
68
|
-
- response_model (Type[BaseModel]): A model
|
|
69
|
-
the response.
|
|
76
|
+
- text_input (str): The input text from the user to generate a response for.
|
|
77
|
+
- system_prompt (str): A prompt that provides context or instructions for the
|
|
78
|
+
response generation.
|
|
79
|
+
- response_model (Type[BaseModel]): A Pydantic model that defines the structure of
|
|
80
|
+
the expected response.
|
|
70
81
|
|
|
71
82
|
Returns:
|
|
72
83
|
--------
|
|
73
84
|
|
|
74
|
-
- BaseModel:
|
|
75
|
-
model.
|
|
85
|
+
- BaseModel: An instance of the specified response model containing the structured
|
|
86
|
+
output from the language model.
|
|
76
87
|
"""
|
|
77
|
-
try:
|
|
78
|
-
if response_model is str:
|
|
79
|
-
response_schema = {"type": "string"}
|
|
80
|
-
else:
|
|
81
|
-
response_schema = response_model
|
|
82
88
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
89
|
+
try:
|
|
90
|
+
return await self.aclient.chat.completions.create(
|
|
91
|
+
model=self.model,
|
|
92
|
+
messages=[
|
|
93
|
+
{
|
|
94
|
+
"role": "user",
|
|
95
|
+
"content": f"""{text_input}""",
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"role": "system",
|
|
99
|
+
"content": system_prompt,
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
api_key=self.api_key,
|
|
103
|
+
max_retries=5,
|
|
104
|
+
api_base=self.endpoint,
|
|
105
|
+
api_version=self.api_version,
|
|
106
|
+
response_model=response_model,
|
|
107
|
+
)
|
|
108
|
+
except (
|
|
109
|
+
ContentFilterFinishReasonError,
|
|
110
|
+
ContentPolicyViolationError,
|
|
111
|
+
InstructorRetryException,
|
|
112
|
+
) as error:
|
|
113
|
+
if (
|
|
114
|
+
isinstance(error, InstructorRetryException)
|
|
115
|
+
and "content management policy" not in str(error).lower()
|
|
116
|
+
):
|
|
117
|
+
raise error
|
|
118
|
+
|
|
119
|
+
if not (self.fallback_model and self.fallback_api_key and self.fallback_endpoint):
|
|
120
|
+
raise ContentPolicyFilterError(
|
|
121
|
+
f"The provided input contains content that is not aligned with our content policy: {text_input}"
|
|
122
|
+
)
|
|
87
123
|
|
|
88
124
|
try:
|
|
89
|
-
|
|
90
|
-
model=
|
|
91
|
-
messages=
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
125
|
+
return await self.aclient.chat.completions.create(
|
|
126
|
+
model=self.fallback_model,
|
|
127
|
+
messages=[
|
|
128
|
+
{
|
|
129
|
+
"role": "user",
|
|
130
|
+
"content": f"""{text_input}""",
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"role": "system",
|
|
134
|
+
"content": system_prompt,
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
max_retries=5,
|
|
138
|
+
api_key=self.fallback_api_key,
|
|
139
|
+
api_base=self.fallback_endpoint,
|
|
140
|
+
response_model=response_model,
|
|
98
141
|
)
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
logger.error(f"Schema validation failed: {str(e)}")
|
|
114
|
-
logger.debug(f"Raw response: {e.raw_response}")
|
|
115
|
-
raise ValueError(f"Response failed schema validation: {str(e)}")
|
|
116
|
-
|
|
117
|
-
def show_prompt(self, text_input: str, system_prompt: str) -> str:
|
|
118
|
-
"""
|
|
119
|
-
Format and display the prompt for a user query.
|
|
120
|
-
|
|
121
|
-
Raises an MissingQueryParameterError if no system prompt is provided.
|
|
122
|
-
|
|
123
|
-
Parameters:
|
|
124
|
-
-----------
|
|
125
|
-
|
|
126
|
-
- text_input (str): The user input text to display along with the system prompt.
|
|
127
|
-
- system_prompt (str): The path or content of the system prompt to be read and
|
|
128
|
-
displayed.
|
|
129
|
-
|
|
130
|
-
Returns:
|
|
131
|
-
--------
|
|
132
|
-
|
|
133
|
-
- str: Returns a formatted string containing the system prompt and user input.
|
|
134
|
-
"""
|
|
135
|
-
if not text_input:
|
|
136
|
-
text_input = "No user input provided."
|
|
137
|
-
if not system_prompt:
|
|
138
|
-
raise MissingSystemPromptPathError()
|
|
139
|
-
system_prompt = LLMGateway.read_query_prompt(system_prompt)
|
|
140
|
-
|
|
141
|
-
formatted_prompt = (
|
|
142
|
-
f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
|
|
143
|
-
if system_prompt
|
|
144
|
-
else None
|
|
145
|
-
)
|
|
146
|
-
return formatted_prompt
|
|
142
|
+
except (
|
|
143
|
+
ContentFilterFinishReasonError,
|
|
144
|
+
ContentPolicyViolationError,
|
|
145
|
+
InstructorRetryException,
|
|
146
|
+
) as error:
|
|
147
|
+
if (
|
|
148
|
+
isinstance(error, InstructorRetryException)
|
|
149
|
+
and "content management policy" not in str(error).lower()
|
|
150
|
+
):
|
|
151
|
+
raise error
|
|
152
|
+
else:
|
|
153
|
+
raise ContentPolicyFilterError(
|
|
154
|
+
f"The provided input contains content that is not aligned with our content policy: {text_input}"
|
|
155
|
+
)
|
|
@@ -6,7 +6,7 @@ from typing import Type
|
|
|
6
6
|
from pydantic import BaseModel
|
|
7
7
|
from openai import ContentFilterFinishReasonError
|
|
8
8
|
from litellm.exceptions import ContentPolicyViolationError
|
|
9
|
-
from instructor.
|
|
9
|
+
from instructor.core import InstructorRetryException
|
|
10
10
|
|
|
11
11
|
from cognee.infrastructure.llm.exceptions import ContentPolicyFilterError
|
|
12
12
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
|
|
@@ -56,9 +56,7 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
56
56
|
self.fallback_api_key = fallback_api_key
|
|
57
57
|
self.fallback_endpoint = fallback_endpoint
|
|
58
58
|
|
|
59
|
-
self.aclient = instructor.from_litellm(
|
|
60
|
-
litellm.acompletion, mode=instructor.Mode.JSON, api_key=api_key
|
|
61
|
-
)
|
|
59
|
+
self.aclient = instructor.from_litellm(litellm.acompletion, mode=instructor.Mode.JSON)
|
|
62
60
|
|
|
63
61
|
@sleep_and_retry_async()
|
|
64
62
|
@rate_limit_async
|
|
@@ -102,6 +100,7 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
102
100
|
},
|
|
103
101
|
],
|
|
104
102
|
max_retries=5,
|
|
103
|
+
api_key=self.api_key,
|
|
105
104
|
api_base=self.endpoint,
|
|
106
105
|
response_model=response_model,
|
|
107
106
|
)
|
|
@@ -119,7 +118,7 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
119
118
|
if not (self.fallback_model and self.fallback_api_key and self.fallback_endpoint):
|
|
120
119
|
raise ContentPolicyFilterError(
|
|
121
120
|
f"The provided input contains content that is not aligned with our content policy: {text_input}"
|
|
122
|
-
)
|
|
121
|
+
) from error
|
|
123
122
|
|
|
124
123
|
try:
|
|
125
124
|
return await self.aclient.chat.completions.create(
|
|
@@ -152,4 +151,4 @@ class GenericAPIAdapter(LLMInterface):
|
|
|
152
151
|
else:
|
|
153
152
|
raise ContentPolicyFilterError(
|
|
154
153
|
f"The provided input contains content that is not aligned with our content policy: {text_input}"
|
|
155
|
-
)
|
|
154
|
+
) from error
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py
CHANGED
|
@@ -32,7 +32,7 @@ class LLMProvider(Enum):
|
|
|
32
32
|
GEMINI = "gemini"
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
def get_llm_client():
|
|
35
|
+
def get_llm_client(raise_api_key_error: bool = True):
|
|
36
36
|
"""
|
|
37
37
|
Get the LLM client based on the configuration using Enums.
|
|
38
38
|
|
|
@@ -65,7 +65,7 @@ def get_llm_client():
|
|
|
65
65
|
)
|
|
66
66
|
|
|
67
67
|
if provider == LLMProvider.OPENAI:
|
|
68
|
-
if llm_config.llm_api_key is None:
|
|
68
|
+
if llm_config.llm_api_key is None and raise_api_key_error:
|
|
69
69
|
raise LLMAPIKeyNotSetError()
|
|
70
70
|
|
|
71
71
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.openai.adapter import (
|
|
@@ -86,7 +86,7 @@ def get_llm_client():
|
|
|
86
86
|
)
|
|
87
87
|
|
|
88
88
|
elif provider == LLMProvider.OLLAMA:
|
|
89
|
-
if llm_config.llm_api_key is None:
|
|
89
|
+
if llm_config.llm_api_key is None and raise_api_key_error:
|
|
90
90
|
raise LLMAPIKeyNotSetError()
|
|
91
91
|
|
|
92
92
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.generic_llm_api.adapter import (
|
|
@@ -111,7 +111,7 @@ def get_llm_client():
|
|
|
111
111
|
)
|
|
112
112
|
|
|
113
113
|
elif provider == LLMProvider.CUSTOM:
|
|
114
|
-
if llm_config.llm_api_key is None:
|
|
114
|
+
if llm_config.llm_api_key is None and raise_api_key_error:
|
|
115
115
|
raise LLMAPIKeyNotSetError()
|
|
116
116
|
|
|
117
117
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.generic_llm_api.adapter import (
|
|
@@ -130,7 +130,7 @@ def get_llm_client():
|
|
|
130
130
|
)
|
|
131
131
|
|
|
132
132
|
elif provider == LLMProvider.GEMINI:
|
|
133
|
-
if llm_config.llm_api_key is None:
|
|
133
|
+
if llm_config.llm_api_key is None and raise_api_key_error:
|
|
134
134
|
raise LLMAPIKeyNotSetError()
|
|
135
135
|
|
|
136
136
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.gemini.adapter import (
|
|
@@ -143,7 +143,6 @@ def get_llm_client():
|
|
|
143
143
|
max_completion_tokens=max_completion_tokens,
|
|
144
144
|
endpoint=llm_config.llm_endpoint,
|
|
145
145
|
api_version=llm_config.llm_api_version,
|
|
146
|
-
streaming=llm_config.llm_streaming,
|
|
147
146
|
)
|
|
148
147
|
|
|
149
148
|
else:
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/llm_interface.py
CHANGED
|
@@ -36,29 +36,3 @@ class LLMInterface(Protocol):
|
|
|
36
36
|
output.
|
|
37
37
|
"""
|
|
38
38
|
raise NotImplementedError
|
|
39
|
-
|
|
40
|
-
def show_prompt(self, text_input: str, system_prompt: str) -> str:
|
|
41
|
-
"""
|
|
42
|
-
Format and display the prompt for a user query.
|
|
43
|
-
|
|
44
|
-
Parameters:
|
|
45
|
-
-----------
|
|
46
|
-
|
|
47
|
-
- text_input (str): Input text from the user to be included in the prompt.
|
|
48
|
-
- system_prompt (str): The system prompt that will be shown alongside the user
|
|
49
|
-
input.
|
|
50
|
-
|
|
51
|
-
Returns:
|
|
52
|
-
--------
|
|
53
|
-
|
|
54
|
-
- str: The formatted prompt string combining system prompt and user input.
|
|
55
|
-
"""
|
|
56
|
-
if not text_input:
|
|
57
|
-
text_input = "No user input provided."
|
|
58
|
-
if not system_prompt:
|
|
59
|
-
raise ValueError("No system prompt path provided.")
|
|
60
|
-
system_prompt = LLMGateway.read_query_prompt(system_prompt)
|
|
61
|
-
|
|
62
|
-
formatted_prompt = f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
|
|
63
|
-
|
|
64
|
-
return formatted_prompt
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py
CHANGED
|
@@ -5,15 +5,13 @@ from typing import Type
|
|
|
5
5
|
from pydantic import BaseModel
|
|
6
6
|
from openai import ContentFilterFinishReasonError
|
|
7
7
|
from litellm.exceptions import ContentPolicyViolationError
|
|
8
|
-
from instructor.
|
|
8
|
+
from instructor.core import InstructorRetryException
|
|
9
9
|
|
|
10
|
-
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
11
10
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.llm_interface import (
|
|
12
11
|
LLMInterface,
|
|
13
12
|
)
|
|
14
13
|
from cognee.infrastructure.llm.exceptions import (
|
|
15
14
|
ContentPolicyFilterError,
|
|
16
|
-
MissingSystemPromptPathError,
|
|
17
15
|
)
|
|
18
16
|
from cognee.infrastructure.files.utils.open_data_file import open_data_file
|
|
19
17
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.rate_limiter import (
|
|
@@ -73,8 +71,19 @@ class OpenAIAdapter(LLMInterface):
|
|
|
73
71
|
fallback_api_key: str = None,
|
|
74
72
|
fallback_endpoint: str = None,
|
|
75
73
|
):
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
# TODO: With gpt5 series models OpenAI expects JSON_SCHEMA as a mode for structured outputs.
|
|
75
|
+
# Make sure all new gpt models will work with this mode as well.
|
|
76
|
+
if "gpt-5" in model:
|
|
77
|
+
self.aclient = instructor.from_litellm(
|
|
78
|
+
litellm.acompletion, mode=instructor.Mode.JSON_SCHEMA
|
|
79
|
+
)
|
|
80
|
+
self.client = instructor.from_litellm(
|
|
81
|
+
litellm.completion, mode=instructor.Mode.JSON_SCHEMA
|
|
82
|
+
)
|
|
83
|
+
else:
|
|
84
|
+
self.aclient = instructor.from_litellm(litellm.acompletion)
|
|
85
|
+
self.client = instructor.from_litellm(litellm.completion)
|
|
86
|
+
|
|
78
87
|
self.transcription_model = transcription_model
|
|
79
88
|
self.model = model
|
|
80
89
|
self.api_key = api_key
|
|
@@ -132,43 +141,16 @@ class OpenAIAdapter(LLMInterface):
|
|
|
132
141
|
api_version=self.api_version,
|
|
133
142
|
response_model=response_model,
|
|
134
143
|
max_retries=self.MAX_RETRIES,
|
|
135
|
-
extra_body={"reasoning_effort": "minimal"},
|
|
136
144
|
)
|
|
137
145
|
except (
|
|
138
146
|
ContentFilterFinishReasonError,
|
|
139
147
|
ContentPolicyViolationError,
|
|
140
148
|
InstructorRetryException,
|
|
141
|
-
) as
|
|
142
|
-
if (
|
|
143
|
-
isinstance(error, InstructorRetryException)
|
|
144
|
-
and "content management policy" not in str(error).lower()
|
|
145
|
-
):
|
|
146
|
-
logger.debug(
|
|
147
|
-
"LLM Model does not support reasoning_effort parameter, trying call without the parameter."
|
|
148
|
-
)
|
|
149
|
-
return await self.aclient.chat.completions.create(
|
|
150
|
-
model=self.model,
|
|
151
|
-
messages=[
|
|
152
|
-
{
|
|
153
|
-
"role": "user",
|
|
154
|
-
"content": f"""{text_input}""",
|
|
155
|
-
},
|
|
156
|
-
{
|
|
157
|
-
"role": "system",
|
|
158
|
-
"content": system_prompt,
|
|
159
|
-
},
|
|
160
|
-
],
|
|
161
|
-
api_key=self.api_key,
|
|
162
|
-
api_base=self.endpoint,
|
|
163
|
-
api_version=self.api_version,
|
|
164
|
-
response_model=response_model,
|
|
165
|
-
max_retries=self.MAX_RETRIES,
|
|
166
|
-
)
|
|
167
|
-
|
|
149
|
+
) as e:
|
|
168
150
|
if not (self.fallback_model and self.fallback_api_key):
|
|
169
151
|
raise ContentPolicyFilterError(
|
|
170
152
|
f"The provided input contains content that is not aligned with our content policy: {text_input}"
|
|
171
|
-
)
|
|
153
|
+
) from e
|
|
172
154
|
|
|
173
155
|
try:
|
|
174
156
|
return await self.aclient.chat.completions.create(
|
|
@@ -201,7 +183,7 @@ class OpenAIAdapter(LLMInterface):
|
|
|
201
183
|
else:
|
|
202
184
|
raise ContentPolicyFilterError(
|
|
203
185
|
f"The provided input contains content that is not aligned with our content policy: {text_input}"
|
|
204
|
-
)
|
|
186
|
+
) from error
|
|
205
187
|
|
|
206
188
|
@observe
|
|
207
189
|
@sleep_and_retry_sync()
|
|
@@ -328,35 +310,3 @@ class OpenAIAdapter(LLMInterface):
|
|
|
328
310
|
max_completion_tokens=300,
|
|
329
311
|
max_retries=self.MAX_RETRIES,
|
|
330
312
|
)
|
|
331
|
-
|
|
332
|
-
def show_prompt(self, text_input: str, system_prompt: str) -> str:
|
|
333
|
-
"""
|
|
334
|
-
Format and display the prompt for a user query.
|
|
335
|
-
|
|
336
|
-
This method formats the prompt using the provided user input and system prompt,
|
|
337
|
-
returning a string representation. Raises MissingSystemPromptPathError if the system prompt is not
|
|
338
|
-
provided.
|
|
339
|
-
|
|
340
|
-
Parameters:
|
|
341
|
-
-----------
|
|
342
|
-
|
|
343
|
-
- text_input (str): The input text provided by the user.
|
|
344
|
-
- system_prompt (str): The system's prompt to guide the model's response.
|
|
345
|
-
|
|
346
|
-
Returns:
|
|
347
|
-
--------
|
|
348
|
-
|
|
349
|
-
- str: A formatted string representing the user input and system prompt.
|
|
350
|
-
"""
|
|
351
|
-
if not text_input:
|
|
352
|
-
text_input = "No user input provided."
|
|
353
|
-
if not system_prompt:
|
|
354
|
-
raise MissingSystemPromptPathError()
|
|
355
|
-
system_prompt = LLMGateway.read_query_prompt(system_prompt)
|
|
356
|
-
|
|
357
|
-
formatted_prompt = (
|
|
358
|
-
f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
|
|
359
|
-
if system_prompt
|
|
360
|
-
else None
|
|
361
|
-
)
|
|
362
|
-
return formatted_prompt
|
|
@@ -3,6 +3,7 @@ from typing import List, Any
|
|
|
3
3
|
from ..tokenizer_interface import TokenizerInterface
|
|
4
4
|
|
|
5
5
|
|
|
6
|
+
# NOTE: DEPRECATED as to count tokens you need to send an API request to Google it is too slow to use with Cognee
|
|
6
7
|
class GeminiTokenizer(TokenizerInterface):
|
|
7
8
|
"""
|
|
8
9
|
Implements a tokenizer interface for the Gemini model, managing token extraction and
|
|
@@ -16,10 +17,10 @@ class GeminiTokenizer(TokenizerInterface):
|
|
|
16
17
|
|
|
17
18
|
def __init__(
|
|
18
19
|
self,
|
|
19
|
-
|
|
20
|
+
llm_model: str,
|
|
20
21
|
max_completion_tokens: int = 3072,
|
|
21
22
|
):
|
|
22
|
-
self.
|
|
23
|
+
self.llm_model = llm_model
|
|
23
24
|
self.max_completion_tokens = max_completion_tokens
|
|
24
25
|
|
|
25
26
|
# Get LLM API key from config
|
|
@@ -28,12 +29,11 @@ class GeminiTokenizer(TokenizerInterface):
|
|
|
28
29
|
get_llm_config,
|
|
29
30
|
)
|
|
30
31
|
|
|
31
|
-
config = get_embedding_config()
|
|
32
32
|
llm_config = get_llm_config()
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
from google import genai
|
|
35
35
|
|
|
36
|
-
genai.
|
|
36
|
+
self.client = genai.Client(api_key=llm_config.llm_api_key)
|
|
37
37
|
|
|
38
38
|
def extract_tokens(self, text: str) -> List[Any]:
|
|
39
39
|
"""
|
|
@@ -77,6 +77,7 @@ class GeminiTokenizer(TokenizerInterface):
|
|
|
77
77
|
|
|
78
78
|
- int: The number of tokens in the given text.
|
|
79
79
|
"""
|
|
80
|
-
import google.generativeai as genai
|
|
81
80
|
|
|
82
|
-
|
|
81
|
+
tokens_response = self.client.models.count_tokens(model=self.llm_model, contents=text)
|
|
82
|
+
|
|
83
|
+
return tokens_response.total_tokens
|
|
@@ -3,6 +3,7 @@ import litellm
|
|
|
3
3
|
from cognee.infrastructure.llm.structured_output_framework.litellm_instructor.llm.get_llm_client import (
|
|
4
4
|
get_llm_client,
|
|
5
5
|
)
|
|
6
|
+
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
6
7
|
from cognee.shared.logging_utils import get_logger
|
|
7
8
|
|
|
8
9
|
logger = get_logger()
|
|
@@ -28,7 +29,7 @@ def get_max_chunk_tokens():
|
|
|
28
29
|
|
|
29
30
|
# Calculate max chunk size based on the following formula
|
|
30
31
|
embedding_engine = get_vector_engine().embedding_engine
|
|
31
|
-
llm_client = get_llm_client()
|
|
32
|
+
llm_client = get_llm_client(raise_api_key_error=False)
|
|
32
33
|
|
|
33
34
|
# We need to make sure chunk size won't take more than half of LLM max context token size
|
|
34
35
|
# but it also can't be bigger than the embedding engine max token size
|
|
@@ -62,7 +63,7 @@ def get_model_max_completion_tokens(model_name: str):
|
|
|
62
63
|
max_completion_tokens = litellm.model_cost[model_name]["max_tokens"]
|
|
63
64
|
logger.debug(f"Max input tokens for {model_name}: {max_completion_tokens}")
|
|
64
65
|
else:
|
|
65
|
-
logger.
|
|
66
|
+
logger.debug("Model not found in LiteLLM's model_cost.")
|
|
66
67
|
|
|
67
68
|
return max_completion_tokens
|
|
68
69
|
|
|
@@ -76,8 +77,7 @@ async def test_llm_connection():
|
|
|
76
77
|
the connection attempt and re-raise the exception for further handling.
|
|
77
78
|
"""
|
|
78
79
|
try:
|
|
79
|
-
|
|
80
|
-
await llm_adapter.acreate_structured_output(
|
|
80
|
+
await LLMGateway.acreate_structured_output(
|
|
81
81
|
text_input="test",
|
|
82
82
|
system_prompt='Respond to me with the following string: "test"',
|
|
83
83
|
response_model=str,
|
|
@@ -27,6 +27,7 @@ class LoaderEngine:
|
|
|
27
27
|
|
|
28
28
|
self.default_loader_priority = [
|
|
29
29
|
"text_loader",
|
|
30
|
+
"advanced_pdf_loader",
|
|
30
31
|
"pypdf_loader",
|
|
31
32
|
"image_loader",
|
|
32
33
|
"audio_loader",
|
|
@@ -86,7 +87,7 @@ class LoaderEngine:
|
|
|
86
87
|
if loader.can_handle(extension=file_info.extension, mime_type=file_info.mime):
|
|
87
88
|
return loader
|
|
88
89
|
else:
|
|
89
|
-
|
|
90
|
+
logger.info(f"Skipping {loader_name}: Preferred Loader not registered")
|
|
90
91
|
|
|
91
92
|
# Try default priority order
|
|
92
93
|
for loader_name in self.default_loader_priority:
|
|
@@ -95,7 +96,9 @@ class LoaderEngine:
|
|
|
95
96
|
if loader.can_handle(extension=file_info.extension, mime_type=file_info.mime):
|
|
96
97
|
return loader
|
|
97
98
|
else:
|
|
98
|
-
|
|
99
|
+
logger.info(
|
|
100
|
+
f"Skipping {loader_name}: Loader not registered (in default priority list)."
|
|
101
|
+
)
|
|
99
102
|
|
|
100
103
|
return None
|
|
101
104
|
|