@ -92,8 +92,8 @@ class FAISS(VectorStore):
self . index_to_docstore_id . update ( index_to_id )
return [ _id for _ , _id , _ in full_info ]
def similarity_search_with_score (
self , query: str , k : int = 4
def similarity_search_with_score _by_vector (
self , embedding: List [ float ] , k : int = 4
) - > List [ Tuple [ Document , float ] ] :
""" Return docs most similar to query.
@ -104,7 +104,6 @@ class FAISS(VectorStore):
Returns :
List of Documents most similar to the query and score for each
"""
embedding = self . embedding_function ( query )
scores , indices = self . index . search ( np . array ( [ embedding ] , dtype = np . float32 ) , k )
docs = [ ]
for j , i in enumerate ( indices [ 0 ] ) :
@ -118,6 +117,37 @@ class FAISS(VectorStore):
docs . append ( ( doc , scores [ 0 ] [ j ] ) )
return docs
def similarity_search_with_score (
self , query : str , k : int = 4
) - > List [ Tuple [ Document , float ] ] :
""" Return docs most similar to query.
Args :
query : Text to look up documents similar to .
k : Number of Documents to return . Defaults to 4.
Returns :
List of Documents most similar to the query and score for each
"""
embedding = self . embedding_function ( query )
docs = self . similarity_search_with_score_by_vector ( embedding , k )
return docs
def similarity_search_by_vector (
self , embedding : List [ float ] , k : int = 4 , * * kwargs : Any
) - > List [ Document ] :
""" Return docs most similar to embedding vector.
Args :
embedding : Embedding to look up documents similar to .
k : Number of Documents to return . Defaults to 4.
Returns :
List of Documents most similar to the embedding .
"""
docs_and_scores = self . similarity_search_with_score_by_vector ( embedding , k )
return [ doc for doc , _ in docs_and_scores ]
def similarity_search (
self , query : str , k : int = 4 , * * kwargs : Any
) - > List [ Document ] :
@ -133,8 +163,8 @@ class FAISS(VectorStore):
docs_and_scores = self . similarity_search_with_score ( query , k )
return [ doc for doc , _ in docs_and_scores ]
def max_marginal_relevance_search (
self , query: str , k : int = 4 , fetch_k : int = 20
def max_marginal_relevance_search _by_vector (
self , embedding: List [ float ] , k : int = 4 , fetch_k : int = 20
) - > List [ Document ] :
""" Return docs selected using the maximal marginal relevance.
@ -142,18 +172,19 @@ class FAISS(VectorStore):
among selected documents .
Args :
query: Text to look up documents similar to .
embedding: Embedding to look up documents similar to .
k : Number of Documents to return . Defaults to 4.
fetch_k : Number of Documents to fetch to pass to MMR algorithm .
Returns :
List of Documents selected by maximal marginal relevance .
"""
embedding = self . embedding_function ( query )
_ , indices = self . index . search ( np . array ( [ embedding ] , dtype = np . float32 ) , fetch_k )
# -1 happens when not enough docs are returned.
embeddings = [ self . index . reconstruct ( int ( i ) ) for i in indices [ 0 ] if i != - 1 ]
mmr_selected = maximal_marginal_relevance ( embedding , embeddings , k = k )
mmr_selected = maximal_marginal_relevance (
np . array ( [ embedding ] , dtype = np . float32 ) , embeddings , k = k
)
selected_indices = [ indices [ 0 ] [ i ] for i in mmr_selected ]
docs = [ ]
for i in selected_indices :
@ -164,6 +195,26 @@ class FAISS(VectorStore):
docs . append ( doc )
return docs
def max_marginal_relevance_search (
self , query : str , k : int = 4 , fetch_k : int = 20
) - > List [ Document ] :
""" Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents .
Args :
query : Text to look up documents similar to .
k : Number of Documents to return . Defaults to 4.
fetch_k : Number of Documents to fetch to pass to MMR algorithm .
Returns :
List of Documents selected by maximal marginal relevance .
"""
embedding = self . embedding_function ( query )
docs = self . max_marginal_relevance_search_by_vector ( embedding , k , fetch_k )
return docs
@classmethod
def from_texts (
cls ,