-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjsondataloader.py
More file actions
58 lines (43 loc) · 1.85 KB
/
Copy pathjsondataloader.py
File metadata and controls
58 lines (43 loc) · 1.85 KB
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
import json
from pathlib import Path
from typing import List, Optional, Union
import uuid
from langchain.docstore.document import Document
from langchain.document_loaders.base import BaseLoader
class JSONDataLoader(BaseLoader):
def load_ship(self,data:list) -> List[Document]:
"""Load and return documents from the JSON file."""
docs:List[Document]=[]
#iterate through ship data and create a Document for each ship
for element in data:
name = element['name']
text = element['description'] + ' '.join(element['amenities'])
description = element['description']
amenities = element['amenities']
metadata = dict(
shipid = element['shipid'],
name = name,
description = description,
amenities = amenities
)
docs.append(Document(page_content=text, metadata=metadata))
return docs
def load_destination(self,data:list) -> List[Document]:
"""Load and return documents from the destination JSON file."""
docs:List[Document]=[]
#iterate through destination data and create a Document for each destination
for element in data:
name = element['name']
text = element['description'] + ' '.join(element['activities'])
location = element['location']
description = element['description']
activities = element['activities']
metadata = dict(
destinationid = element['destinationid'],
name = name,
location = location,
description = description,
activities = activities
)
docs.append(Document(page_content=text, metadata=metadata))
return docs