Skip to content

Commit

Permalink
Throw error when vector and query are None (#142)
Browse files Browse the repository at this point in the history
These are okay:

```python
weaviate_vector_store._perform_search(query="hello", vector=None, k=5)

    weaviate_vector_store._perform_search(query=None, vector=[1, 2, 3], k=5)
```

These are not okay:

```python
weaviate_vector_store._perform_search(query=None, k=5)
weaviate_vector_store._perform_search(query=None, vector=None, k=5)

```

Signed-off-by: hsm207 <[email protected]>

---------

Signed-off-by: hsm207 <[email protected]>
  • Loading branch information
hsm207 authored Mar 25, 2024
1 parent 87a9933 commit 52474ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 10 additions & 5 deletions langchain_weaviate/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,25 @@ def _perform_search(
if self._embedding is None:
raise ValueError("_embedding cannot be None for similarity_search")

if "return_metadata" in kwargs and "score" not in kwargs["return_metadata"]:
kwargs["return_metadata"].append("score")
else:
if "return_metadata" not in kwargs:
kwargs["return_metadata"] = ["score"]
elif "score" not in kwargs["return_metadata"]:
kwargs["return_metadata"].append("score")

if (
"return_properties" in kwargs
and self._text_key not in kwargs["return_properties"]
):
kwargs["return_properties"].append(self._text_key)

# workaround to handle test_max_marginal_relevance_search
vector = kwargs.pop("vector", None)
if vector is None and query is not None:
if vector is None and query is None:
# raise an error because weaviate will do a fetch object query
# if both query and vector are None
raise ValueError("Either query or vector must be provided.")

# workaround to handle test_max_marginal_relevance_search
if vector is None:
vector = self._embedding.embed_query(query)

return_uuids = kwargs.pop("return_uuids", False)
Expand Down
22 changes: 22 additions & 0 deletions tests/integration_tests/test_vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,25 @@ def run_similarity_test(search_method):
run_similarity_test("similarity_search")

run_similarity_test("similarity_search_with_score")


def test_invalid_search_param(weaviate_client, embedding_openai):
index_name = f"TestIndex_{uuid.uuid4().hex}"
text_key = "page"
weaviate_vector_store = WeaviateVectorStore(
weaviate_client, index_name, text_key, embedding_openai
)

with pytest.raises(ValueError) as excinfo:
weaviate_vector_store._perform_search(query=None, k=5)

assert str(excinfo.value) == "Either query or vector must be provided."

with pytest.raises(ValueError) as excinfo:
weaviate_vector_store._perform_search(query=None, vector=None, k=5)

assert str(excinfo.value) == "Either query or vector must be provided."

weaviate_vector_store._perform_search(query="hello", vector=None, k=5)

weaviate_vector_store._perform_search(query=None, vector=[1, 2, 3], k=5)

0 comments on commit 52474ed

Please sign in to comment.