Skip to content

Commit

Permalink
Merge pull request #1 from ZouhairElhadi/feature/add_similarty_by_vec…
Browse files Browse the repository at this point in the history
…tor_to_weaviate

update class Weaviate (weaviate.py): add an attribute (by_text) to ch…
  • Loading branch information
ZouhairElhadi authored May 8, 2023
2 parents aa11f7c + 82ea600 commit 4c401e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions langchain/vectorstores/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ def similarity_search(
) -> List[Document]:
"""Return docs most similar to query."""

@abstractmethod
def similarity_search_by_text(
self, query: str, k: int = 4, **kwargs: Any
) -> List[Document]:
"""Return docs most similar to query."""

def similarity_search_with_relevance_scores(
self,
query: str,
Expand Down
11 changes: 11 additions & 0 deletions langchain/vectorstores/weaviate.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(
client: Any,
index_name: str,
text_key: str,
by_text: bool = True,
embedding: Optional[Embeddings] = None,
attributes: Optional[List[str]] = None,
relevance_score_fn: Optional[
Expand All @@ -105,6 +106,7 @@ def __init__(
self._index_name = index_name
self._embedding = embedding
self._text_key = text_key
self._by_text = by_text
self._query_attrs = [self._text_key]
self._relevance_score_fn = relevance_score_fn
if attributes is not None:
Expand Down Expand Up @@ -154,6 +156,15 @@ def json_serializable(value: Any) -> Any:
return ids

def similarity_search(
self, query: str, k: int = 4, **kwargs: Any
) -> List[Document]:
if self._by_text:
return self.similarity_search_by_text(query, k, **kwargs)
else:
embd = self._embedding.embed_query(query)
return self.similarity_search_by_vector(embd, k, **kwargs)

def similarity_search_by_text(
self, query: str, k: int = 4, **kwargs: Any
) -> List[Document]:
"""Return docs most similar to query.
Expand Down

0 comments on commit 4c401e0

Please sign in to comment.