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
|
@@ -15,38 +15,6 @@ from . import stream_types
|
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
type_map = {
|
|
18
|
-
"types.
|
|
19
|
-
"stream_types.
|
|
20
|
-
"types.ContentLabel": types.ContentLabel,
|
|
21
|
-
"stream_types.ContentLabel": stream_types.ContentLabel,
|
|
22
|
-
"types.DefaultContentPrediction": types.DefaultContentPrediction,
|
|
23
|
-
"stream_types.DefaultContentPrediction": stream_types.DefaultContentPrediction,
|
|
24
|
-
"types.DynamicKnowledgeGraph": types.DynamicKnowledgeGraph,
|
|
25
|
-
"stream_types.DynamicKnowledgeGraph": stream_types.DynamicKnowledgeGraph,
|
|
26
|
-
"types.Edge": types.Edge,
|
|
27
|
-
"stream_types.Edge": stream_types.Edge,
|
|
28
|
-
"types.ImageContent": types.ImageContent,
|
|
29
|
-
"stream_types.ImageContent": stream_types.ImageContent,
|
|
30
|
-
"types.KnowledgeGraph": types.KnowledgeGraph,
|
|
31
|
-
"stream_types.KnowledgeGraph": stream_types.KnowledgeGraph,
|
|
32
|
-
"types.Model3DContent": types.Model3DContent,
|
|
33
|
-
"stream_types.Model3DContent": stream_types.Model3DContent,
|
|
34
|
-
"types.MultimediaContent": types.MultimediaContent,
|
|
35
|
-
"stream_types.MultimediaContent": stream_types.MultimediaContent,
|
|
36
|
-
"types.Node": types.Node,
|
|
37
|
-
"stream_types.Node": stream_types.Node,
|
|
38
|
-
"types.ProceduralContent": types.ProceduralContent,
|
|
39
|
-
"stream_types.ProceduralContent": stream_types.ProceduralContent,
|
|
40
|
-
"types.SummarizedClass": types.SummarizedClass,
|
|
41
|
-
"stream_types.SummarizedClass": stream_types.SummarizedClass,
|
|
42
|
-
"types.SummarizedCode": types.SummarizedCode,
|
|
43
|
-
"stream_types.SummarizedCode": stream_types.SummarizedCode,
|
|
44
|
-
"types.SummarizedContent": types.SummarizedContent,
|
|
45
|
-
"stream_types.SummarizedContent": stream_types.SummarizedContent,
|
|
46
|
-
"types.SummarizedFunction": types.SummarizedFunction,
|
|
47
|
-
"stream_types.SummarizedFunction": stream_types.SummarizedFunction,
|
|
48
|
-
"types.TextContent": types.TextContent,
|
|
49
|
-
"stream_types.TextContent": stream_types.TextContent,
|
|
50
|
-
"types.VideoContent": types.VideoContent,
|
|
51
|
-
"stream_types.VideoContent": stream_types.VideoContent,
|
|
18
|
+
"types.ResponseModel": types.ResponseModel,
|
|
19
|
+
"stream_types.ResponseModel": stream_types.ResponseModel,
|
|
52
20
|
}
|
|
@@ -48,119 +48,14 @@ def all_succeeded(checks: typing.Dict[CheckName, Check]) -> bool:
|
|
|
48
48
|
# #########################################################################
|
|
49
49
|
|
|
50
50
|
# #########################################################################
|
|
51
|
-
# Generated classes (
|
|
51
|
+
# Generated classes (1)
|
|
52
52
|
# #########################################################################
|
|
53
53
|
|
|
54
54
|
|
|
55
|
-
class
|
|
56
|
-
type: str
|
|
57
|
-
subclass: typing.List[str]
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
class ContentLabel(BaseModel):
|
|
61
|
-
content_type: typing.Union[
|
|
62
|
-
typing_extensions.Literal["text"],
|
|
63
|
-
typing_extensions.Literal["audio"],
|
|
64
|
-
typing_extensions.Literal["image"],
|
|
65
|
-
typing_extensions.Literal["video"],
|
|
66
|
-
typing_extensions.Literal["multimedia"],
|
|
67
|
-
typing_extensions.Literal["3d_model"],
|
|
68
|
-
typing_extensions.Literal["procedural"],
|
|
69
|
-
]
|
|
70
|
-
type: str
|
|
71
|
-
subclass: typing.List[str]
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
class DefaultContentPrediction(BaseModel):
|
|
75
|
-
label: "ContentLabel"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
class DynamicKnowledgeGraph(BaseModel):
|
|
55
|
+
class ResponseModel(BaseModel):
|
|
79
56
|
model_config = ConfigDict(extra="allow")
|
|
80
57
|
|
|
81
58
|
|
|
82
|
-
class Edge(BaseModel):
|
|
83
|
-
# doc string for edge
|
|
84
|
-
# doc string for source_node_id
|
|
85
|
-
|
|
86
|
-
source_node_id: str
|
|
87
|
-
target_node_id: str
|
|
88
|
-
relationship_name: str
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
class ImageContent(BaseModel):
|
|
92
|
-
type: str
|
|
93
|
-
subclass: typing.List[str]
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
class KnowledgeGraph(BaseModel):
|
|
97
|
-
nodes: typing.List["Node"]
|
|
98
|
-
edges: typing.List["Edge"]
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
class Model3DContent(BaseModel):
|
|
102
|
-
type: str
|
|
103
|
-
subclass: typing.List[str]
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
class MultimediaContent(BaseModel):
|
|
107
|
-
type: str
|
|
108
|
-
subclass: typing.List[str]
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
class Node(BaseModel):
|
|
112
|
-
model_config = ConfigDict(extra="allow")
|
|
113
|
-
id: str
|
|
114
|
-
name: str
|
|
115
|
-
type: str
|
|
116
|
-
description: str
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
class ProceduralContent(BaseModel):
|
|
120
|
-
type: str
|
|
121
|
-
subclass: typing.List[str]
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
class SummarizedClass(BaseModel):
|
|
125
|
-
name: str
|
|
126
|
-
description: str
|
|
127
|
-
methods: typing.Optional[typing.List["SummarizedFunction"]] = None
|
|
128
|
-
decorators: typing.Optional[typing.List[str]] = None
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
class SummarizedCode(BaseModel):
|
|
132
|
-
high_level_summary: str
|
|
133
|
-
key_features: typing.List[str]
|
|
134
|
-
imports: typing.List[str]
|
|
135
|
-
constants: typing.List[str]
|
|
136
|
-
classes: typing.List["SummarizedClass"]
|
|
137
|
-
functions: typing.List["SummarizedFunction"]
|
|
138
|
-
workflow_description: typing.Optional[str] = None
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
class SummarizedContent(BaseModel):
|
|
142
|
-
summary: str
|
|
143
|
-
description: str
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
class SummarizedFunction(BaseModel):
|
|
147
|
-
name: str
|
|
148
|
-
description: str
|
|
149
|
-
inputs: typing.Optional[typing.List[str]] = None
|
|
150
|
-
outputs: typing.Optional[typing.List[str]] = None
|
|
151
|
-
decorators: typing.Optional[typing.List[str]] = None
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
class TextContent(BaseModel):
|
|
155
|
-
type: str
|
|
156
|
-
subclass: typing.List[str]
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
class VideoContent(BaseModel):
|
|
160
|
-
type: str
|
|
161
|
-
subclass: typing.List[str]
|
|
162
|
-
|
|
163
|
-
|
|
164
59
|
# #########################################################################
|
|
165
60
|
# Generated type aliases (0)
|
|
166
61
|
# #########################################################################
|
cognee/infrastructure/llm/structured_output_framework/baml/baml_src/acreate_structured_output.baml
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class ResponseModel {
|
|
2
|
+
@@dynamic
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
// OpenAI client with environment model selection
|
|
6
|
+
client<llm> OpenAI {
|
|
7
|
+
provider openai
|
|
8
|
+
options {
|
|
9
|
+
model client_registry.model
|
|
10
|
+
api_key client_registry.api_key
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function AcreateStructuredOutput(
|
|
15
|
+
text_input: string,
|
|
16
|
+
system_prompt: string,
|
|
17
|
+
) -> ResponseModel {
|
|
18
|
+
client OpenAI
|
|
19
|
+
|
|
20
|
+
prompt #"
|
|
21
|
+
{{ system_prompt }}
|
|
22
|
+
{{ ctx.output_format }}
|
|
23
|
+
{{ _.role('user') }}
|
|
24
|
+
{{ text_input }}
|
|
25
|
+
"#
|
|
26
|
+
}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
from .
|
|
2
|
-
from .extract_summary import extract_summary, extract_code_summary
|
|
1
|
+
from .acreate_structured_output import acreate_structured_output
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from typing import Type
|
|
3
|
+
from cognee.shared.logging_utils import get_logger
|
|
4
|
+
|
|
5
|
+
from cognee.infrastructure.llm.config import get_llm_config
|
|
6
|
+
from cognee.infrastructure.llm.structured_output_framework.baml.baml_src.extraction.create_dynamic_baml_type import (
|
|
7
|
+
create_dynamic_baml_type,
|
|
8
|
+
)
|
|
9
|
+
from cognee.infrastructure.llm.structured_output_framework.baml.baml_client.type_builder import (
|
|
10
|
+
TypeBuilder,
|
|
11
|
+
)
|
|
12
|
+
from cognee.infrastructure.llm.structured_output_framework.baml.baml_client import b
|
|
13
|
+
from pydantic import BaseModel
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
logger = get_logger()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def acreate_structured_output(
|
|
20
|
+
text_input: str, system_prompt: str, response_model: Type[BaseModel]
|
|
21
|
+
):
|
|
22
|
+
"""
|
|
23
|
+
Generate a response from a user query.
|
|
24
|
+
|
|
25
|
+
This method asynchronously creates structured output by sending a request through BAML
|
|
26
|
+
using the provided parameters to generate a completion based on the user input and
|
|
27
|
+
system prompt.
|
|
28
|
+
|
|
29
|
+
Parameters:
|
|
30
|
+
-----------
|
|
31
|
+
|
|
32
|
+
- text_input (str): The input text provided by the user for generating a response.
|
|
33
|
+
- system_prompt (str): The system's prompt to guide the model's response.
|
|
34
|
+
- response_model (Type[BaseModel]): The expected model type for the response.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
--------
|
|
38
|
+
|
|
39
|
+
- BaseModel: A structured output generated by the model, returned as an instance of
|
|
40
|
+
BaseModel.
|
|
41
|
+
"""
|
|
42
|
+
config = get_llm_config()
|
|
43
|
+
|
|
44
|
+
# Dynamically create BAML response model
|
|
45
|
+
tb = TypeBuilder()
|
|
46
|
+
type_builder = create_dynamic_baml_type(tb, tb.ResponseModel, response_model)
|
|
47
|
+
|
|
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
|
+
)
|
|
53
|
+
|
|
54
|
+
# Transform BAML response to proper pydantic reponse model
|
|
55
|
+
if response_model is str:
|
|
56
|
+
# Note: when a response model is set to string in python, result is stored in text property in the BAML response model
|
|
57
|
+
return str(result.text)
|
|
58
|
+
return response_model.model_validate(result.dict())
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if __name__ == "__main__":
|
|
62
|
+
loop = asyncio.new_event_loop()
|
|
63
|
+
asyncio.set_event_loop(loop)
|
|
64
|
+
try:
|
|
65
|
+
from typing import Optional, Dict, Any, List, Literal
|
|
66
|
+
|
|
67
|
+
# Models for representing different entities
|
|
68
|
+
class TestModel(BaseModel):
|
|
69
|
+
type: str
|
|
70
|
+
source: Optional[str] = None
|
|
71
|
+
target: Optional[str] = None
|
|
72
|
+
properties: Optional[Dict[str, List[str]]] = None
|
|
73
|
+
|
|
74
|
+
loop.run_until_complete(acreate_structured_output("TEST", "THIS IS A TEST", TestModel))
|
|
75
|
+
finally:
|
|
76
|
+
loop.run_until_complete(loop.shutdown_asyncgens())
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from typing import get_origin, get_args
|
|
5
|
+
|
|
6
|
+
from baml_py.baml_py import ClassBuilder
|
|
7
|
+
from cognee.shared.logging_utils import get_logger
|
|
8
|
+
|
|
9
|
+
logger = get_logger()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def create_dynamic_baml_type(tb, baml_model, pydantic_model):
|
|
13
|
+
if pydantic_model is str:
|
|
14
|
+
baml_model.add_property("text", tb.string())
|
|
15
|
+
return tb
|
|
16
|
+
|
|
17
|
+
def map_type(field_type, field_info):
|
|
18
|
+
"""
|
|
19
|
+
Convert a Python / Pydantic type -> BAML TypeBuilder representation.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
origin = get_origin(field_type) # e.g. list[…] -> list
|
|
23
|
+
args = get_args(field_type) # e.g. list[int] -> (int,)
|
|
24
|
+
|
|
25
|
+
# ------------------------------------------------------------------
|
|
26
|
+
# 1. Optional / Union ------------------------------------------------
|
|
27
|
+
# ------------------------------------------------------------------
|
|
28
|
+
if origin is Union:
|
|
29
|
+
non_none_args = [t for t in args if t is not type(None)]
|
|
30
|
+
|
|
31
|
+
# Optional[T] ⇢ exactly (T, NoneType)
|
|
32
|
+
if len(args) == 2 and len(non_none_args) == 1:
|
|
33
|
+
return map_type(non_none_args[0], field_info).optional()
|
|
34
|
+
|
|
35
|
+
# Plain Union[A, B, …]
|
|
36
|
+
return tb.union(*(map_type(t, field_info) for t in args))
|
|
37
|
+
|
|
38
|
+
# ------------------------------------------------------------------
|
|
39
|
+
# 2. List / Sequence -------------------------------------------------
|
|
40
|
+
# ------------------------------------------------------------------
|
|
41
|
+
if origin in (list,):
|
|
42
|
+
(inner_type,) = args # list has exactly one parameter
|
|
43
|
+
return map_type(inner_type, field_info).list()
|
|
44
|
+
|
|
45
|
+
# ------------------------------------------------------------------
|
|
46
|
+
# 3. Dict / Map -------------------------------------------------------
|
|
47
|
+
# ------------------------------------------------------------------
|
|
48
|
+
def _is_enum_subclass(key_type) -> bool:
|
|
49
|
+
"""Guarded issubclass – returns False when tp is not a class."""
|
|
50
|
+
return isinstance(key_type, type) and issubclass(key_type, Enum)
|
|
51
|
+
|
|
52
|
+
if origin in (dict,):
|
|
53
|
+
key_type, value_type = args or (str, object)
|
|
54
|
+
|
|
55
|
+
if key_type is not str and not _is_enum_subclass(key_type):
|
|
56
|
+
raise ValueError("BAML maps only allow 'str' or Enum subclasses as keys")
|
|
57
|
+
|
|
58
|
+
return tb.map(
|
|
59
|
+
map_type(key_type, field_info), # mostly tb.string() or enum
|
|
60
|
+
map_type(value_type, field_info),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# ------------------------------------------------------------------
|
|
64
|
+
# 4. Enum -------------------------------------------------------------
|
|
65
|
+
# ------------------------------------------------------------------
|
|
66
|
+
if _is_enum_subclass(field_type):
|
|
67
|
+
enum_builder = tb.add_enum(field_type.__name__)
|
|
68
|
+
for member in field_type:
|
|
69
|
+
enum_builder.add_value(member.name)
|
|
70
|
+
return enum_builder.type()
|
|
71
|
+
|
|
72
|
+
# ------------------------------------------------------------------
|
|
73
|
+
# 5. Nested Pydantic model -------------------------------------------
|
|
74
|
+
# ------------------------------------------------------------------
|
|
75
|
+
from pydantic import BaseModel # local import
|
|
76
|
+
|
|
77
|
+
if isinstance(field_type, type) and issubclass(field_type, BaseModel):
|
|
78
|
+
try:
|
|
79
|
+
# Create nested class if it doesn't exist
|
|
80
|
+
nested_class = tb.add_class(field_type.__name__)
|
|
81
|
+
# Find dynamic types of nested class
|
|
82
|
+
create_dynamic_baml_type(tb, nested_class, field_type)
|
|
83
|
+
except ValueError:
|
|
84
|
+
# If nested class already exists get it
|
|
85
|
+
nested_class = tb._tb.class_(field_type.__name__)
|
|
86
|
+
|
|
87
|
+
# Return nested class model
|
|
88
|
+
if isinstance(nested_class, ClassBuilder):
|
|
89
|
+
# Different nested_class objects have different syntax for type information
|
|
90
|
+
# If nested class already exists type information can be found using the field method
|
|
91
|
+
return nested_class.field()
|
|
92
|
+
else:
|
|
93
|
+
# If nested class was created type information can be found using type method
|
|
94
|
+
return nested_class.type()
|
|
95
|
+
|
|
96
|
+
primitive_map = {
|
|
97
|
+
str: tb.string(),
|
|
98
|
+
int: tb.int(),
|
|
99
|
+
float: tb.float(),
|
|
100
|
+
bool: tb.bool(),
|
|
101
|
+
datetime: tb.string(), # BAML has no native datetime
|
|
102
|
+
}
|
|
103
|
+
if field_type in primitive_map:
|
|
104
|
+
return primitive_map[field_type]
|
|
105
|
+
|
|
106
|
+
raise ValueError(f"Unsupported type for BAML mapping: {field_type}")
|
|
107
|
+
|
|
108
|
+
fields = pydantic_model.model_fields
|
|
109
|
+
|
|
110
|
+
# Add fields
|
|
111
|
+
for field_name, field_info in fields.items():
|
|
112
|
+
field_type = field_info.annotation
|
|
113
|
+
baml_type = map_type(field_type, field_info)
|
|
114
|
+
|
|
115
|
+
# Add property with type
|
|
116
|
+
prop = baml_model.add_property(field_name, baml_type)
|
|
117
|
+
|
|
118
|
+
# Add description if available
|
|
119
|
+
if field_info.description:
|
|
120
|
+
prop.description(field_info.description)
|
|
121
|
+
|
|
122
|
+
return tb
|
|
@@ -6,13 +6,13 @@ generator target {
|
|
|
6
6
|
output_type "python/pydantic"
|
|
7
7
|
|
|
8
8
|
// Where the generated code will be saved (relative to baml_src/)
|
|
9
|
-
output_dir "../
|
|
9
|
+
output_dir "../"
|
|
10
10
|
|
|
11
11
|
// The version of the BAML package you have installed (e.g. same version as your baml-py or @boundaryml/baml).
|
|
12
12
|
// The BAML VSCode extension version should also match this version.
|
|
13
|
-
version "0.
|
|
13
|
+
version "0.206.0"
|
|
14
14
|
|
|
15
15
|
// Valid values: "sync", "async"
|
|
16
16
|
// This controls what `b.FunctionName()` will be (sync or async).
|
|
17
|
-
default_client_mode
|
|
17
|
+
default_client_mode async
|
|
18
18
|
}
|
cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py
CHANGED
|
@@ -70,35 +70,3 @@ class AnthropicAdapter(LLMInterface):
|
|
|
70
70
|
],
|
|
71
71
|
response_model=response_model,
|
|
72
72
|
)
|
|
73
|
-
|
|
74
|
-
def show_prompt(self, text_input: str, system_prompt: str) -> str:
|
|
75
|
-
"""
|
|
76
|
-
Format and display the prompt for a user query.
|
|
77
|
-
|
|
78
|
-
Parameters:
|
|
79
|
-
-----------
|
|
80
|
-
|
|
81
|
-
- text_input (str): The input text from the user, defaults to a placeholder if
|
|
82
|
-
empty.
|
|
83
|
-
- system_prompt (str): The path to the system prompt to be read and formatted.
|
|
84
|
-
|
|
85
|
-
Returns:
|
|
86
|
-
--------
|
|
87
|
-
|
|
88
|
-
- str: A formatted string displaying the system prompt and user input.
|
|
89
|
-
"""
|
|
90
|
-
|
|
91
|
-
if not text_input:
|
|
92
|
-
text_input = "No user input provided."
|
|
93
|
-
if not system_prompt:
|
|
94
|
-
raise MissingSystemPromptPathError()
|
|
95
|
-
|
|
96
|
-
system_prompt = LLMGateway.read_query_prompt(system_prompt)
|
|
97
|
-
|
|
98
|
-
formatted_prompt = (
|
|
99
|
-
f"""System Prompt:\n{system_prompt}\n\nUser Input:\n{text_input}\n"""
|
|
100
|
-
if system_prompt
|
|
101
|
-
else None
|
|
102
|
-
)
|
|
103
|
-
|
|
104
|
-
return formatted_prompt
|