Skip to content

Commit

Permalink
Improve handling of empty queries for timescale vector (#12393)
Browse files Browse the repository at this point in the history
**Description:** Improve handling of empty queries in timescale-vector.
For timescale-vector it is more efficient to get a None embedding when
the embedding has no semantic meaning. It allows timescale-vector to
perform more optimizations. Thus, when the query is empty, use a None
embedding.

 Also pass down constructor arguments to the timescale vector client.

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
  • Loading branch information
cevian and baskaryan authored Oct 27, 2023
1 parent 38cee5f commit 11505f9
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions libs/langchain/langchain/vectorstores/timescalevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(
logger: Optional[logging.Logger] = None,
relevance_score_fn: Optional[Callable[[float], float]] = None,
time_partition_interval: Optional[timedelta] = None,
**kwargs: Any,
) -> None:
try:
from timescale_vector import client
Expand All @@ -103,13 +104,15 @@ def __init__(
self.num_dimensions,
self._distance_strategy.value.lower(),
time_partition_interval=self._time_partition_interval,
**kwargs,
)
self.async_client = client.Async(
self.service_url,
self.collection_name,
self.num_dimensions,
self._distance_strategy.value.lower(),
time_partition_interval=self._time_partition_interval,
**kwargs,
)
self.__post_init__()

Expand Down Expand Up @@ -310,6 +313,13 @@ async def aadd_texts(
texts=texts, embeddings=embeddings, metadatas=metadatas, ids=ids, **kwargs
)

def _embed_query(self, query: str) -> Optional[List[float]]:
# an empty query should not be embedded
if query is None or query == "" or query.isspace():
return None
else:
return self.embedding.embed_query(query)

def similarity_search(
self,
query: str,
Expand All @@ -328,7 +338,7 @@ def similarity_search(
Returns:
List of Documents most similar to the query.
"""
embedding = self.embedding.embed_query(text=query)
embedding = self._embed_query(query)
return self.similarity_search_by_vector(
embedding=embedding,
k=k,
Expand All @@ -355,7 +365,7 @@ async def asimilarity_search(
Returns:
List of Documents most similar to the query.
"""
embedding = self.embedding.embed_query(text=query)
embedding = self._embed_query(query)
return await self.asimilarity_search_by_vector(
embedding=embedding,
k=k,
Expand All @@ -382,7 +392,7 @@ def similarity_search_with_score(
Returns:
List of Documents most similar to the query and score for each
"""
embedding = self.embedding.embed_query(query)
embedding = self._embed_query(query)
docs = self.similarity_search_with_score_by_vector(
embedding=embedding,
k=k,
Expand Down Expand Up @@ -410,7 +420,8 @@ async def asimilarity_search_with_score(
Returns:
List of Documents most similar to the query and score for each
"""
embedding = self.embedding.embed_query(query)

embedding = self._embed_query(query)
return await self.asimilarity_search_with_score_by_vector(
embedding=embedding,
k=k,
Expand Down Expand Up @@ -445,7 +456,7 @@ def date_to_range_filter(self, **kwargs: Any) -> Any:

def similarity_search_with_score_by_vector(
self,
embedding: List[float],
embedding: Optional[List[float]],
k: int = 4,
filter: Optional[Union[dict, list]] = None,
predicates: Optional[Predicates] = None,
Expand Down Expand Up @@ -481,7 +492,7 @@ def similarity_search_with_score_by_vector(

async def asimilarity_search_with_score_by_vector(
self,
embedding: List[float],
embedding: Optional[List[float]],
k: int = 4,
filter: Optional[Union[dict, list]] = None,
predicates: Optional[Predicates] = None,
Expand Down Expand Up @@ -517,7 +528,7 @@ async def asimilarity_search_with_score_by_vector(

def similarity_search_by_vector(
self,
embedding: List[float],
embedding: Optional[List[float]],
k: int = 4,
filter: Optional[Union[dict, list]] = None,
predicates: Optional[Predicates] = None,
Expand All @@ -540,7 +551,7 @@ def similarity_search_by_vector(

async def asimilarity_search_by_vector(
self,
embedding: List[float],
embedding: Optional[List[float]],
k: int = 4,
filter: Optional[Union[dict, list]] = None,
predicates: Optional[Predicates] = None,
Expand Down

0 comments on commit 11505f9

Please sign in to comment.