Skip to content

Commit

Permalink
community[patch]: implement qdrant _aembed_query and use it in other …
Browse files Browse the repository at this point in the history
…async funcs (langchain-ai#19155)

`amax_marginal_relevance_search ` and `asimilarity_search_with_score `
should use an async version of `_embed_query `.
  • Loading branch information
kaijietti authored and Dave Bechberger committed Mar 29, 2024
1 parent ec239ed commit 81119b0
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions libs/community/langchain_community/vectorstores/qdrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,9 @@ async def asimilarity_search_with_score(
Returns:
List of documents most similar to the query text and distance for each.
"""
query_embedding = await self._aembed_query(query)
return await self.asimilarity_search_with_score_by_vector(
self._embed_query(query),
query_embedding,
k,
filter=filter,
search_params=search_params,
Expand Down Expand Up @@ -841,7 +842,7 @@ async def amax_marginal_relevance_search(
Returns:
List of Documents selected by maximal marginal relevance.
"""
query_embedding = self._embed_query(query)
query_embedding = await self._aembed_query(query)
return await self.amax_marginal_relevance_search_by_vector(
query_embedding,
k=k,
Expand Down Expand Up @@ -2022,6 +2023,26 @@ def _embed_query(self, query: str) -> List[float]:
raise ValueError("Neither of embeddings or embedding_function is set")
return embedding.tolist() if hasattr(embedding, "tolist") else embedding

async def _aembed_query(self, query: str) -> List[float]:
"""Embed query text asynchronously.
Used to provide backward compatibility with `embedding_function` argument.
Args:
query: Query text.
Returns:
List of floats representing the query embedding.
"""
if self.embeddings is not None:
embedding = await self.embeddings.aembed_query(query)
else:
if self._embeddings_function is not None:
embedding = self._embeddings_function(query)
else:
raise ValueError("Neither of embeddings or embedding_function is set")
return embedding.tolist() if hasattr(embedding, "tolist") else embedding

def _embed_texts(self, texts: Iterable[str]) -> List[List[float]]:
"""Embed search texts.
Expand Down

0 comments on commit 81119b0

Please sign in to comment.