FaissVectorStore saving/loading changes? My old code for generating/saving and loading an index no longer works.
def construct_index_from_nodes(nodes, persist_dir):
load_dotenv()
openai_api_key = os.getenv('OPENAI_API_KEY')
# Generate Node Embeddings
embed_model = OpenAIEmbedding(api_key=openai_api_key)
for node in tqdm(nodes, desc="Generating Node Embeddings"):
node_embedding = embed_model.get_text_embedding(
node.get_content(metadata_mode="all")
)
node.embedding = node_embedding
# Build the Index from the Nodes
print("Building Index from Nodes...")
d = 1536
faiss_index = faiss.IndexFlatL2(d)
vector_store = FaissVectorStore(faiss_index=faiss_index)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex(
nodes=nodes,
storage_context=storage_context,
)
# save the resulting index to disk so that we can use it later
print("Index created. Saving to disk...")
index.storage_context.persist(persist_dir=persist_dir)
print("Complete.")
def test_index(persist_dir, test_query, similarity_top_k=3):
vector_store = FaissVectorStore.from_persist_dir(persist_dir)
storage_context = StorageContext.from_defaults(
vector_store=vector_store, persist_dir=persist_dir)
index = load_index_from_storage(storage_context=storage_context)
retriever = index.as_retriever(similarity_top_k=similarity_top_k)
nodes = retriever.retrieve(test_query)
for i, node in enumerate(nodes):
print("NODE", i, "[", round(node.get_score(), 2), "]", ":", node.dict()["node"]["text"], "\n\n")
Used to produce:
[docstore.json, graph_store.json, index_store.json, vector_store.json]
Now produces:
[default__vector_store.json, docstore.json, graph_store.json, image__vector_store.json, index_store.json]