Skip to content

Faiss Vector DB

Overview

The FaissVectorDB class is designed to manage and query vector embeddings using the FAISS library. This functionality is part of the Euler Graph Database and extends the BaseVectorStore class.

Arguments

  • dimension (int): The dimensionality of the vectors to be indexed.
  • index (faiss.IndexIDMap): The FAISS index for storing and querying vectors, initialized in the __post_init__ method.

Example Usage

Here's an example demonstrating how to use the FaissVectorDB class:

from euler.vector_store.faiss_vector_db import FaissVectorDB
from euler.graph_api import KnowledgeGraphAPI

Initialize the FAISS Vector Database

vector_db = FaissVectorDB(dimension=64)

Generate and Add Embeddings

api = KnowledgeGraphAPI()
api.graph.generate_embeddings(network_graph, method="simple", dimensions=64)
simple_embedding = api.graph.get_embedding('1')
print("Simple embedding for node '1':", simple_embedding)

embeddings = {node_id: api.graph.get_embedding(node_id) for node_id in api.graph.nodes}
vector_db.add_vectors(list(embeddings.values()), list(map(int, embeddings.keys())))

Generate Node2Vec Embeddings

api.graph.generate_embeddings(network_graph, method="node2vec", dimensions=64, walk_length=30, num_walks=200, workers=4)
node2vec_embedding = api.graph.get_embedding('1')
print("Node2Vec embedding for node '1':", node2vec_embedding)

Save and Load the Index

# Save
api.save_graph("graph.json")
vector_db.save("faiss_index.bin")

# Load
api.load_graph("graph.json")
vector_db.load("faiss_index.bin")

Query the FAISS Index

# FAISS index for nearest neighbors of node '1'
query_results = vector_db.query_vectors(api.graph.get_embedding('1'), k=4)
print("Query results after loading:", query_results)

api.visualize_graph(file_path='graph.png')

Functions

__post_init__

The __post_init__ method initializes the FAISS index using IndexFlatL2 and wraps it in IndexIDMap.

add_vectors

The add_vectors method adds vectors to the FAISS index. It takes a list of vectors and a corresponding list of IDs. The vectors are converted to a NumPy array before being added to the index.

query_vectors

The query_vectors method queries the FAISS index for the k nearest neighbors of a given vector. It returns a list of IDs of the k nearest neighbors.

save

The save method saves the FAISS index to a file. It takes the path to the file as an argument.

load

The load method loads the FAISS index from a file. It takes the path to the file as an argument.

This documentation provides an overview, argument details, and a complete example of how to use the FaissVectorDB class for managing and querying vector embeddings using the FAISS library.