Embedchain json loader update (#876)
Co-authored-by: Deven Patel <deven298@yahoo.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
from embedchain.chunkers.base_chunker import BaseChunker
|
from embedchain.chunkers.base_chunker import BaseChunker
|
||||||
from embedchain.config import AddConfig
|
from embedchain.config import AddConfig
|
||||||
from embedchain.config.add_config import ChunkerConfig, LoaderConfig
|
from embedchain.config.add_config import ChunkerConfig, LoaderConfig
|
||||||
|
|||||||
@@ -1,24 +1,37 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
|
import json
|
||||||
from langchain.document_loaders.json_loader import \
|
import os
|
||||||
JSONLoader as LangchainJSONLoader
|
|
||||||
|
|
||||||
from embedchain.loaders.base_loader import BaseLoader
|
from embedchain.loaders.base_loader import BaseLoader
|
||||||
|
|
||||||
langchain_json_jq_schema = 'to_entries | map("\(.key): \(.value|tostring)") | .[]'
|
|
||||||
|
|
||||||
|
|
||||||
class JSONLoader(BaseLoader):
|
class JSONLoader(BaseLoader):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_data(content):
|
def load_data(content):
|
||||||
"""Load a json file. Each data point is a key value pair."""
|
"""Load a json file. Each data point is a key value pair."""
|
||||||
|
try:
|
||||||
|
from llama_hub.jsondata.base import \
|
||||||
|
JSONDataReader as LLHBUBJSONLoader
|
||||||
|
except ImportError:
|
||||||
|
raise Exception(
|
||||||
|
f"Couldn't import the required packages to load {content}, \
|
||||||
|
Do `pip install --upgrade 'embedchain[json]`"
|
||||||
|
)
|
||||||
|
|
||||||
|
loader = LLHBUBJSONLoader()
|
||||||
|
|
||||||
|
if not isinstance(content, str) and not os.path.isfile(content):
|
||||||
|
print(f"Invaid content input. Provide the correct path to the json file saved locally in {content}")
|
||||||
|
|
||||||
data = []
|
data = []
|
||||||
data_content = []
|
data_content = []
|
||||||
loader = LangchainJSONLoader(content, text_content=False, jq_schema=langchain_json_jq_schema)
|
|
||||||
docs = loader.load()
|
with open(content, "r") as json_file:
|
||||||
for doc in docs:
|
json_data = json.load(json_file)
|
||||||
meta_data = doc.metadata
|
docs = loader.load_data(json_data)
|
||||||
data.append({"content": doc.page_content, "meta_data": {"url": content, "row": meta_data["seq_num"]}})
|
for doc in docs:
|
||||||
data_content.append(doc.page_content)
|
doc_content = doc.text
|
||||||
|
data.append({"content": doc_content, "meta_data": {"url": content}})
|
||||||
|
data_content.append(doc_content)
|
||||||
doc_id = hashlib.sha256((content + ", ".join(data_content)).encode()).hexdigest()
|
doc_id = hashlib.sha256((content + ", ".join(data_content)).encode()).hexdigest()
|
||||||
return {"doc_id": doc_id, "data": data}
|
return {"doc_id": doc_id, "data": data}
|
||||||
|
|||||||
@@ -1,32 +1,34 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
from langchain.docstore.document import Document
|
|
||||||
from langchain.document_loaders.json_loader import \
|
|
||||||
JSONLoader as LangchainJSONLoader
|
|
||||||
|
|
||||||
from embedchain.loaders.json import JSONLoader
|
from embedchain.loaders.json import JSONLoader
|
||||||
|
|
||||||
|
|
||||||
def test_load_data():
|
def test_load_data(mocker):
|
||||||
mock_document = [
|
content = "temp.json"
|
||||||
Document(page_content="content1", metadata={"seq_num": 1}),
|
|
||||||
Document(page_content="content2", metadata={"seq_num": 2}),
|
mock_document = {
|
||||||
|
"doc_id": hashlib.sha256((content + ", ".join(["content1", "content2"])).encode()).hexdigest(),
|
||||||
|
"data": [
|
||||||
|
{"content": "content1", "meta_data": {"url": content}},
|
||||||
|
{"content": "content2", "meta_data": {"url": content}},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
mocker.patch("embedchain.loaders.json.JSONLoader.load_data", return_value=mock_document)
|
||||||
|
|
||||||
|
json_loader = JSONLoader()
|
||||||
|
|
||||||
|
result = json_loader.load_data(content)
|
||||||
|
|
||||||
|
assert "doc_id" in result
|
||||||
|
assert "data" in result
|
||||||
|
|
||||||
|
expected_data = [
|
||||||
|
{"content": "content1", "meta_data": {"url": content}},
|
||||||
|
{"content": "content2", "meta_data": {"url": content}},
|
||||||
]
|
]
|
||||||
with patch.object(LangchainJSONLoader, "load", return_value=mock_document):
|
|
||||||
content = "temp.json"
|
|
||||||
|
|
||||||
result = JSONLoader.load_data(content)
|
assert result["data"] == expected_data
|
||||||
|
|
||||||
assert "doc_id" in result
|
expected_doc_id = hashlib.sha256((content + ", ".join(["content1", "content2"])).encode()).hexdigest()
|
||||||
assert "data" in result
|
assert result["doc_id"] == expected_doc_id
|
||||||
|
|
||||||
expected_data = [
|
|
||||||
{"content": "content1", "meta_data": {"url": content, "row": 1}},
|
|
||||||
{"content": "content2", "meta_data": {"url": content, "row": 2}},
|
|
||||||
]
|
|
||||||
|
|
||||||
assert result["data"] == expected_data
|
|
||||||
|
|
||||||
expected_doc_id = hashlib.sha256((content + ", ".join(["content1", "content2"])).encode()).hexdigest()
|
|
||||||
assert result["doc_id"] == expected_doc_id
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
from embedchain.telemetry.posthog import AnonymousTelemetry
|
from embedchain.telemetry.posthog import AnonymousTelemetry
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user