chunk-metadata-adapter 3.3.3__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.
- chunk_metadata_adapter/__init__.py +139 -0
- chunk_metadata_adapter/ast/__init__.py +85 -0
- chunk_metadata_adapter/ast/factory.py +91 -0
- chunk_metadata_adapter/ast/nodes.py +631 -0
- chunk_metadata_adapter/ast/parameterization.py +548 -0
- chunk_metadata_adapter/ast/serialization.py +112 -0
- chunk_metadata_adapter/ast/visitors.py +294 -0
- chunk_metadata_adapter/ast.py +84 -0
- chunk_metadata_adapter/ast_nodes.py +50 -0
- chunk_metadata_adapter/ast_optimizer.py +589 -0
- chunk_metadata_adapter/chunk_query.py +1336 -0
- chunk_metadata_adapter/complexity_analyzer.py +132 -0
- chunk_metadata_adapter/data_types.py +374 -0
- chunk_metadata_adapter/examples/__init__.py +21 -0
- chunk_metadata_adapter/examples/api_integration_examples.py +469 -0
- chunk_metadata_adapter/examples/ast_basic_usage.py +96 -0
- chunk_metadata_adapter/examples/ast_json_serialization_demo.py +220 -0
- chunk_metadata_adapter/examples/ast_parameterization_demo.py +302 -0
- chunk_metadata_adapter/examples/ast_visitor_pattern_usage.py +147 -0
- chunk_metadata_adapter/examples/bm25_search_examples.py +379 -0
- chunk_metadata_adapter/examples/bm25_tokens_example.py +118 -0
- chunk_metadata_adapter/examples/bm25_usage_examples.py +409 -0
- chunk_metadata_adapter/examples/chunk_query_integration_demo.py +321 -0
- chunk_metadata_adapter/examples/filter_executor_usage.py +316 -0
- chunk_metadata_adapter/examples/filter_parser_total_chunks_example.py +213 -0
- chunk_metadata_adapter/examples/metrics_demo.py +383 -0
- chunk_metadata_adapter/examples_module.py +623 -0
- chunk_metadata_adapter/filter_executor.py +698 -0
- chunk_metadata_adapter/filter_grammar.py +375 -0
- chunk_metadata_adapter/filter_parser.py +661 -0
- chunk_metadata_adapter/hybrid_search.py +391 -0
- chunk_metadata_adapter/metadata_builder.py +449 -0
- chunk_metadata_adapter/performance_analyzer.py +95 -0
- chunk_metadata_adapter/query_business_examples.py +319 -0
- chunk_metadata_adapter/query_examples.py +363 -0
- chunk_metadata_adapter/query_serialization_examples.py +352 -0
- chunk_metadata_adapter/query_validator.py +360 -0
- chunk_metadata_adapter/search_response.py +463 -0
- chunk_metadata_adapter/security_validator.py +237 -0
- chunk_metadata_adapter/semantic_chunk.py +1077 -0
- chunk_metadata_adapter/utils.py +505 -0
- chunk_metadata_adapter-3.3.3.dist-info/METADATA +561 -0
- chunk_metadata_adapter-3.3.3.dist-info/RECORD +45 -0
- chunk_metadata_adapter-3.3.3.dist-info/WHEEL +5 -0
- chunk_metadata_adapter-3.3.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Chunk Metadata Adapter - A package for managing metadata for chunked content.
|
|
3
|
+
|
|
4
|
+
This package provides tools for creating, managing, and converting metadata
|
|
5
|
+
for chunks of content in various systems, including RAG pipelines, document
|
|
6
|
+
processing, and machine learning training datasets.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
# Core classes
|
|
10
|
+
from .semantic_chunk import SemanticChunk, ChunkMetrics, FeedbackMetrics
|
|
11
|
+
from .chunk_query import ChunkQuery
|
|
12
|
+
from .metadata_builder import ChunkMetadataBuilder
|
|
13
|
+
|
|
14
|
+
# Data types and enums
|
|
15
|
+
from .data_types import (
|
|
16
|
+
ChunkType,
|
|
17
|
+
ChunkRole,
|
|
18
|
+
ChunkStatus,
|
|
19
|
+
BlockType,
|
|
20
|
+
LanguageEnum,
|
|
21
|
+
ComparableEnum,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# AST and parsing
|
|
25
|
+
from .ast import (
|
|
26
|
+
ASTNode,
|
|
27
|
+
ASTVisitor,
|
|
28
|
+
ASTValidator,
|
|
29
|
+
ASTAnalyzer,
|
|
30
|
+
FieldCondition,
|
|
31
|
+
LogicalOperator,
|
|
32
|
+
ParenExpression,
|
|
33
|
+
TypedValue,
|
|
34
|
+
ASTNodeFactory,
|
|
35
|
+
ast_to_json,
|
|
36
|
+
ast_from_json,
|
|
37
|
+
ast_to_json_string,
|
|
38
|
+
ast_from_json_string,
|
|
39
|
+
)
|
|
40
|
+
from .filter_parser import FilterParser, FilterParseError
|
|
41
|
+
from .filter_grammar import FILTER_GRAMMAR
|
|
42
|
+
from .ast_optimizer import ASTOptimizer, optimize_ast
|
|
43
|
+
from .filter_executor import FilterExecutor
|
|
44
|
+
|
|
45
|
+
# Search response handling
|
|
46
|
+
from .search_response import SearchResult, ChunkQueryResponse, SearchResponseBuilder
|
|
47
|
+
|
|
48
|
+
# Hybrid search functionality
|
|
49
|
+
from .hybrid_search import (
|
|
50
|
+
HybridStrategy,
|
|
51
|
+
HybridSearchConfig,
|
|
52
|
+
HybridSearchHelper,
|
|
53
|
+
HybridSearchValidator,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
# Validation and analysis
|
|
57
|
+
from .query_validator import QueryValidator, ValidationResult
|
|
58
|
+
from .complexity_analyzer import ComplexityAnalyzer, analyze_complexity
|
|
59
|
+
from .security_validator import SecurityValidator
|
|
60
|
+
from .performance_analyzer import PerformanceAnalyzer
|
|
61
|
+
|
|
62
|
+
# Examples
|
|
63
|
+
from .examples_module import (
|
|
64
|
+
example_basic_flat_metadata,
|
|
65
|
+
example_structured_chunk,
|
|
66
|
+
example_conversion_between_formats,
|
|
67
|
+
example_chain_processing,
|
|
68
|
+
example_data_lifecycle,
|
|
69
|
+
example_metrics_extension,
|
|
70
|
+
example_full_chain_structured_semantic_flat,
|
|
71
|
+
example_is_code_detection,
|
|
72
|
+
example_filter_factory_method,
|
|
73
|
+
example_filter_usage,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
__version__ = "3.3.3"
|
|
77
|
+
|
|
78
|
+
__all__ = [
|
|
79
|
+
# Core classes
|
|
80
|
+
"SemanticChunk",
|
|
81
|
+
"ChunkMetrics",
|
|
82
|
+
"ChunkQuery",
|
|
83
|
+
"ChunkMetadataBuilder",
|
|
84
|
+
# Data types and enums
|
|
85
|
+
"ChunkType",
|
|
86
|
+
"ChunkRole",
|
|
87
|
+
"ChunkStatus",
|
|
88
|
+
"BlockType",
|
|
89
|
+
"LanguageEnum",
|
|
90
|
+
"FeedbackMetrics",
|
|
91
|
+
"ComparableEnum",
|
|
92
|
+
# AST and parsing
|
|
93
|
+
"ASTNode",
|
|
94
|
+
"ASTVisitor",
|
|
95
|
+
"ASTValidator",
|
|
96
|
+
"ASTAnalyzer",
|
|
97
|
+
"FieldCondition",
|
|
98
|
+
"LogicalOperator",
|
|
99
|
+
"ParenExpression",
|
|
100
|
+
"TypedValue",
|
|
101
|
+
"ASTNodeFactory",
|
|
102
|
+
"ast_to_json",
|
|
103
|
+
"ast_from_json",
|
|
104
|
+
"ast_to_json_string",
|
|
105
|
+
"ast_from_json_string",
|
|
106
|
+
"FilterParser",
|
|
107
|
+
"FilterParseError",
|
|
108
|
+
"FILTER_GRAMMAR",
|
|
109
|
+
"ASTOptimizer",
|
|
110
|
+
"optimize_ast",
|
|
111
|
+
"FilterExecutor",
|
|
112
|
+
# Search response handling
|
|
113
|
+
"SearchResult",
|
|
114
|
+
"ChunkQueryResponse",
|
|
115
|
+
"SearchResponseBuilder",
|
|
116
|
+
# Hybrid search functionality
|
|
117
|
+
"HybridStrategy",
|
|
118
|
+
"HybridSearchConfig",
|
|
119
|
+
"HybridSearchHelper",
|
|
120
|
+
"HybridSearchValidator",
|
|
121
|
+
# Validation and analysis
|
|
122
|
+
"QueryValidator",
|
|
123
|
+
"ValidationResult",
|
|
124
|
+
"ComplexityAnalyzer",
|
|
125
|
+
"analyze_complexity",
|
|
126
|
+
"SecurityValidator",
|
|
127
|
+
"PerformanceAnalyzer",
|
|
128
|
+
# Examples
|
|
129
|
+
"example_basic_flat_metadata",
|
|
130
|
+
"example_structured_chunk",
|
|
131
|
+
"example_conversion_between_formats",
|
|
132
|
+
"example_chain_processing",
|
|
133
|
+
"example_data_lifecycle",
|
|
134
|
+
"example_metrics_extension",
|
|
135
|
+
"example_full_chain_structured_semantic_flat",
|
|
136
|
+
"example_is_code_detection",
|
|
137
|
+
"example_filter_factory_method",
|
|
138
|
+
"example_filter_usage",
|
|
139
|
+
]
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AST (Abstract Syntax Tree) module for filter expression parsing and execution.
|
|
3
|
+
|
|
4
|
+
This module provides the core data structures for representing filter expressions
|
|
5
|
+
as Abstract Syntax Trees (AST). It includes typed nodes for field conditions,
|
|
6
|
+
logical operators, and parenthesized expressions.
|
|
7
|
+
|
|
8
|
+
Key features:
|
|
9
|
+
- TypedValue: Type-safe value representation with validation
|
|
10
|
+
- FieldCondition: Field comparison conditions (age > 18)
|
|
11
|
+
- LogicalOperator: Logical operations (AND, OR, NOT)
|
|
12
|
+
- ParenExpression: Parenthesized expressions for precedence
|
|
13
|
+
- JSON serialization: Full serialization/deserialization support
|
|
14
|
+
- Visitor pattern: AST traversal and analysis
|
|
15
|
+
|
|
16
|
+
Usage examples:
|
|
17
|
+
>>> from chunk_metadata_adapter.ast import FieldCondition, TypedValue
|
|
18
|
+
>>> condition = FieldCondition("age", ">", TypedValue("int", 18))
|
|
19
|
+
>>> result = condition.evaluate({"age": 25})
|
|
20
|
+
|
|
21
|
+
Dependencies:
|
|
22
|
+
- dataclasses: For data structure definitions
|
|
23
|
+
- typing: For type hints and annotations
|
|
24
|
+
- datetime: For date/time value support
|
|
25
|
+
- json: For serialization support
|
|
26
|
+
|
|
27
|
+
Author: Development Team
|
|
28
|
+
Created: 2024-01-15
|
|
29
|
+
Updated: 2024-01-20
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
# Core AST classes
|
|
33
|
+
from .nodes import (
|
|
34
|
+
ASTNode, FieldCondition, LogicalOperator, ParenExpression, TypedValue
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# Visitor pattern classes
|
|
38
|
+
from .visitors import (
|
|
39
|
+
ASTVisitor, ASTPrinter, ASTValidator, ASTAnalyzer, ASTOptimizer
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Factory for creating AST nodes
|
|
43
|
+
from .factory import ASTNodeFactory
|
|
44
|
+
|
|
45
|
+
# JSON serialization utilities
|
|
46
|
+
from .serialization import (
|
|
47
|
+
ast_to_json, ast_from_json, ast_to_json_string, ast_from_json_string
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# Parameterization and caching
|
|
51
|
+
from .parameterization import (
|
|
52
|
+
ParameterValue, ParameterizedAST, ASTParameterizer, ASTInstantiator, QueryCache
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
__all__ = [
|
|
56
|
+
# Core AST classes
|
|
57
|
+
"ASTNode",
|
|
58
|
+
"FieldCondition",
|
|
59
|
+
"LogicalOperator",
|
|
60
|
+
"ParenExpression",
|
|
61
|
+
"TypedValue",
|
|
62
|
+
|
|
63
|
+
# Visitor pattern
|
|
64
|
+
"ASTVisitor",
|
|
65
|
+
"ASTPrinter",
|
|
66
|
+
"ASTValidator",
|
|
67
|
+
"ASTAnalyzer",
|
|
68
|
+
"ASTOptimizer",
|
|
69
|
+
|
|
70
|
+
# Factory
|
|
71
|
+
"ASTNodeFactory",
|
|
72
|
+
|
|
73
|
+
# JSON serialization
|
|
74
|
+
"ast_to_json",
|
|
75
|
+
"ast_from_json",
|
|
76
|
+
"ast_to_json_string",
|
|
77
|
+
"ast_from_json_string",
|
|
78
|
+
|
|
79
|
+
# Parameterization and caching
|
|
80
|
+
"ParameterValue",
|
|
81
|
+
"ParameterizedAST",
|
|
82
|
+
"ASTParameterizer",
|
|
83
|
+
"ASTInstantiator",
|
|
84
|
+
"QueryCache",
|
|
85
|
+
]
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AST node factory for convenient node creation.
|
|
3
|
+
|
|
4
|
+
This module provides a factory class for creating various types of AST nodes
|
|
5
|
+
with proper validation and convenient methods.
|
|
6
|
+
|
|
7
|
+
Key features:
|
|
8
|
+
- ASTNodeFactory: Factory for creating AST nodes
|
|
9
|
+
- Convenient methods for common node types
|
|
10
|
+
- Automatic validation and error handling
|
|
11
|
+
|
|
12
|
+
Usage examples:
|
|
13
|
+
>>> from ast.factory import ASTNodeFactory
|
|
14
|
+
>>> condition = ASTNodeFactory.create_simple_condition("age", ">", "int", 18)
|
|
15
|
+
>>> operator = ASTNodeFactory.create_and_operator([condition1, condition2])
|
|
16
|
+
|
|
17
|
+
Dependencies:
|
|
18
|
+
- typing: For type hints and annotations
|
|
19
|
+
- .nodes: For AST node classes
|
|
20
|
+
|
|
21
|
+
Author: Development Team
|
|
22
|
+
Created: 2024-01-15
|
|
23
|
+
Updated: 2024-01-20
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from typing import List, Literal, Any
|
|
27
|
+
from .nodes import ASTNode, FieldCondition, LogicalOperator, ParenExpression, TypedValue
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ASTNodeFactory:
|
|
31
|
+
"""
|
|
32
|
+
Factory for creating AST nodes.
|
|
33
|
+
|
|
34
|
+
This class provides convenient methods for creating
|
|
35
|
+
various types of AST nodes with proper validation.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def create_field_condition(field: str, operator: str, value: TypedValue) -> FieldCondition:
|
|
40
|
+
"""Create a field condition node."""
|
|
41
|
+
return FieldCondition(field=field, operator=operator, value=value)
|
|
42
|
+
|
|
43
|
+
@staticmethod
|
|
44
|
+
def create_logical_operator(operator: Literal["AND", "OR", "NOT", "XOR"],
|
|
45
|
+
children: List[ASTNode]) -> LogicalOperator:
|
|
46
|
+
"""Create a logical operator node."""
|
|
47
|
+
return LogicalOperator(operator=operator, children=children)
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def create_paren_expression(expression: ASTNode) -> ParenExpression:
|
|
51
|
+
"""Create a parenthesized expression node."""
|
|
52
|
+
return ParenExpression(expression=expression)
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def create_typed_value(type_name: str, value: Any) -> TypedValue:
|
|
56
|
+
"""Create a typed value."""
|
|
57
|
+
return TypedValue(type=type_name, value=value)
|
|
58
|
+
|
|
59
|
+
@staticmethod
|
|
60
|
+
def create_and_operator(children: List[ASTNode]) -> LogicalOperator:
|
|
61
|
+
"""Create an AND operator node."""
|
|
62
|
+
return ASTNodeFactory.create_logical_operator("AND", children)
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
def create_or_operator(children: List[ASTNode]) -> LogicalOperator:
|
|
66
|
+
"""Create an OR operator node."""
|
|
67
|
+
return ASTNodeFactory.create_logical_operator("OR", children)
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def create_not_operator(child: ASTNode) -> LogicalOperator:
|
|
71
|
+
"""Create a NOT operator node."""
|
|
72
|
+
return ASTNodeFactory.create_logical_operator("NOT", [child])
|
|
73
|
+
|
|
74
|
+
@staticmethod
|
|
75
|
+
def create_simple_condition(field: str, operator: str, type_name: str, value: Any) -> FieldCondition:
|
|
76
|
+
"""Create a simple field condition with automatic typed value creation."""
|
|
77
|
+
typed_value = ASTNodeFactory.create_typed_value(type_name, value)
|
|
78
|
+
return ASTNodeFactory.create_field_condition(field, operator, typed_value)
|
|
79
|
+
|
|
80
|
+
@staticmethod
|
|
81
|
+
def create_complex_expression(conditions: List[tuple],
|
|
82
|
+
operator: Literal["AND", "OR"] = "AND") -> LogicalOperator:
|
|
83
|
+
"""Create a complex expression from a list of conditions."""
|
|
84
|
+
field_conditions = []
|
|
85
|
+
for field, op, type_name, value in conditions:
|
|
86
|
+
condition = ASTNodeFactory.create_simple_condition(field, op, type_name, value)
|
|
87
|
+
field_conditions.append(condition)
|
|
88
|
+
if operator == "AND":
|
|
89
|
+
return ASTNodeFactory.create_and_operator(field_conditions)
|
|
90
|
+
else:
|
|
91
|
+
return ASTNodeFactory.create_or_operator(field_conditions)
|