Spaces:
No application file
No application file
Delete faiss_utils.py
Browse files- faiss_utils.py +0 -45
faiss_utils.py
DELETED
|
@@ -1,45 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from langchain_community.vectorstores import FAISS
|
| 3 |
-
from langchain.embeddings import OpenAIEmbeddings
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
def embed(input_strings):
|
| 7 |
-
vectorstore = FAISS.from_texts(texts=input_strings, embedding=OpenAIEmbeddings())
|
| 8 |
-
return vectorstore
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Function to save a FAISS vectorstore to a specified path
|
| 12 |
-
def save_local(vectorstore, path="safe/"):
|
| 13 |
-
if not os.path.exists(path):
|
| 14 |
-
os.makedirs(path)
|
| 15 |
-
file_path = os.path.join(path, "faiss_index.index")
|
| 16 |
-
vectorstore.save_local(file_path)
|
| 17 |
-
print(f"FAISS vectorstore saved to {file_path}")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Function to load a FAISS vectorstore from a specified path
|
| 21 |
-
def load_vectorstore(path):
|
| 22 |
-
embeddings = OpenAIEmbeddings() # Needed to initialize the FAISS properly
|
| 23 |
-
vectorstore = FAISS.load_local(path, embeddings, allow_dangerous_deserialization=True)
|
| 24 |
-
print(f"FAISS vectorstore loaded from {path}")
|
| 25 |
-
return vectorstore
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Example usage
|
| 29 |
-
if __name__ == "__main__":
|
| 30 |
-
# Embed a few words
|
| 31 |
-
words = ["hello", "world", "sample", "text"]
|
| 32 |
-
faiss_db1 = embed(words)
|
| 33 |
-
|
| 34 |
-
# Save the vectorstore
|
| 35 |
-
save_local(faiss_db1)
|
| 36 |
-
|
| 37 |
-
# Load the vectorstore
|
| 38 |
-
loaded_db1 = load_vectorstore("safe/faiss_index.index")
|
| 39 |
-
|
| 40 |
-
# Embed another set of words and create a second vectorstore
|
| 41 |
-
more_words = ["FAISS", "database", "information", "retrieval"]
|
| 42 |
-
faiss_db2 = embed(more_words)
|
| 43 |
-
|
| 44 |
-
loaded_db1.merge_from(faiss_db2)
|
| 45 |
-
print("Merged vectorstore with other vectorstore containing total vectors:", loaded_db1.index.ntotal)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|