metadata=dict(page=1)),    Document(page_content="bar", metadata=dict(page=1)),    Document(page_content="foo", metadata=dict(page=2)),    Document(page_content="barbar", metadata=dict(page=2)),    Document(page_content="foo", metadata=dict(page=3)),    Document(page_content="bar burr", metadata=dict(page=3)),    Document(page_content="foo", metadata=dict(page=4)),    Document(page_content="bar bruh", metadata=dict(page=4)),]db = FAISS.from_documents(list_of_documents, embeddings)results_with_scores = db.similarity_search_with_score("foo")for doc, score in results_with_scores:    print(f"Content: {doc.page_content}, Metadata: {doc.metadata}, Score: {score}")    Content: foo, Metadata: {'page': 1}, Score: 5.159960813797904e-15    Content: foo, Metadata: {'page': 2}, Score: 5.159960813797904e-15    Content: foo, Metadata: {'page': 3}, Score: 5.159960813797904e-15    Content: foo, Metadata: {'page': 4}, Score: 5.159960813797904e-15Now we make the same query call but we filter for only page = 1 results_with_scores = db.similarity_search_with_score("foo", filter=dict(page=1))for doc, score in results_with_scores:    print(f"Content: {doc.page_content}, Metadata: {doc.metadata}, Score: {score}")    Content: foo, Metadata: {'page': 1}, Score: 5.159960813797904e-15    Content: bar, Metadata: {'page': 1}, Score: 0.3131446838378906Same thing can be done with the max_marginal_relevance_search as well.results = db.max_marginal_relevance_search("foo", filter=dict(page=1))for doc in results:    print(f"Content: {doc.page_content}, Metadata: {doc.metadata}")    Content: foo, Metadata: {'page': 1}    Content: bar, Metadata: {'page': 1}Here is an example of how to set fetch_k parameter when calling similarity_search. Usually you would want the fetch_k parameter >> k parameter. This is because the fetch_k parameter is the number of documents that will be fetched before filtering. If you set fetch_k to a low number, you might not get enough documents to filter from.results =
Facebook AI Similarity Search (Faiss) is a library for efficient similarity search and clustering of dense vectors. It contains algorithms that search in sets of vectors of any size, up to ones that possibly do not fit in RAM. It also contains supporting code for evaluation and parameter tuning.
Facebook AI Similarity Search (Faiss) is a library for efficient similarity search and clustering of dense vectors. It contains algorithms that search in sets of vectors of any size, up to ones that possibly do not fit in RAM. It also contains supporting code for evaluation and parameter tuning. ->: metadata=dict(page=1)),    Document(page_content="bar", metadata=dict(page=1)),    Document(page_content="foo", metadata=dict(page=2)),    Document(page_content="barbar", metadata=dict(page=2)),    Document(page_content="foo", metadata=dict(page=3)),    Document(page_content="bar burr", metadata=dict(page=3)),    Document(page_content="foo", metadata=dict(page=4)),    Document(page_content="bar bruh", metadata=dict(page=4)),]db = FAISS.from_documents(list_of_documents, embeddings)results_with_scores = db.similarity_search_with_score("foo")for doc, score in results_with_scores:    print(f"Content: {doc.page_content}, Metadata: {doc.metadata}, Score: {score}")    Content: foo, Metadata: {'page': 1}, Score: 5.159960813797904e-15    Content: foo, Metadata: {'page': 2}, Score: 5.159960813797904e-15    Content: foo, Metadata: {'page': 3}, Score: 5.159960813797904e-15    Content: foo, Metadata: {'page': 4}, Score: 5.159960813797904e-15Now we make the same query call but we filter for only page = 1 results_with_scores = db.similarity_search_with_score("foo", filter=dict(page=1))for doc, score in results_with_scores:    print(f"Content: {doc.page_content}, Metadata: {doc.metadata}, Score: {score}")    Content: foo, Metadata: {'page': 1}, Score: 5.159960813797904e-15    Content: bar, Metadata: {'page': 1}, Score: 0.3131446838378906Same thing can be done with the max_marginal_relevance_search as well.results = db.max_marginal_relevance_search("foo", filter=dict(page=1))for doc in results:    print(f"Content: {doc.page_content}, Metadata: {doc.metadata}")    Content: foo, Metadata: {'page': 1}    Content: bar, Metadata: {'page': 1}Here is an example of how to set fetch_k parameter when calling similarity_search. Usually you would want the fetch_k parameter >> k parameter. This is because the fetch_k parameter is the number of documents that will be fetched before filtering. If you set fetch_k to a low number, you might not get enough documents to filter from.results =