chunk-metadata-adapter 1.0.0__tar.gz
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-1.0.0/PKG-INFO +218 -0
- chunk_metadata_adapter-1.0.0/README.md +201 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter/__init__.py +30 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter/examples.py +173 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter/metadata_builder.py +249 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter/models.py +364 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter.egg-info/PKG-INFO +218 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter.egg-info/SOURCES.txt +17 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter.egg-info/dependency_links.txt +1 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter.egg-info/requires.txt +1 -0
- chunk_metadata_adapter-1.0.0/chunk_metadata_adapter.egg-info/top_level.txt +1 -0
- chunk_metadata_adapter-1.0.0/pyproject.toml +47 -0
- chunk_metadata_adapter-1.0.0/setup.cfg +4 -0
- chunk_metadata_adapter-1.0.0/tests/test_examples_part1.py +113 -0
- chunk_metadata_adapter-1.0.0/tests/test_examples_part2.py +99 -0
- chunk_metadata_adapter-1.0.0/tests/test_metadata_builder_part1.py +217 -0
- chunk_metadata_adapter-1.0.0/tests/test_metadata_builder_part2.py +215 -0
- chunk_metadata_adapter-1.0.0/tests/test_models_part1.py +235 -0
- chunk_metadata_adapter-1.0.0/tests/test_models_part2.py +388 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: chunk_metadata_adapter
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Reusable metadata builder for chunk-based systems
|
|
5
|
+
Author: SmartAssistant
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourusername/chunk_metadata_adapter
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/chunk_metadata_adapter/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Requires-Python: >=3.8
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
Requires-Dist: pydantic>=2.0.0
|
|
17
|
+
|
|
18
|
+
# Chunk Metadata Adapter
|
|
19
|
+
|
|
20
|
+
[](https://badge.fury.io/py/chunk-metadata-adapter)
|
|
21
|
+
[](https://opensource.org/licenses/MIT)
|
|
22
|
+
|
|
23
|
+
A library for creating, transforming, and managing universal metadata for content chunks for RAG systems, documentation processing, and machine learning training.
|
|
24
|
+
|
|
25
|
+
[Русская версия](README.md)
|
|
26
|
+
|
|
27
|
+
## Purpose
|
|
28
|
+
|
|
29
|
+
The package provides tools for:
|
|
30
|
+
|
|
31
|
+
1. Creating structured metadata for content chunks (text, code, messages)
|
|
32
|
+
2. Converting between flat and structured formats
|
|
33
|
+
3. Safely storing and validating connections between chunks
|
|
34
|
+
4. Tracking quality and usage metrics of chunks
|
|
35
|
+
5. Ensuring data integrity through SHA256 hashing
|
|
36
|
+
6. Strict validation of identifiers (UUID4) and timestamps (ISO8601 with timezone)
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install chunk-metadata-adapter
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Data Models
|
|
45
|
+
|
|
46
|
+
The package provides two main data formats:
|
|
47
|
+
|
|
48
|
+
### SemanticChunk
|
|
49
|
+
|
|
50
|
+
A fully structured model with nested objects, convenient for programmatic processing:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
chunk = SemanticChunk(
|
|
54
|
+
uuid="550e8400-e29b-41d4-a716-446655440000",
|
|
55
|
+
type=ChunkType.DOC_BLOCK,
|
|
56
|
+
role=ChunkRole.DEVELOPER,
|
|
57
|
+
project="MyProject",
|
|
58
|
+
task_id="TASK-123",
|
|
59
|
+
subtask_id="TASK-123-A",
|
|
60
|
+
unit_id="chunker-service",
|
|
61
|
+
text="# Introduction\n\nThis is the system documentation.",
|
|
62
|
+
summary="Documentation introduction",
|
|
63
|
+
language="markdown",
|
|
64
|
+
source_id="48bb4273-1f56-4015-8b14-3d685b8cc9ae",
|
|
65
|
+
source_path="docs/intro.md",
|
|
66
|
+
source_lines=[1, 3],
|
|
67
|
+
ordinal=0,
|
|
68
|
+
created_at="2023-10-15T12:34:56.789+00:00",
|
|
69
|
+
status=ChunkStatus.INDEXED,
|
|
70
|
+
chunking_version="1.0",
|
|
71
|
+
sha256="abcdef1234567890...",
|
|
72
|
+
links=["parent:a1b2c3d4-e5f6-4a5b-8c7d-9e0f1a2b3c4d"],
|
|
73
|
+
tags=["introduction", "documentation"],
|
|
74
|
+
metrics=ChunkMetrics(quality_score=0.95, used_in_generation=True)
|
|
75
|
+
)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### FlatSemanticChunk
|
|
79
|
+
|
|
80
|
+
A flat structure for storage systems that prefer simple key-value formats:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
flat_chunk = FlatSemanticChunk(
|
|
84
|
+
uuid="550e8400-e29b-41d4-a716-446655440000",
|
|
85
|
+
source_id="48bb4273-1f56-4015-8b14-3d685b8cc9ae",
|
|
86
|
+
project="MyProject",
|
|
87
|
+
task_id="TASK-123",
|
|
88
|
+
subtask_id="TASK-123-A",
|
|
89
|
+
unit_id="chunker-service",
|
|
90
|
+
type="DocBlock",
|
|
91
|
+
role="developer",
|
|
92
|
+
language="markdown",
|
|
93
|
+
text="# Introduction\n\nThis is the system documentation.",
|
|
94
|
+
summary="Documentation introduction",
|
|
95
|
+
ordinal=0,
|
|
96
|
+
sha256="abcdef1234567890...",
|
|
97
|
+
created_at="2023-10-15T12:34:56.789+00:00",
|
|
98
|
+
status="indexed",
|
|
99
|
+
source_path="docs/intro.md",
|
|
100
|
+
source_lines_start=1,
|
|
101
|
+
source_lines_end=3,
|
|
102
|
+
tags="introduction,documentation",
|
|
103
|
+
link_parent="a1b2c3d4-e5f6-4a5b-8c7d-9e0f1a2b3c4d",
|
|
104
|
+
quality_score=0.95,
|
|
105
|
+
used_in_generation=True
|
|
106
|
+
)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Usage
|
|
110
|
+
|
|
111
|
+
### Creating Chunk Metadata
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
import uuid
|
|
115
|
+
from chunk_metadata_adapter import ChunkMetadataBuilder, ChunkType, ChunkRole
|
|
116
|
+
|
|
117
|
+
# Create a metadata builder for the project
|
|
118
|
+
builder = ChunkMetadataBuilder(project="MyProject", unit_id="chunker-service-1")
|
|
119
|
+
|
|
120
|
+
# Generate a UUID for the source document
|
|
121
|
+
source_id = str(uuid.uuid4())
|
|
122
|
+
|
|
123
|
+
# Create metadata for a code chunk
|
|
124
|
+
metadata = builder.build_flat_metadata(
|
|
125
|
+
text="def hello_world():\n print('Hello, World!')",
|
|
126
|
+
source_id=source_id,
|
|
127
|
+
ordinal=1,
|
|
128
|
+
type=ChunkType.CODE_BLOCK,
|
|
129
|
+
language="python",
|
|
130
|
+
source_path="src/hello.py",
|
|
131
|
+
source_lines_start=10,
|
|
132
|
+
source_lines_end=12,
|
|
133
|
+
tags="example,hello",
|
|
134
|
+
role=ChunkRole.DEVELOPER
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
print(f"Chunk UUID: {metadata['uuid']}")
|
|
138
|
+
print(f"SHA256: {metadata['sha256']}")
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Creating a Structured Chunk
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from chunk_metadata_adapter import ChunkMetadataBuilder, ChunkType, ChunkRole
|
|
145
|
+
|
|
146
|
+
# Create a builder for a documentation project
|
|
147
|
+
builder = ChunkMetadataBuilder(
|
|
148
|
+
project="DocumentationProject",
|
|
149
|
+
unit_id="docs-generator"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Create a structured chunk
|
|
153
|
+
chunk = builder.build_semantic_chunk(
|
|
154
|
+
text="# Introduction\n\nThis is the system documentation.",
|
|
155
|
+
language="markdown",
|
|
156
|
+
type=ChunkType.DOC_BLOCK,
|
|
157
|
+
source_id=str(uuid.uuid4()),
|
|
158
|
+
summary="Project introduction section",
|
|
159
|
+
role=ChunkRole.DEVELOPER,
|
|
160
|
+
source_path="docs/intro.md",
|
|
161
|
+
source_lines=[1, 3],
|
|
162
|
+
ordinal=0,
|
|
163
|
+
task_id="DOC-123",
|
|
164
|
+
subtask_id="DOC-123-A",
|
|
165
|
+
tags=["introduction", "documentation", "overview"],
|
|
166
|
+
links=[f"parent:{str(uuid.uuid4())}"]
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
print(f"Chunk UUID: {chunk.uuid}")
|
|
170
|
+
print(f"Summary: {chunk.summary}")
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Converting Between Formats
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
# Convert from structured to flat format
|
|
177
|
+
flat_dict = builder.semantic_to_flat(chunk)
|
|
178
|
+
|
|
179
|
+
# Convert from flat to structured format
|
|
180
|
+
restored_chunk = builder.flat_to_semantic(flat_dict)
|
|
181
|
+
|
|
182
|
+
# Verify they are equivalent
|
|
183
|
+
assert restored_chunk.uuid == chunk.uuid
|
|
184
|
+
assert restored_chunk.text == chunk.text
|
|
185
|
+
assert restored_chunk.type == chunk.type
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Features
|
|
189
|
+
|
|
190
|
+
- **Strict Type Validation:** All UUIDs are validated against the UUIDv4 format, dates are validated against ISO8601 with timezone.
|
|
191
|
+
- **Flexible Formats:** Support for both structured and flat data representations for different storage systems.
|
|
192
|
+
- **Built-in Integrity:** Automatic calculation of SHA256 hashes for integrity verification.
|
|
193
|
+
- **Compatibility:** Designed to work in the RAG and ML system ecosystem.
|
|
194
|
+
- **Type Safety:** Full typing support for IDEs and static analysis.
|
|
195
|
+
|
|
196
|
+
## Documentation
|
|
197
|
+
|
|
198
|
+
Documentation is available in English and Russian:
|
|
199
|
+
|
|
200
|
+
### English Documentation
|
|
201
|
+
- [Documentation Overview](docs/README.md)
|
|
202
|
+
- [Metadata Structure](docs/Metadata.md)
|
|
203
|
+
- [Usage Guide](docs/Usage.md)
|
|
204
|
+
- [Component Interaction](docs/Component_Interaction.md)
|
|
205
|
+
|
|
206
|
+
### Russian Documentation
|
|
207
|
+
- [Обзор документации](docs/README.ru.md)
|
|
208
|
+
- [Структура метаданных](docs/Metadata.ru.md)
|
|
209
|
+
- [Руководство по использованию](docs/Usage.ru.md)
|
|
210
|
+
- [Взаимодействие компонентов](docs/Component_Interaction.ru.md)
|
|
211
|
+
|
|
212
|
+
## License
|
|
213
|
+
|
|
214
|
+
MIT
|
|
215
|
+
|
|
216
|
+
## Author
|
|
217
|
+
|
|
218
|
+
SmartAssistant Team
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# Chunk Metadata Adapter
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/chunk-metadata-adapter)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
A library for creating, transforming, and managing universal metadata for content chunks for RAG systems, documentation processing, and machine learning training.
|
|
7
|
+
|
|
8
|
+
[Русская версия](README.md)
|
|
9
|
+
|
|
10
|
+
## Purpose
|
|
11
|
+
|
|
12
|
+
The package provides tools for:
|
|
13
|
+
|
|
14
|
+
1. Creating structured metadata for content chunks (text, code, messages)
|
|
15
|
+
2. Converting between flat and structured formats
|
|
16
|
+
3. Safely storing and validating connections between chunks
|
|
17
|
+
4. Tracking quality and usage metrics of chunks
|
|
18
|
+
5. Ensuring data integrity through SHA256 hashing
|
|
19
|
+
6. Strict validation of identifiers (UUID4) and timestamps (ISO8601 with timezone)
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install chunk-metadata-adapter
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Data Models
|
|
28
|
+
|
|
29
|
+
The package provides two main data formats:
|
|
30
|
+
|
|
31
|
+
### SemanticChunk
|
|
32
|
+
|
|
33
|
+
A fully structured model with nested objects, convenient for programmatic processing:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
chunk = SemanticChunk(
|
|
37
|
+
uuid="550e8400-e29b-41d4-a716-446655440000",
|
|
38
|
+
type=ChunkType.DOC_BLOCK,
|
|
39
|
+
role=ChunkRole.DEVELOPER,
|
|
40
|
+
project="MyProject",
|
|
41
|
+
task_id="TASK-123",
|
|
42
|
+
subtask_id="TASK-123-A",
|
|
43
|
+
unit_id="chunker-service",
|
|
44
|
+
text="# Introduction\n\nThis is the system documentation.",
|
|
45
|
+
summary="Documentation introduction",
|
|
46
|
+
language="markdown",
|
|
47
|
+
source_id="48bb4273-1f56-4015-8b14-3d685b8cc9ae",
|
|
48
|
+
source_path="docs/intro.md",
|
|
49
|
+
source_lines=[1, 3],
|
|
50
|
+
ordinal=0,
|
|
51
|
+
created_at="2023-10-15T12:34:56.789+00:00",
|
|
52
|
+
status=ChunkStatus.INDEXED,
|
|
53
|
+
chunking_version="1.0",
|
|
54
|
+
sha256="abcdef1234567890...",
|
|
55
|
+
links=["parent:a1b2c3d4-e5f6-4a5b-8c7d-9e0f1a2b3c4d"],
|
|
56
|
+
tags=["introduction", "documentation"],
|
|
57
|
+
metrics=ChunkMetrics(quality_score=0.95, used_in_generation=True)
|
|
58
|
+
)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### FlatSemanticChunk
|
|
62
|
+
|
|
63
|
+
A flat structure for storage systems that prefer simple key-value formats:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
flat_chunk = FlatSemanticChunk(
|
|
67
|
+
uuid="550e8400-e29b-41d4-a716-446655440000",
|
|
68
|
+
source_id="48bb4273-1f56-4015-8b14-3d685b8cc9ae",
|
|
69
|
+
project="MyProject",
|
|
70
|
+
task_id="TASK-123",
|
|
71
|
+
subtask_id="TASK-123-A",
|
|
72
|
+
unit_id="chunker-service",
|
|
73
|
+
type="DocBlock",
|
|
74
|
+
role="developer",
|
|
75
|
+
language="markdown",
|
|
76
|
+
text="# Introduction\n\nThis is the system documentation.",
|
|
77
|
+
summary="Documentation introduction",
|
|
78
|
+
ordinal=0,
|
|
79
|
+
sha256="abcdef1234567890...",
|
|
80
|
+
created_at="2023-10-15T12:34:56.789+00:00",
|
|
81
|
+
status="indexed",
|
|
82
|
+
source_path="docs/intro.md",
|
|
83
|
+
source_lines_start=1,
|
|
84
|
+
source_lines_end=3,
|
|
85
|
+
tags="introduction,documentation",
|
|
86
|
+
link_parent="a1b2c3d4-e5f6-4a5b-8c7d-9e0f1a2b3c4d",
|
|
87
|
+
quality_score=0.95,
|
|
88
|
+
used_in_generation=True
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Usage
|
|
93
|
+
|
|
94
|
+
### Creating Chunk Metadata
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import uuid
|
|
98
|
+
from chunk_metadata_adapter import ChunkMetadataBuilder, ChunkType, ChunkRole
|
|
99
|
+
|
|
100
|
+
# Create a metadata builder for the project
|
|
101
|
+
builder = ChunkMetadataBuilder(project="MyProject", unit_id="chunker-service-1")
|
|
102
|
+
|
|
103
|
+
# Generate a UUID for the source document
|
|
104
|
+
source_id = str(uuid.uuid4())
|
|
105
|
+
|
|
106
|
+
# Create metadata for a code chunk
|
|
107
|
+
metadata = builder.build_flat_metadata(
|
|
108
|
+
text="def hello_world():\n print('Hello, World!')",
|
|
109
|
+
source_id=source_id,
|
|
110
|
+
ordinal=1,
|
|
111
|
+
type=ChunkType.CODE_BLOCK,
|
|
112
|
+
language="python",
|
|
113
|
+
source_path="src/hello.py",
|
|
114
|
+
source_lines_start=10,
|
|
115
|
+
source_lines_end=12,
|
|
116
|
+
tags="example,hello",
|
|
117
|
+
role=ChunkRole.DEVELOPER
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
print(f"Chunk UUID: {metadata['uuid']}")
|
|
121
|
+
print(f"SHA256: {metadata['sha256']}")
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Creating a Structured Chunk
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from chunk_metadata_adapter import ChunkMetadataBuilder, ChunkType, ChunkRole
|
|
128
|
+
|
|
129
|
+
# Create a builder for a documentation project
|
|
130
|
+
builder = ChunkMetadataBuilder(
|
|
131
|
+
project="DocumentationProject",
|
|
132
|
+
unit_id="docs-generator"
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
# Create a structured chunk
|
|
136
|
+
chunk = builder.build_semantic_chunk(
|
|
137
|
+
text="# Introduction\n\nThis is the system documentation.",
|
|
138
|
+
language="markdown",
|
|
139
|
+
type=ChunkType.DOC_BLOCK,
|
|
140
|
+
source_id=str(uuid.uuid4()),
|
|
141
|
+
summary="Project introduction section",
|
|
142
|
+
role=ChunkRole.DEVELOPER,
|
|
143
|
+
source_path="docs/intro.md",
|
|
144
|
+
source_lines=[1, 3],
|
|
145
|
+
ordinal=0,
|
|
146
|
+
task_id="DOC-123",
|
|
147
|
+
subtask_id="DOC-123-A",
|
|
148
|
+
tags=["introduction", "documentation", "overview"],
|
|
149
|
+
links=[f"parent:{str(uuid.uuid4())}"]
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
print(f"Chunk UUID: {chunk.uuid}")
|
|
153
|
+
print(f"Summary: {chunk.summary}")
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Converting Between Formats
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
# Convert from structured to flat format
|
|
160
|
+
flat_dict = builder.semantic_to_flat(chunk)
|
|
161
|
+
|
|
162
|
+
# Convert from flat to structured format
|
|
163
|
+
restored_chunk = builder.flat_to_semantic(flat_dict)
|
|
164
|
+
|
|
165
|
+
# Verify they are equivalent
|
|
166
|
+
assert restored_chunk.uuid == chunk.uuid
|
|
167
|
+
assert restored_chunk.text == chunk.text
|
|
168
|
+
assert restored_chunk.type == chunk.type
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Features
|
|
172
|
+
|
|
173
|
+
- **Strict Type Validation:** All UUIDs are validated against the UUIDv4 format, dates are validated against ISO8601 with timezone.
|
|
174
|
+
- **Flexible Formats:** Support for both structured and flat data representations for different storage systems.
|
|
175
|
+
- **Built-in Integrity:** Automatic calculation of SHA256 hashes for integrity verification.
|
|
176
|
+
- **Compatibility:** Designed to work in the RAG and ML system ecosystem.
|
|
177
|
+
- **Type Safety:** Full typing support for IDEs and static analysis.
|
|
178
|
+
|
|
179
|
+
## Documentation
|
|
180
|
+
|
|
181
|
+
Documentation is available in English and Russian:
|
|
182
|
+
|
|
183
|
+
### English Documentation
|
|
184
|
+
- [Documentation Overview](docs/README.md)
|
|
185
|
+
- [Metadata Structure](docs/Metadata.md)
|
|
186
|
+
- [Usage Guide](docs/Usage.md)
|
|
187
|
+
- [Component Interaction](docs/Component_Interaction.md)
|
|
188
|
+
|
|
189
|
+
### Russian Documentation
|
|
190
|
+
- [Обзор документации](docs/README.ru.md)
|
|
191
|
+
- [Структура метаданных](docs/Metadata.ru.md)
|
|
192
|
+
- [Руководство по использованию](docs/Usage.ru.md)
|
|
193
|
+
- [Взаимодействие компонентов](docs/Component_Interaction.ru.md)
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT
|
|
198
|
+
|
|
199
|
+
## Author
|
|
200
|
+
|
|
201
|
+
SmartAssistant Team
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
from .metadata_builder import ChunkMetadataBuilder
|
|
10
|
+
from .models import (
|
|
11
|
+
SemanticChunk,
|
|
12
|
+
FlatSemanticChunk,
|
|
13
|
+
ChunkType,
|
|
14
|
+
ChunkRole,
|
|
15
|
+
ChunkStatus,
|
|
16
|
+
ChunkMetrics,
|
|
17
|
+
FeedbackMetrics
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__version__ = "1.0.0"
|
|
21
|
+
__all__ = [
|
|
22
|
+
"ChunkMetadataBuilder",
|
|
23
|
+
"SemanticChunk",
|
|
24
|
+
"FlatSemanticChunk",
|
|
25
|
+
"ChunkType",
|
|
26
|
+
"ChunkRole",
|
|
27
|
+
"ChunkStatus",
|
|
28
|
+
"ChunkMetrics",
|
|
29
|
+
"FeedbackMetrics",
|
|
30
|
+
]
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Examples of how to use the Chunk Metadata Adapter.
|
|
3
|
+
|
|
4
|
+
This module contains practical examples for various use cases.
|
|
5
|
+
"""
|
|
6
|
+
import uuid
|
|
7
|
+
|
|
8
|
+
from chunk_metadata_adapter import (
|
|
9
|
+
ChunkMetadataBuilder,
|
|
10
|
+
SemanticChunk,
|
|
11
|
+
FlatSemanticChunk,
|
|
12
|
+
ChunkType,
|
|
13
|
+
ChunkRole,
|
|
14
|
+
ChunkStatus
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def example_basic_flat_metadata():
|
|
19
|
+
"""Example of creating basic flat metadata for a chunk."""
|
|
20
|
+
# Create a builder instance for a specific project
|
|
21
|
+
builder = ChunkMetadataBuilder(project="MyProject", unit_id="chunker-service-1")
|
|
22
|
+
|
|
23
|
+
# Generate UUID for the source document
|
|
24
|
+
source_id = str(uuid.uuid4())
|
|
25
|
+
|
|
26
|
+
# Create metadata for a piece of code
|
|
27
|
+
metadata = builder.build_flat_metadata(
|
|
28
|
+
text="def hello_world():\n print('Hello, World!')",
|
|
29
|
+
source_id=source_id,
|
|
30
|
+
ordinal=1, # First chunk in the document
|
|
31
|
+
type=ChunkType.CODE_BLOCK, # Using enum
|
|
32
|
+
language="python",
|
|
33
|
+
source_path="src/hello.py",
|
|
34
|
+
source_lines_start=10,
|
|
35
|
+
source_lines_end=12,
|
|
36
|
+
tags="example,hello",
|
|
37
|
+
role=ChunkRole.DEVELOPER
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# Access the metadata
|
|
41
|
+
print(f"Generated UUID: {metadata['uuid']}")
|
|
42
|
+
print(f"SHA256: {metadata['sha256']}")
|
|
43
|
+
print(f"Created at: {metadata['created_at']}")
|
|
44
|
+
|
|
45
|
+
return metadata
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def example_structured_chunk():
|
|
49
|
+
"""Example of creating a structured SemanticChunk instance."""
|
|
50
|
+
# Create a builder for a project with task
|
|
51
|
+
builder = ChunkMetadataBuilder(
|
|
52
|
+
project="DocumentationProject",
|
|
53
|
+
unit_id="docs-generator"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
# Generate a source document ID
|
|
57
|
+
source_id = str(uuid.uuid4())
|
|
58
|
+
|
|
59
|
+
# Create a structured chunk
|
|
60
|
+
chunk = builder.build_semantic_chunk(
|
|
61
|
+
text="# Introduction\n\nThis is the documentation for the system.",
|
|
62
|
+
language="markdown",
|
|
63
|
+
type=ChunkType.DOC_BLOCK,
|
|
64
|
+
source_id=source_id,
|
|
65
|
+
summary="Project introduction section",
|
|
66
|
+
role=ChunkRole.DEVELOPER,
|
|
67
|
+
source_path="docs/intro.md",
|
|
68
|
+
source_lines=[1, 3],
|
|
69
|
+
ordinal=0,
|
|
70
|
+
task_id="DOC-123",
|
|
71
|
+
subtask_id="DOC-123-A",
|
|
72
|
+
tags=["introduction", "documentation", "overview"],
|
|
73
|
+
links=[f"parent:{str(uuid.uuid4())}"]
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
# Access the data
|
|
77
|
+
print(f"Chunk UUID: {chunk.uuid}")
|
|
78
|
+
print(f"Content summary: {chunk.summary}")
|
|
79
|
+
print(f"Links: {chunk.links}")
|
|
80
|
+
|
|
81
|
+
return chunk
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def example_conversion_between_formats():
|
|
85
|
+
"""Example of converting between structured and flat formats."""
|
|
86
|
+
# Create a builder instance
|
|
87
|
+
builder = ChunkMetadataBuilder(project="ConversionExample")
|
|
88
|
+
|
|
89
|
+
# Start with a structured chunk
|
|
90
|
+
structured_chunk = builder.build_semantic_chunk(
|
|
91
|
+
text="This is a sample chunk for conversion demonstration.",
|
|
92
|
+
language="text",
|
|
93
|
+
type=ChunkType.COMMENT,
|
|
94
|
+
source_id=str(uuid.uuid4()),
|
|
95
|
+
role=ChunkRole.REVIEWER
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# Convert to flat dictionary
|
|
99
|
+
flat_dict = builder.semantic_to_flat(structured_chunk)
|
|
100
|
+
print(f"Flat representation has {len(flat_dict)} fields")
|
|
101
|
+
|
|
102
|
+
# Convert back to structured format
|
|
103
|
+
restored_chunk = builder.flat_to_semantic(flat_dict)
|
|
104
|
+
print(f"Restored structured chunk: {restored_chunk.uuid}")
|
|
105
|
+
|
|
106
|
+
# Verify they're equivalent
|
|
107
|
+
assert restored_chunk.uuid == structured_chunk.uuid
|
|
108
|
+
assert restored_chunk.text == structured_chunk.text
|
|
109
|
+
assert restored_chunk.type == structured_chunk.type
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
"original": structured_chunk,
|
|
113
|
+
"flat": flat_dict,
|
|
114
|
+
"restored": restored_chunk
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def example_chain_processing():
|
|
119
|
+
"""Example of a chain of processing for document chunks."""
|
|
120
|
+
# Create a document with multiple chunks
|
|
121
|
+
builder = ChunkMetadataBuilder(project="ChainExample", unit_id="processor")
|
|
122
|
+
source_id = str(uuid.uuid4())
|
|
123
|
+
|
|
124
|
+
# Create a sequence of chunks from a document
|
|
125
|
+
chunks = []
|
|
126
|
+
for i, text in enumerate([
|
|
127
|
+
"# Document Title",
|
|
128
|
+
"## Section 1\n\nThis is the content of section 1.",
|
|
129
|
+
"## Section 2\n\nThis is the content of section 2.",
|
|
130
|
+
"## Conclusion\n\nFinal thoughts on the topic."
|
|
131
|
+
]):
|
|
132
|
+
chunk = builder.build_semantic_chunk(
|
|
133
|
+
text=text,
|
|
134
|
+
language="markdown",
|
|
135
|
+
type=ChunkType.DOC_BLOCK,
|
|
136
|
+
source_id=source_id,
|
|
137
|
+
ordinal=i,
|
|
138
|
+
summary=f"Section {i}" if i > 0 else "Title"
|
|
139
|
+
)
|
|
140
|
+
chunks.append(chunk)
|
|
141
|
+
|
|
142
|
+
# Create explicit links between chunks (parent-child relationships)
|
|
143
|
+
for i in range(1, len(chunks)):
|
|
144
|
+
# Add parent link to the title chunk
|
|
145
|
+
chunks[i].links.append(f"parent:{chunks[0].uuid}")
|
|
146
|
+
# Update status to show progress
|
|
147
|
+
chunks[i].status = ChunkStatus.INDEXED
|
|
148
|
+
|
|
149
|
+
# Simulate processing and updating metrics
|
|
150
|
+
for chunk in chunks:
|
|
151
|
+
# Update metrics based on some processing
|
|
152
|
+
chunk.metrics.quality_score = 0.95
|
|
153
|
+
chunk.metrics.used_in_generation = True
|
|
154
|
+
chunk.metrics.matches = 3
|
|
155
|
+
|
|
156
|
+
# Add feedback
|
|
157
|
+
chunk.metrics.feedback.accepted = 2
|
|
158
|
+
|
|
159
|
+
# Print the processed chain
|
|
160
|
+
print(f"Processed {len(chunks)} chunks from source {source_id}")
|
|
161
|
+
for i, chunk in enumerate(chunks):
|
|
162
|
+
print(f"Chunk {i}: {chunk.summary} - Status: {chunk.status.value}")
|
|
163
|
+
|
|
164
|
+
return chunks
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
if __name__ == "__main__":
|
|
168
|
+
print("Running examples...")
|
|
169
|
+
example_basic_flat_metadata()
|
|
170
|
+
example_structured_chunk()
|
|
171
|
+
example_conversion_between_formats()
|
|
172
|
+
example_chain_processing()
|
|
173
|
+
print("All examples completed.")
|