-
Notifications
You must be signed in to change notification settings - Fork 0
/
docLoader.py
70 lines (67 loc) · 2.3 KB
/
docLoader.py
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
from langchain_community.document_loaders import (
WebBaseLoader,
TextLoader,
PyPDFLoader,
CSVLoader,
Docx2txtLoader,
YoutubeLoader
)
from pathlib import Path
from langchain_text_splitters import RecursiveCharacterTextSplitter
async def load_file(filename:str,filepath:Path,metadata:dict, file_id:str = None):
file_ext = filename.split(".")[-1].lower()
docs = []
splitter = RecursiveCharacterTextSplitter(
chunk_size=1024,
chunk_overlap=0,
length_function=len,
is_separator_regex=False
)
match file_ext:
case "txt":
loader = TextLoader(filepath, autodetect_encoding=True)
wholeDoc = loader.load()
docs = splitter.split_documents(wholeDoc)
case "csv":
loader = CSVLoader(filepath)
docs = loader.load()
case "pdf":
loader = PyPDFLoader(file_path=filepath)
wholeDoc = loader.load()
docs = splitter.split_documents(wholeDoc)
case "docx":
loader = Docx2txtLoader(filepath)
wholeDoc = loader.load()
docs = splitter.split_documents(wholeDoc)
case _:
raise TypeError(f'The file extension {file_ext} is not supported')
for doc in docs:
doc.metadata = doc.metadata | metadata
if file_id:
doc.metadata["file_id"] = file_id
return docs
async def load_link(url:str, metadata:dict = None, file_id:str = None):
website = url.split(".")[1].lower()
docs = []
splitter = RecursiveCharacterTextSplitter(
chunk_size=1024,
chunk_overlap=0,
length_function=len,
is_separator_regex=False
)
match website:
case 'youtube':
loader = YoutubeLoader.from_youtube_url(url, add_video_info=False)
wholeDoc = loader.load()
docs = splitter.split_documents(wholeDoc)
case _: #assume html webpage
loader = WebBaseLoader(url)
wholeDoc = loader.load()
docs = splitter.split_documents(wholeDoc)
if metadata:
for doc in docs:
doc.metadata = doc.metadata | metadata
if file_id:
for doc in docs:
doc.metadata["file_id"] = file_id
return docs