-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
65 lines (52 loc) · 1.96 KB
/
Copy pathvector_store.py
File metadata and controls
65 lines (52 loc) · 1.96 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
59
60
61
62
63
64
65
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import os
import pickle
'''model = SentenceTransformer('all-MiniLM-L6-v2')
index_path = 'data/faiss.index'
meta_path = 'data/docs.pkl'
def create_faiss_index(docs):
embeddings = model.encode(docs)
dimension = embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(np.array(embeddings))
with open(meta_path, 'wb') as f:
pickle.dump(docs, f)
faiss.write_index(index, index_path)
def load_faiss_index():
if not os.path.exists(index_path) or not os.path.exists(meta_path):
raise FileNotFoundError("Index or document metadata not found.")
index = faiss.read_index(index_path)
with open(meta_path, 'rb') as f:
docs = pickle.load(f)
return index, docs'''
# Update append text to existing text
EMBED_MODEL_NAME = "all-MiniLM-L6-v2"
model = SentenceTransformer(EMBED_MODEL_NAME)
INDEX_PATH = "data/indexes/faiss.index"
META_PATH = "data/indexes/docs_meta.pkl"
def create_faiss_index(docs_texts):
os.makedirs(os.path.dirname(INDEX_PATH), exist_ok=True)
embeddings = model.encode(docs_texts, show_progress_bar=True, convert_to_numpy=True)
dim = embeddings.shape[1]
index = faiss.IndexFlatL2(dim)
index.add(embeddings)
faiss.write_index(index, INDEX_PATH)
with open(META_PATH, "wb") as f:
pickle.dump(docs_texts, f)
return index
def load_faiss_index():
if not os.path.exists(INDEX_PATH) or not os.path.exists(META_PATH):
raise FileNotFoundError("Index or metadata not found. Build index first.")
index = faiss.read_index(INDEX_PATH)
with open(META_PATH, "rb") as f:
docs = pickle.load(f)
return index, docs
def add_to_index(new_texts):
if os.path.exists(INDEX_PATH) and os.path.exists(META_PATH):
index, docs = load_faiss_index()
docs.extend(new_texts)
return create_faiss_index(docs)
else:
return create_faiss_index(new_texts)