Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove similarity_search_by_vector #141

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading