Skip to content

Commit

Permalink
remove similarity_search_by_vector (#141)
Browse files Browse the repository at this point in the history
Remove `similarity_search_by_vector` since the same function can be done
by hybrid search

Fixes:

* #132
* #131

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

---------

Signed-off-by: hsm207 <[email protected]>
  • Loading branch information
hsm207 authored Mar 25, 2024
1 parent e409654 commit 87a9933
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
37 changes: 9 additions & 28 deletions langchain_weaviate/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Dict,
Iterable,
List,
Literal,
Optional,
Tuple,
Union,
Expand Down Expand Up @@ -205,7 +204,6 @@ def _perform_search(
query: str,
k: int,
return_score=False,
search_method: Literal["hybrid", "near_vector"] = "hybrid",
tenant: Optional[str] = None,
**kwargs: Any,
) -> List[Union[Document, Tuple[Document, float]]]:
Expand All @@ -217,8 +215,6 @@ def _perform_search(
k (int): The number of results to return.
return_score (bool, optional): Whether to return the score along with the
document. Defaults to False.
search_method (Literal['hybrid', 'near_vector'], optional): The search method
to use. Can be 'hybrid' or 'near_vector'. Defaults to 'hybrid'.
tenant (Optional[str], optional): The tenant name. Defaults to None.
**kwargs: Additional parameters to pass to the search method. These parameters
will be directly passed to the underlying Weaviate client's search method.
Expand All @@ -245,19 +241,18 @@ def _perform_search(
):
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:
vector = self._embedding.embed_query(query)

return_uuids = kwargs.pop("return_uuids", False)

with self._tenant_context(tenant) as collection:
try:
if search_method == "hybrid":
embedding = self._embedding.embed_query(query)
result = collection.query.hybrid(
query=query, vector=embedding, limit=k, **kwargs
)
elif search_method == "near_vector":
result = collection.query.near_vector(limit=k, **kwargs)
else:
raise ValueError(f"Invalid search method: {search_method}")
result = collection.query.hybrid(
query=query, vector=vector, limit=k, **kwargs
)
except weaviate.exceptions.WeaviateQueryException as e:
raise ValueError(f"Error during query: {e}")

Expand Down Expand Up @@ -302,19 +297,6 @@ def similarity_search(
result = self._perform_search(query, k, **kwargs)
return result

def similarity_search_by_vector(
self, embedding: List[float], k: int = 4, **kwargs: Any
) -> List[Document]:
"""Look up similar documents by embedding vector in Weaviate."""

return self._perform_search(
query=None,
k=k,
near_vector=embedding,
search_method="near_vector",
**kwargs,
)

def max_marginal_relevance_search(
self,
query: str,
Expand Down Expand Up @@ -381,8 +363,7 @@ def max_marginal_relevance_search_by_vector(
query=None,
k=fetch_k,
include_vector=True,
near_vector=embedding,
search_method="near_vector",
vector=embedding,
**kwargs,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/integration_tests/test_vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_add_texts_with_given_embedding(
)

docsearch.add_texts(["foo"])
output = docsearch.similarity_search_by_vector(embedding.embed_query("foo"), k=2)
output = docsearch.similarity_search("foo", alpha=1, k=2)
assert output == [
Document(page_content="foo"),
Document(page_content="foo"),
Expand All @@ -309,7 +309,7 @@ def test_add_texts_with_given_uuids(

# Weaviate replaces the object if the UUID already exists
docsearch.add_texts(["foo"], uuids=[uuids[0]])
output = docsearch.similarity_search_by_vector(embedding.embed_query("foo"), k=2)
output = docsearch.similarity_search("foo", alpha=1, k=2)
assert output[0] == Document(page_content="foo")
assert output[1] != Document(page_content="foo")

Expand Down

0 comments on commit 87a9933

Please sign in to comment.