Usage
Build and query a vector store with Vicinity
Vicinity can be used to build and query a vector store. It provides a unified interface for various approximate nearest neighbor (ANN) backends, allowing you to easily switch between them and compare their performance.
Building a Vector Store
To build a vector store with Vicinity, you can use the Vicinity.from_vectors_and_items
method. This method allows you to create a vector store from a set of vectors and their corresponding items.
For this example, we will use some dummy data, but you can replace it with your own vectors and items.
There are two important parameters here:
backend_type
: This specifies the type of backend to use. You can choose from various backends likeBackend.BASIC
(default),Backend.FAISS
, etc. Each backend has its own strengths and weaknesses. View the full list of backends in the supported backends documentation.metric
: This specifies the distance metric to use for querying. You can choose from various metrics likeMetric.COSINE
(default),Metric.EUCLIDEAN
, etc. Which metrics are supported depends on the backend you choose.
Note that most backends have their own parameters that can be configured. These can be passed as additional keyword arguments. For a full list of parameters for each backend, see the supported backends documentation.
For example, if you want to use the FAISS backend with the HNSW algorithm and Euclidean metric, you can do it like this:
Querying a Vector Store
To query a vector store, you can use the query
and query_threshold
methods. This allows you to find the nearest neighbors of a given query vector or a list of query vectors.
Both methods support both single and batch queries.
Query Top-K Nearest Neighbors
Query with a Threshold
Saving and Loading a Vector Store
You can save and load a vector store using the save
and load
methods. This allows you to persist your vector store to disk and load it later.
Pushing and Loading to/from Hugging Face Hub
You can push and load a vector store to/from the Hugging Face Hub using the push_to_hub
and from_pretrained
methods. This allows you to easily share your vector store with others.
Evaluate a Vector Store
You can evaluate a vector store using the evaluate
method. This method computes the recall and queries per second of the vector store based on a set of queries and their expected results.