-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
49 lines (32 loc) · 1.11 KB
/
vector_store.py
File metadata and controls
49 lines (32 loc) · 1.11 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
import os
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from dotenv import load_dotenv
load_dotenv()
VECTOR_PATH = "faiss_index"
def create_vectorstore():
docs = []
folder = "documents"
for file in os.listdir(folder):
loader = TextLoader(os.path.join(folder, file))
docs.extend(loader.load())
splitter = RecursiveCharacterTextSplitter(
chunk_size=800,
chunk_overlap=100
)
splits = splitter.split_documents(docs)
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(splits, embeddings)
vectorstore.save_local(VECTOR_PATH)
return vectorstore
def load_vectorstore():
embeddings = OpenAIEmbeddings()
if os.path.exists(VECTOR_PATH):
return FAISS.load_local(
VECTOR_PATH,
embeddings,
allow_dangerous_deserialization=True
)
return create_vectorstore()