Skip to content

feat: json document processor #1661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ChunkingStrategy(Enum):
PAGE = "page"
FIXED_SIZE_OVERLAP = "fixed_size_overlap"
PARAGRAPH = "paragraph"
JSON = "json"


class ChunkingSettings:
Expand Down
37 changes: 37 additions & 0 deletions code/backend/batch/utilities/document_chunking/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import json
from typing import List
from .document_chunking_base import DocumentChunkingBase
from langchain.text_splitter import RecursiveJsonSplitter
from .chunking_strategy import ChunkingSettings
from ..common.source_document import SourceDocument


class JSONDocumentChunking(DocumentChunkingBase):
def __init__(self) -> None:
pass

def chunk(
self, documents: List[SourceDocument], chunking: ChunkingSettings
) -> List[SourceDocument]:
full_document_content = "".join(
list(map(lambda document: str(document.content), documents))
)
document_url = documents[0].source
json_data = json.loads(full_document_content)
splitter = RecursiveJsonSplitter(max_chunk_size=chunking.chunk_size)
chunked_content_list = splitter.split_json(json_data)
# Create document for each chunk
documents = []
chunk_offset = 0
for idx, chunked_content in enumerate(chunked_content_list):
documents.append(
SourceDocument.from_metadata(
content=str(chunked_content),
document_url=document_url,
metadata={"offset": chunk_offset},
idx=idx,
)
)

chunk_offset += len(chunked_content)
return documents
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .page import PageDocumentChunking
from .fixed_size_overlap import FixedSizeOverlapDocumentChunking
from .paragraph import ParagraphDocumentChunking
from .json import JSONDocumentChunking


def get_document_chunker(chunking_strategy: str):
Expand All @@ -14,5 +15,7 @@ def get_document_chunker(chunking_strategy: str):
return FixedSizeOverlapDocumentChunking()
elif chunking_strategy == ChunkingStrategy.PARAGRAPH.value:
return ParagraphDocumentChunking()
elif chunking_strategy == ChunkingStrategy.JSON.value:
return JSONDocumentChunking()
else:
raise Exception(f"Unknown chunking strategy: {chunking_strategy}")
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def get_available_document_types(self) -> list[str]:
"jpg",
"png",
"docx",
"json"
}
if self.env_helper.USE_ADVANCED_IMAGE_PROCESSING:
document_types.update(ADVANCED_IMAGE_PROCESSING_FILE_TYPES)
Expand Down
11 changes: 11 additions & 0 deletions code/backend/batch/utilities/helpers/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@
"strategy": "docx"
}
},
{
"document_type": "json",
"chunking": {
"strategy": "json",
"size": 500,
"overlap": 100
},
"loading": {
"strategy": "web"
}
},
{
"document_type": "jpg",
"chunking": {
Expand Down
9 changes: 8 additions & 1 deletion code/tests/utilities/helpers/test_config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ def test_default_config_when_use_advanced_image_processing(env_helper_mock):
"chunking": expected_chunking,
"loading": {"strategy": "docx"},
},
{
"document_type": "json",
"chunking": {"strategy": "json", "size": 500, "overlap": 100},
"loading": {"strategy": "web"},
},
{"document_type": "jpeg", "use_advanced_image_processing": True},
{"document_type": "jpg", "use_advanced_image_processing": True},
{"document_type": "png", "use_advanced_image_processing": True},
Expand Down Expand Up @@ -420,7 +425,7 @@ def test_get_available_document_types(config: Config):

# then
assert sorted(document_types) == sorted(
["txt", "pdf", "url", "html", "htm", "md", "jpeg", "jpg", "png", "docx"]
["txt", "pdf", "url", "html", "htm", "md", "jpeg", "jpg", "png", "docx", "json"]
)


Expand Down Expand Up @@ -448,6 +453,7 @@ def test_get_available_document_types_when_advanced_image_processing_enabled(
"docx",
"tiff",
"bmp",
"json"
]
)

Expand All @@ -471,6 +477,7 @@ def test_get_available_chunking_strategies(config: Config):
"page",
"fixed_size_overlap",
"paragraph",
"json"
]
)

Expand Down
34 changes: 34 additions & 0 deletions code/tests/utilities/helpers/test_document_chunking_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,37 @@ def test_document_chunking_fixed_size_overlap():
chunked_documents[6].content
== " shows how the different chunking strategies work now!"
)


def test_document_chunking_json():
# Test json chunking strategy
chunking = ChunkingSettings({"strategy": ChunkingStrategy.JSON, "size": 175, "overlap": 0})

json_documents = [
SourceDocument(
content="""
{
"window":{
"title":"Sample Widget",
"name":"main_window",
"width":500,
"height":500
},
"image":{
"src":"Images/Sun.png",
"name":"sun1",
"hOffset":250,
"vOffset":250,
"alignment":"center"
}
}
""",
source="https://example.com/sample_document.json",
),
]

document_chunking = DocumentChunking()
chunked_documents = document_chunking.chunk(json_documents, chunking)
assert len(chunked_documents) == 2
assert chunked_documents[0].content == "{'window': {'title': 'Sample Widget', 'name': 'main_window', 'width': 500, 'height': 500}}"
assert chunked_documents[1].content == "{'image': {'src': 'Images/Sun.png', 'name': 'sun1', 'hOffset': 250, 'vOffset': 250, 'alignment': 'center'}}"
1 change: 1 addition & 0 deletions docs/supported_file_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Out-of-the-box, you can upload the following file types:
* HTML
* MD (Markdown)
* DOCX
* JSON