-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_docs.py
50 lines (35 loc) · 1.26 KB
/
local_docs.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
import os
from PyPDF2 import PdfReader
from langchain import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
os.environ["OPENAI_API_KEY"] = "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"
def read_pdf(pdf_path):
reader = PdfReader(pdf_path)
raw_text = ''
for i, page in enumerate(reader.pages):
text = page.extract_text()
if text:
raw_text += text
text_splitter = CharacterTextSplitter(
separator = " ",
chunk_size = 2000,
chunk_overlap = 500,
length_function = len,
)
texts = text_splitter.split_text(raw_text)
return texts
fds_path = 'local_docs/wiper_manual_mechanical.pdf'
#default embedding model is text-embedding-ada-002
embeddings = OpenAIEmbeddings()
fds_retriever = None
fds_texts = read_pdf(fds_path)
# fds_texts = read_from_image_pdf(fds_path)
fds_retriever = FAISS.from_texts(fds_texts, embeddings)
fds_retriever.save_local("local_docs")
# 本地知识库2
# idl_retriever = None
# idl_texts = read_pdf(idl_path)
# if idl_enable:
# print("######## embedding idl and store into vector db ########")
# idl_retriever = FAISS.from_texts(idl_texts, embeddings).as_retriever(search_kwargs = {"k": 5})