Pudding48 commited on
Commit
7c8630f
·
verified ·
1 Parent(s): a673c29

Update prepare_vector_dp.py

Browse files
Files changed (1) hide show
  1. prepare_vector_dp.py +44 -44
prepare_vector_dp.py CHANGED
@@ -1,45 +1,45 @@
1
- from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
2
- from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
3
- from langchain_community.vectorstores import FAISS
4
- from langchain_community.embeddings import GPT4AllEmbeddings
5
-
6
- # Khai bao bien
7
- pdf_data_path = "data"
8
- vector_dp_path = "vectorstores/db_faiss"
9
-
10
- # Ham 1. Tao ra vector DB tu 1 doan text
11
- def create_db_from_text():
12
- raw_text = "Trường Đại học Khoa học – Đại học Huế là một trong những cơ sở đào tạo và nghiên cứu hàng đầu tại khu vực miền Trung và Tây Nguyên. Được thành lập từ năm 1957, trường có bề dày truyền thống trong giảng dạy các ngành khoa học tự nhiên, xã hội và nhân văn. Với đội ngũ giảng viên giàu kinh nghiệm, cơ sở vật chất hiện đại và môi trường học tập năng động, Trường Đại học Khoa học luôn là lựa chọn uy tín của sinh viên trong và ngoài nước. Trường hiện tọa lạc tại số 77 Nguyễn Huệ, thành phố Huế – trung tâm văn hóa, giáo dục lớn của cả nước."
13
-
14
- text_splitter = CharacterTextSplitter(
15
- separator="\n",
16
- chunk_size=512,
17
- chunk_overlap=50,
18
- length_function=len
19
- )
20
-
21
- chunks = text_splitter.split_text(raw_text)
22
-
23
- # Embeding
24
- embedding_model = GPT4AllEmbeddings(model_file= "model/tinyllama-1.1b-chat-v1.0.Q8_0.gguf")
25
-
26
- # Dua vao Faiss Vector DB
27
- db = FAISS.from_texts(texts=chunks, embedding=embedding_model)
28
- db.save_local(vector_dp_path)
29
- return db
30
-
31
- def create_dp_from_files():
32
- # Khai bao loader de quet toan bo thu muc data
33
- loader = DirectoryLoader(pdf_data_path, glob="*.pdf",loader_cls=PyPDFLoader)
34
- documents = loader.load()
35
-
36
- text_splitter = CharacterTextSplitter(chunk_size = 512, chunk_overlap = 50)
37
- chunks = text_splitter.split_documents(documents)
38
-
39
- embedding_model = GPT4AllEmbeddings(model_file = "model/tinyllama-1.1b-chat-v1.0.Q8_0.gguf")
40
- dp = FAISS.from_documents(chunks, embedding_model)
41
- dp.save_local(vector_dp_path)
42
- return dp
43
-
44
- # create_db_from_text()
45
  create_dp_from_files()
 
1
+ from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
2
+ from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
3
+ from langchain_community.vectorstores import FAISS
4
+ from langchain_community.embeddings import GPT4AllEmbeddings
5
+
6
+ # Khai bao bien
7
+ pdf_data_path = "data"
8
+ vector_dp_path = "vectorstores/db_faiss"
9
+
10
+ # Ham 1. Tao ra vector DB tu 1 doan text
11
+ def create_db_from_text():
12
+ raw_text = "Trường Đại học Khoa học – Đại học Huế là một trong những cơ sở đào tạo và nghiên cứu hàng đầu tại khu vực miền Trung và Tây Nguyên. Được thành lập từ năm 1957, trường có bề dày truyền thống trong giảng dạy các ngành khoa học tự nhiên, xã hội và nhân văn. Với đội ngũ giảng viên giàu kinh nghiệm, cơ sở vật chất hiện đại và môi trường học tập năng động, Trường Đại học Khoa học luôn là lựa chọn uy tín của sinh viên trong và ngoài nước. Trường hiện tọa lạc tại số 77 Nguyễn Huệ, thành phố Huế – trung tâm văn hóa, giáo dục lớn của cả nước."
13
+
14
+ text_splitter = CharacterTextSplitter(
15
+ separator="\n",
16
+ chunk_size=512,
17
+ chunk_overlap=50,
18
+ length_function=len
19
+ )
20
+
21
+ chunks = text_splitter.split_text(raw_text)
22
+
23
+ # Embeding
24
+ embedding_model = GPT4AllEmbeddings(model_file= "tinyllama-1.1b-chat-v1.0.Q8_0.gguf")
25
+
26
+ # Dua vao Faiss Vector DB
27
+ db = FAISS.from_texts(texts=chunks, embedding=embedding_model)
28
+ db.save_local(vector_dp_path)
29
+ return db
30
+
31
+ def create_dp_from_files():
32
+ # Khai bao loader de quet toan bo thu muc data
33
+ loader = DirectoryLoader(pdf_data_path, glob="*.pdf",loader_cls=PyPDFLoader)
34
+ documents = loader.load()
35
+
36
+ text_splitter = CharacterTextSplitter(chunk_size = 512, chunk_overlap = 50)
37
+ chunks = text_splitter.split_documents(documents)
38
+
39
+ embedding_model = GPT4AllEmbeddings(model_file = "tinyllama-1.1b-chat-v1.0.Q8_0.gguf")
40
+ dp = FAISS.from_documents(chunks, embedding_model)
41
+ dp.save_local(vector_dp_path)
42
+ return dp
43
+
44
+ # create_db_from_text()
45
  create_dp_from_files()