mirror of
https://github.com/microsoft/graphrag.git
synced 2026-01-14 00:57:23 +08:00
Some checks failed
Python Build and Type Check / python-ci (ubuntu-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
* Delete NoopTextSplitter * Delete unused check_token_limit * Add base chunking factory and migrate workflow to use it * Split apart chunker module * Co-locate chunking/splitting * Collapse token splitting functionality into one class/function * Restore create_base_text_units parameterization * Move Tokenizer base class to common package * Move pre-pending into chunkers * Streamline config * Fix defaults construction * Add prepending tests * Remove chunk_size_includes_metadata config * Revert ChunkingDocument interface * Move metadata prepending to a util * Move Tokenizer back to GR core * Fix tokenizer removal from chunker * Set defaults for chunking config * Move chunking to monorepo package * Format * Typo * Add ChunkResult model * Streamline chunking config * Add missing version updates for graphrag_chunking
44 lines
1.7 KiB
Python
44 lines
1.7 KiB
Python
# Copyright (c) 2024 Microsoft Corporation.
|
|
# Licensed under the MIT License
|
|
|
|
from graphrag_chunking.add_metadata import add_metadata
|
|
|
|
|
|
def test_add_metadata_one_row():
|
|
"""Test prepending metadata to chunks"""
|
|
chunks = ["This is a test.", "Another sentence."]
|
|
metadata = {"message": "hello"}
|
|
results = [add_metadata(chunk, metadata) for chunk in chunks]
|
|
assert results[0] == "message: hello\nThis is a test."
|
|
assert results[1] == "message: hello\nAnother sentence."
|
|
|
|
|
|
def test_add_metadata_one_row_append():
|
|
"""Test prepending metadata to chunks"""
|
|
chunks = ["This is a test.", "Another sentence."]
|
|
metadata = {"message": "hello"}
|
|
results = [add_metadata(chunk, metadata, append=True) for chunk in chunks]
|
|
assert results[0] == "This is a test.message: hello\n"
|
|
assert results[1] == "Another sentence.message: hello\n"
|
|
|
|
|
|
def test_add_metadata_multiple_rows():
|
|
"""Test prepending metadata to chunks"""
|
|
chunks = ["This is a test.", "Another sentence."]
|
|
metadata = {"message": "hello", "tag": "first"}
|
|
results = [add_metadata(chunk, metadata) for chunk in chunks]
|
|
assert results[0] == "message: hello\ntag: first\nThis is a test."
|
|
assert results[1] == "message: hello\ntag: first\nAnother sentence."
|
|
|
|
|
|
def test_add_metadata_custom_delimiters():
|
|
"""Test prepending metadata to chunks"""
|
|
chunks = ["This is a test.", "Another sentence."]
|
|
metadata = {"message": "hello", "tag": "first"}
|
|
results = [
|
|
add_metadata(chunk, metadata, delimiter="-", line_delimiter="_")
|
|
for chunk in chunks
|
|
]
|
|
assert results[0] == "message-hello_tag-first_This is a test."
|
|
assert results[1] == "message-hello_tag-first_Another sentence."
|