fixed faiss integ tests (#5808)

Fixes # 5807

Realigned tests with implementation.
Also reinforced folder unicity for the test_faiss_local_save_load test
using date-time suffix

#### Before submitting

- Integration test updated
- formatting and linting ok (locally) 

#### Who can review?

Tag maintainers/contributors who might be interested:

  @hwchase17 - project lead
  VectorStores / Retrievers / Memory
  -@dev2049
This commit is contained in:
bnassivet 2023-06-07 01:07:27 -04:00 committed by GitHub
parent 92b87c2fec
commit 062c3c00a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
"""Test FAISS functionality.""" """Test FAISS functionality."""
import datetime
import math import math
import tempfile import tempfile
@ -105,10 +106,10 @@ def test_faiss_local_save_load() -> None:
"""Test end to end serialization.""" """Test end to end serialization."""
texts = ["foo", "bar", "baz"] texts = ["foo", "bar", "baz"]
docsearch = FAISS.from_texts(texts, FakeEmbeddings()) docsearch = FAISS.from_texts(texts, FakeEmbeddings())
temp_timestamp = datetime.datetime.utcnow().strftime("%Y%m%d-%H%M%S")
with tempfile.NamedTemporaryFile() as temp_file: with tempfile.TemporaryDirectory(suffix="_" + temp_timestamp + "/") as temp_folder:
docsearch.save_local(temp_file.name) docsearch.save_local(temp_folder)
new_docsearch = FAISS.load_local(temp_file.name, FakeEmbeddings()) new_docsearch = FAISS.load_local(temp_folder, FakeEmbeddings())
assert new_docsearch.index is not None assert new_docsearch.index is not None
@ -118,7 +119,7 @@ def test_faiss_similarity_search_with_relevance_scores() -> None:
docsearch = FAISS.from_texts( docsearch = FAISS.from_texts(
texts, texts,
FakeEmbeddings(), FakeEmbeddings(),
normalize_score_fn=lambda score: 1.0 - score / math.sqrt(2), relevance_score_fn=lambda score: 1.0 - score / math.sqrt(2),
) )
outputs = docsearch.similarity_search_with_relevance_scores("foo", k=1) outputs = docsearch.similarity_search_with_relevance_scores("foo", k=1)
output, score = outputs[0] output, score = outputs[0]
@ -130,11 +131,9 @@ def test_faiss_invalid_normalize_fn() -> None:
"""Test the similarity search with normalized similarities.""" """Test the similarity search with normalized similarities."""
texts = ["foo", "bar", "baz"] texts = ["foo", "bar", "baz"]
docsearch = FAISS.from_texts( docsearch = FAISS.from_texts(
texts, FakeEmbeddings(), normalize_score_fn=lambda _: 2.0 texts, FakeEmbeddings(), relevance_score_fn=lambda _: 2.0
) )
with pytest.raises( with pytest.warns(Warning, match="scores must be between"):
ValueError, match="Normalized similarity scores must be between 0 and 1"
):
docsearch.similarity_search_with_relevance_scores("foo", k=1) docsearch.similarity_search_with_relevance_scores("foo", k=1)
@ -143,4 +142,5 @@ def test_missing_normalize_score_fn() -> None:
with pytest.raises(ValueError): with pytest.raises(ValueError):
texts = ["foo", "bar", "baz"] texts = ["foo", "bar", "baz"]
faiss_instance = FAISS.from_texts(texts, FakeEmbeddings()) faiss_instance = FAISS.from_texts(texts, FakeEmbeddings())
faiss_instance.relevance_score_fn = None
faiss_instance.similarity_search_with_relevance_scores("foo", k=2) faiss_instance.similarity_search_with_relevance_scores("foo", k=2)