faiss_retriever#

Semantic search/embedding-based retriever using FAISS.

Classes

FAISSRetriever([embedder, top_k, ...])

Semantic search/embedding-based retriever using FAISS.

class FAISSRetriever(embedder: Embedder | None = None, top_k: int = 5, dimensions: int | None = None, documents: Any | None = None, document_map_func: Callable[[Any], List[float] | ndarray] | None = None, metric: Literal['cosine', 'euclidean', 'prob'] = 'prob')[source]#

Bases: Retriever[List[float] | ndarray, str | List[float] | List[List[float]] | ndarray]

Semantic search/embedding-based retriever using FAISS.

To use the retriever, you can either pass the index embeddings from the __init__() or use the build_index_from_documents() method.

Parameters:
  • embedder (Embedder, optimal) – The embedder component to use for converting the queries in string format to embeddings. Ensure the vectorizer is exactly the same as the one used to the embeddings in the index.

  • top_k (int, optional) – Number of chunks to retrieve. Defaults to 5.

  • dimensions (Optional[int], optional) – Dimension of the embeddings. Defaults to None. It can automatically infer the dimensions from the first chunk.

  • documents (Optional[FAISSRetrieverDocumentType], optional) – List of embeddings. Format can be List[List[float]] or List[np.ndarray]. Defaults to None.

  • metric (Literal["cosine", "euclidean", "prob"], optional) – The metric to use for the retrieval. Defaults to “prob” which converts cosine similarity to probability.

How FAISS works:

The retriever uses in-memory Faiss index to retrieve the top k chunks d: dimension of the vectors xb: number of vectors to put in the index xq: number of queries The data type dtype must be float32.

Note: When the num of chunks are less than top_k, the last columns will be -1

Other index options: - faiss.IndexFlatL2: L2 or Euclidean distance, [-inf, inf] - faiss.IndexFlatIP: Inner product of embeddings (inner product of normalized vectors will be cosine similarity, [-1, 1])

We choose cosine similarity and convert it to range [0, 1] by adding 1 and dividing by 2 to simulate probability in [0, 1]

Install FAISS:

As FAISS is optional package, you can install it with pip for cpu version: `bash pip install faiss-cpu ` For GPU version: You might have to use conda to install faiss-gpu:https://github.com/facebookresearch/faiss/wiki/Installing-Faiss

References: - FAISS: facebookresearch/faiss

reset_index()[source]#

Initialize/reset any attributes/states for the index.

build_index_from_documents(documents: Sequence[Any], document_map_func: Callable[[Any], List[float] | ndarray] | None = None)[source]#

Build index from embeddings.

Parameters:

documents – List of embeddings. Format can be List[List[float]] or List[np.ndarray]

If you are using Document format, pass them as [doc.vector for doc in documents]

retrieve_embedding_queries(input: Sequence[List[float] | List[List[float]] | ndarray], top_k: int | None = None) List[RetrieverOutput][source]#
retrieve_string_queries(input: str | List[str], top_k: int | None = None) List[RetrieverOutput][source]#

Retrieve the top k chunks given the query or queries in string format.

Parameters:
  • input – The query or list of queries in string format. Note: ensure the maximum number of queries fits into the embedder.

  • top_k – The number of chunks to retrieve. When top_k is not provided, it will use the default top_k set during initialization.

When top_k is not provided, it will use the default top_k set during initialization.

call(input: Sequence[List[float] | List[List[float]] | ndarray], top_k: int | None = None) List[RetrieverOutput][source]#
call(input: Sequence[str], top_k: int | None = None) List[RetrieverOutput]

Retrieve the top k chunks given the query or queries in embedding or string format.