Skip to content
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

feat: support single metadata dictionary in MarkdownToDocument #6629

Merged
merged 9 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
support single metadata dict in markdown2document
  • Loading branch information
ZanSara committed Dec 22, 2023
commit be931f7a0b76c6982537ac5d88c67dbaadd71ccc
19 changes: 9 additions & 10 deletions haystack/components/converters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from haystack import Document, component
from haystack.dataclasses import ByteStream
from haystack.lazy_imports import LazyImport
from haystack.components.converters.utils import get_bytestream_from_source
from haystack.components.converters.utils import get_bytestream_from_source, normalize_metadata

with LazyImport("Run 'pip install markdown-it-py mdit_plain'") as markdown_conversion_imports:
from markdown_it import MarkdownIt
Expand All @@ -27,7 +27,7 @@ class MarkdownToDocument:
from haystack.components.converters.markdown import MarkdownToDocument

converter = MarkdownToDocument()
results = converter.run(sources=["sample.md"])
results = converter.run(sources=["sample.md"], meta={"date_added": datetime.now().isoformat()})
documents = results["documents"]
print(documents[0].content)
# 'This is a text from the markdown file.'
Expand All @@ -50,23 +50,22 @@ def run(self, sources: List[Union[str, Path, ByteStream]], meta: Optional[List[D
Reads text from a markdown file and executes optional preprocessing steps.

:param sources: A list of markdown data sources (file paths or binary objects)
:param meta: Optional list of metadata to attach to the Documents.
The length of the list must match the number of paths. Defaults to `None`.
:param meta: Optional metadata to attach to the Documents.
This value can be either a list of dictionaries or a single dictionary.
If it's a single dictionary, its content is added to the metadata of all produced Documents.
julian-risch marked this conversation as resolved.
Show resolved Hide resolved
If it's a list, the length of the list must match the number of sources, because the two lists will be zipped.
Defaults to `None`.
:return: A dictionary containing a list of Document objects under the 'documents' key.
"""
parser = MarkdownIt(renderer_cls=RendererPlain)
if self.table_to_single_line:
parser.enable("table")

documents = []

if meta is None:
meta = [{}] * len(sources)
elif len(sources) != len(meta):
raise ValueError("The length of the metadata list must match the number of sources.")
meta_list = normalize_metadata(meta=meta, sources_count=len(sources))

for source, metadata in tqdm(
zip(sources, meta),
zip(sources, meta_list),
total=len(sources),
desc="Converting markdown files to Documents",
disable=not self.progress_bar,
Expand Down
33 changes: 33 additions & 0 deletions releasenotes/notes/main-082bae7b20bd605d.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
prelude: >
Replace this text with content to appear at the top of the section for this
release. This is equivalent to the "Highlights" section we used before.
The prelude might repeat some details that are also present in other notes
from the same release, that's ok. Not every release note requires a prelude,
use it only to describe major features or notable changes.
upgrade:
- |
List upgrade notes here, or remove this section.
Upgrade notes should be rare: only list known/potential breaking changes,
or major changes that require user action before the upgrade.
Notes here must include steps that users can follow to 1. know if they're
affected and 2. handle the change gracefully on their end.
features:
- |
List new features here, or remove this section.
enhancements:
- |
List new behavior that is too small to be
considered a new feature, or remove this section.
issues:
- |
List known issues here, or remove this section. For example, if some change is experimental or known to not work in some cases, it should be mentioned here.
deprecations:
- |
List deprecations notes here, or remove this section. Deprecations should not be used for something that is removed in the release, use upgrade section instead. Deprecation should allow time for users to make necessary changes for the removal to happen in a future release.
security:
- |
Add security notes here, or remove this section.
fixes:
- |
Add normal bug fixes here, or remove this section.
10 changes: 6 additions & 4 deletions test/components/converters/test_markdown_to_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ def test_run(self, test_files_path):
assert "What to build with Haystack" in doc.content
assert "# git clone https://github.com/deepset-ai/haystack.git" in doc.content

def test_run_with_meta(self):
def test_run_with_meta(self, test_files_path):
bytestream = ByteStream(data=b"test", meta={"author": "test_author", "language": "en"})

converter = MarkdownToDocument()

with patch("haystack.components.converters.markdown.MarkdownIt"):
output = converter.run(sources=[bytestream], meta=[{"language": "it"}])
ZanSara marked this conversation as resolved.
Show resolved Hide resolved
document = output["documents"][0]
output = converter.run(
sources=[bytestream, test_files_path / "markdown" / "sample.md"], meta=[{"language": "it"}]
)

# check that the metadata from the bytestream is merged with that from the meta parameter
assert document.meta == {"author": "test_author", "language": "it"}
assert output["documents"][0].meta == {"author": "test_author", "language": "it"}
assert output["documents"][1].meta == {"language": "it"}

@pytest.mark.integration
def test_run_wrong_file_type(self, test_files_path, caplog):
Expand Down
Loading