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

unstructured: add missing from_dict method #532

Merged
merged 1 commit into from
Mar 4, 2024
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 @@ -8,9 +8,9 @@
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Union

from haystack import Document, component, default_to_dict
from haystack import Document, component, default_from_dict, default_to_dict
from haystack.components.converters.utils import normalize_metadata
from haystack.utils import Secret
from haystack.utils import Secret, deserialize_secrets_inplace
from tqdm import tqdm

from unstructured.documents.elements import Element # type: ignore[import]
Expand Down Expand Up @@ -91,6 +91,18 @@ def to_dict(self) -> Dict[str, Any]:
progress_bar=self.progress_bar,
)

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "UnstructuredFileConverter":
"""
Deserializes the component from a dictionary.
:param data:
Dictionary to deserialize from.
:returns:
Deserialized component.
"""
deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"])
return default_from_dict(cls, data)

@component.output_types(documents=List[Document])
def run(
self,
Expand Down
21 changes: 21 additions & 0 deletions integrations/unstructured/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ def test_to_dict(self):
},
}

def test_from_dict(self, monkeypatch):
monkeypatch.setenv("UNSTRUCTURED_API_KEY", "test-api-key")
converter_dict = {
"type": "haystack_integrations.components.converters.unstructured.converter.UnstructuredFileConverter",
"init_parameters": {
"api_url": "http://custom-url:8000/general",
"api_key": {"env_vars": ["UNSTRUCTURED_API_KEY"], "strict": False, "type": "env_var"},
"document_creation_mode": "one-doc-per-element",
"separator": "|",
"unstructured_kwargs": {"foo": "bar"},
"progress_bar": False,
},
}
converter = UnstructuredFileConverter.from_dict(converter_dict)
assert converter.api_url == "http://custom-url:8000/general"
assert converter.api_key.resolve_value() == "test-api-key"
assert converter.document_creation_mode == "one-doc-per-element"
assert converter.separator == "|"
assert converter.unstructured_kwargs == {"foo": "bar"}
assert not converter.progress_bar

@pytest.mark.integration
def test_run_one_doc_per_file(self, samples_path):
pdf_path = samples_path / "sample_pdf.pdf"
Expand Down