2323import weaviate # type: ignore
2424from langchain_core .documents import Document
2525from langchain_core .embeddings import Embeddings
26+ from langchain_core .runnables .config import run_in_executor
2627from langchain_core .vectorstores import VectorStore
2728
2829from langchain_weaviate .utils import maximal_marginal_relevance
@@ -570,7 +571,10 @@ async def aadd_texts(
570571 ) -> List [str ]:
571572 """Add texts to Weaviate asynchronously."""
572573 if self ._client_async is None :
573- raise ValueError ("client_async must be an instance of WeaviateAsyncClient" )
574+ logger .warning ("client_async is None, using synchronous client instead" )
575+ return await run_in_executor (
576+ None , self .add_texts , texts , metadatas , tenant , ** kwargs
577+ )
574578 from weaviate .util import get_valid_uuid # type: ignore
575579
576580 if tenant and not await self ._adoes_tenant_exist (tenant ):
@@ -678,7 +682,7 @@ async def _perform_asearch(
678682 ValueError: If _embedding is None or an invalid search method is provided.
679683 """
680684 if self ._client_async is None :
681- raise ValueError ("client_async must be an instance of WeaviateAsyncClient " )
685+ raise ValueError ("cannot perform asearch with synchronous client " )
682686 if self ._embedding is None :
683687 raise ValueError ("_embedding cannot be None for similarity_search" )
684688
@@ -739,8 +743,9 @@ async def _perform_asearch(
739743
740744 async def _adoes_tenant_exist (self , tenant : str ) -> bool :
741745 """Check if tenant exists in Weaviate asynchronously."""
742- if self ._client_async is None :
743- raise ValueError ("client_async must be an instance of WeaviateAsyncClient" )
746+ assert (
747+ self ._client_async is not None
748+ ), "client_async must be an instance of WeaviateAsyncClient"
744749 assert (
745750 self ._multi_tenancy_enabled
746751 ), "Cannot check for tenant existence when multi-tenancy is not enabled"
@@ -763,6 +768,10 @@ async def asimilarity_search(
763768 Returns:
764769 List of Documents most similar to the query.
765770 """
771+ if self ._client_async is None :
772+ return await run_in_executor (
773+ None , self .similarity_search , query , k , ** kwargs
774+ )
766775 result = await self ._perform_asearch (query , k , ** kwargs )
767776 return result
768777
@@ -791,6 +800,16 @@ async def amax_marginal_relevance_search(
791800 Returns:
792801 List of Documents selected by maximal marginal relevance.
793802 """
803+ if self ._client_async is None :
804+ return await run_in_executor (
805+ None ,
806+ self .max_marginal_relevance_search ,
807+ query ,
808+ k ,
809+ fetch_k ,
810+ lambda_mult ,
811+ ** kwargs ,
812+ )
794813 if self ._embedding is not None :
795814 embedding = await self ._embedding .aembed_query (query )
796815 else :
@@ -827,6 +846,16 @@ async def amax_marginal_relevance_search_by_vector(
827846 Returns:
828847 List of Documents selected by maximal marginal relevance.
829848 """
849+ if self ._client_async is None :
850+ return await run_in_executor (
851+ None ,
852+ self .max_marginal_relevance_search_by_vector ,
853+ embedding ,
854+ k ,
855+ fetch_k ,
856+ lambda_mult ,
857+ ** kwargs ,
858+ )
830859 results = await self ._perform_asearch (
831860 query = None ,
832861 k = fetch_k ,
@@ -857,9 +886,36 @@ async def asimilarity_search_with_score(
857886 text and cosine distance in float for each.
858887 Lower score represents more similarity.
859888 """
889+ if self ._client_async is None :
890+ return await run_in_executor (
891+ None , self .similarity_search_with_score , query , k , ** kwargs
892+ )
860893 results = await self ._perform_asearch (query , k , return_score = True , ** kwargs )
861894 return results
862895
896+ async def asimilarity_search_by_vector (
897+ self , embedding : List [float ], k : int = 4 , ** kwargs : Any
898+ ) -> List [Document ]:
899+ """Return docs most similar to embedding vector asynchronously.
900+
901+ Args:
902+ embedding: Embedding vector to look up documents similar to.
903+ k: Number of Documents to return. Defaults to 4.
904+ **kwargs: Additional keyword arguments will be passed to the `hybrid()`
905+ function of the weaviate client.
906+
907+ Returns:
908+ List of Documents most similar to the embedding.
909+ """
910+ if self ._client_async is None :
911+ return await run_in_executor (
912+ None , self .similarity_search_by_vector , embedding , k , ** kwargs
913+ )
914+ result = await self ._perform_asearch (
915+ query = None , k = k , vector = embedding , ** kwargs
916+ )
917+ return result
918+
863919 @classmethod
864920 async def afrom_texts (
865921 cls ,
@@ -917,8 +973,6 @@ async def afrom_texts(
917973
918974 if client is None :
919975 raise ValueError ("client must be an instance of WeaviateClient" )
920- if client_async is None :
921- raise ValueError ("client_async must be an instance of WeaviateAsyncClient" )
922976
923977 weaviate_vector_store = cls (
924978 client ,
@@ -962,22 +1016,3 @@ async def _atenant_context(
9621016 yield collection
9631017 finally :
9641018 pass
965-
966- async def asimilarity_search_by_vector (
967- self , embedding : List [float ], k : int = 4 , ** kwargs : Any
968- ) -> List [Document ]:
969- """Return docs most similar to embedding vector asynchronously.
970-
971- Args:
972- embedding: Embedding vector to look up documents similar to.
973- k: Number of Documents to return. Defaults to 4.
974- **kwargs: Additional keyword arguments will be passed to the `hybrid()`
975- function of the weaviate client.
976-
977- Returns:
978- List of Documents most similar to the embedding.
979- """
980- result = await self ._perform_asearch (
981- query = None , k = k , vector = embedding , ** kwargs
982- )
983- return result
0 commit comments