File size: 10,670 Bytes
362a075 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import json
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Set, Tuple, Union
from haystack import component, default_from_dict, default_to_dict, logging
from haystack.components.converters.utils import get_bytestream_from_source, normalize_metadata
from haystack.dataclasses import ByteStream, Document
from haystack.lazy_imports import LazyImport
logger = logging.getLogger(__name__)
with LazyImport("Run 'pip install jq'") as jq_import:
import jq
@component
class JSONConverter:
"""
Converts one or more JSON files into a text document.
### Usage examples
```python
import json
from haystack.components.converters import JSONConverter
from haystack.dataclasses import ByteStream
source = ByteStream.from_string(json.dumps({"text": "This is the content of my document"}))
converter = JSONConverter(content_key="text")
results = converter.run(sources=[source])
documents = results["documents"]
print(documents[0].content)
# 'This is the content of my document'
```
Optionally, you can also provide a `jq_schema` string to filter the JSON source files and `extra_meta_fields`
to extract from the filtered data:
```python
import json
from haystack.components.converters import JSONConverter
from haystack.dataclasses import ByteStream
data = {
"laureates": [
{
"firstname": "Enrico",
"surname": "Fermi",
"motivation": "for his demonstrations of the existence of new radioactive elements produced "
"by neutron irradiation, and for his related discovery of nuclear reactions brought about by"
" slow neutrons",
},
{
"firstname": "Rita",
"surname": "Levi-Montalcini",
"motivation": "for their discoveries of growth factors",
},
],
}
source = ByteStream.from_string(json.dumps(data))
converter = JSONConverter(
jq_schema=".laureates[]", content_key="motivation", extra_meta_fields={"firstname", "surname"}
)
results = converter.run(sources=[source])
documents = results["documents"]
print(documents[0].content)
# 'for his demonstrations of the existence of new radioactive elements produced by
# neutron irradiation, and for his related discovery of nuclear reactions brought
# about by slow neutrons'
print(documents[0].meta)
# {'firstname': 'Enrico', 'surname': 'Fermi'}
print(documents[1].content)
# 'for their discoveries of growth factors'
print(documents[1].meta)
# {'firstname': 'Rita', 'surname': 'Levi-Montalcini'}
```
"""
def __init__(
self,
jq_schema: Optional[str] = None,
content_key: Optional[str] = None,
extra_meta_fields: Optional[Union[Set[str], Literal["*"]]] = None,
):
"""
Creates a JSONConverter component.
An optional `jq_schema` can be provided to extract nested data in the JSON source files.
See the [official jq documentation](https://jqlang.github.io/jq/) for more info on the filters syntax.
If `jq_schema` is not set, whole JSON source files will be used to extract content.
Optionally, you can provide a `content_key` to specify which key in the extracted object must
be set as the document's content.
If both `jq_schema` and `content_key` are set, the component will search for the `content_key` in
the JSON object extracted by `jq_schema`. If the extracted data is not a JSON object, it will be skipped.
If only `jq_schema` is set, the extracted data must be a scalar value. If it's a JSON object or array,
it will be skipped.
If only `content_key` is set, the source JSON file must be a JSON object, else it will be skipped.
`extra_meta_fields` can either be set to a set of strings or a literal `"*"` string.
If it's a set of strings, it must specify fields in the extracted objects that must be set in
the extracted documents. If a field is not found, the meta value will be `None`.
If set to `"*"`, all fields that are not `content_key` found in the filtered JSON object will
be saved as metadata.
Initialization will fail if neither `jq_schema` nor `content_key` are set.
:param jq_schema:
Optional jq filter string to extract content.
If not specified, whole JSON object will be used to extract information.
:param content_key:
Optional key to extract document content.
If `jq_schema` is specified, the `content_key` will be extracted from that object.
:param extra_meta_fields:
An optional set of meta keys to extract from the content.
If `jq_schema` is specified, all keys will be extracted from that object.
"""
self._compiled_filter = None
if jq_schema:
jq_import.check()
self._compiled_filter = jq.compile(jq_schema)
self._jq_schema = jq_schema
self._content_key = content_key
self._meta_fields = extra_meta_fields
if self._compiled_filter is None and self._content_key is None:
msg = "No `jq_schema` nor `content_key` specified. Set either or both to extract data."
raise ValueError(msg)
def to_dict(self) -> Dict[str, Any]:
"""
Serializes the component to a dictionary.
:returns:
Dictionary with serialized data.
"""
return default_to_dict(
self, jq_schema=self._jq_schema, content_key=self._content_key, extra_meta_fields=self._meta_fields
)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "JSONConverter":
"""
Deserializes the component from a dictionary.
:param data:
Dictionary to deserialize from.
:returns:
Deserialized component.
"""
return default_from_dict(cls, data)
def _get_content_and_meta(self, source: ByteStream) -> List[Tuple[str, Dict[str, Any]]]:
"""
Utility function to extract text and metadata from a JSON file.
:param source:
UTF-8 byte stream.
:returns:
Collection of text and metadata dict tuples, each corresponding
to a different document.
"""
try:
file_content = source.data.decode("utf-8")
except UnicodeError as exc:
logger.warning(
"Failed to extract text from {source}. Skipping it. Error: {error}",
source=source.meta["file_path"],
error=exc,
)
meta_fields = self._meta_fields or set()
if self._compiled_filter is not None:
try:
objects = list(self._compiled_filter.input_text(file_content))
except Exception as exc:
logger.warning(
"Failed to extract text from {source}. Skipping it. Error: {error}",
source=source.meta["file_path"],
error=exc,
)
return []
else:
# We just load the whole file as JSON if the user didn't provide a jq filter.
# We put it in a list even if it's not to ease handling it later on.
objects = [json.loads(file_content)]
result = []
if self._content_key is not None:
for obj in objects:
if not isinstance(obj, dict):
logger.warning("Expected a dictionary but got {obj}. Skipping it.", obj=obj)
continue
if self._content_key not in obj:
logger.warning(
"'{content_key}' not found in {obj}. Skipping it.", content_key=self._content_key, obj=obj
)
continue
text = obj[self._content_key]
if isinstance(text, (dict, list)):
logger.warning("Expected a scalar value but got {obj}. Skipping it.", obj=obj)
continue
meta = {}
if meta_fields == "*":
meta = {k: v for k, v in obj.items() if k != self._content_key}
else:
for field in meta_fields:
meta[field] = obj.get(field, None)
result.append((text, meta))
else:
for obj in objects:
if isinstance(obj, (dict, list)):
logger.warning("Expected a scalar value but got {obj}. Skipping it.", obj=obj)
continue
result.append((str(obj), {}))
return result
@component.output_types(documents=List[Document])
def run(
self,
sources: List[Union[str, Path, ByteStream]],
meta: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None,
):
"""
Converts a list of JSON files to documents.
:param sources:
A list of file paths or ByteStream objects.
: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.
If it's a list, the length of the list must match the number of sources.
If `sources` contain ByteStream objects, their `meta` will be added to the output documents.
:returns:
A dictionary with the following keys:
- `documents`: A list of created documents.
"""
documents = []
meta_list = normalize_metadata(meta=meta, sources_count=len(sources))
for source, metadata in zip(sources, meta_list):
try:
bytestream = get_bytestream_from_source(source)
except Exception as exc:
logger.warning("Could not read {source}. Skipping it. Error: {error}", source=source, error=exc)
continue
data = self._get_content_and_meta(bytestream)
for text, extra_meta in data:
merged_metadata = {**bytestream.meta, **metadata, **extra_meta}
document = Document(content=text, meta=merged_metadata)
documents.append(document)
return {"documents": documents}
|