From e93be14c11a7a134e62c16477cb04d9fc3b8a72f Mon Sep 17 00:00:00 2001 From: Bob Lin Date: Tue, 2 Jan 2024 18:48:17 -0600 Subject: [PATCH] Improvement: Allow passing parameters to the underlying es_client. Closes: #14403 (#14435) ### Description In https://github.com/langchain-ai/langchain/issues/14403, the user mentioned that he hopes not to verify ssl and needs to pass more parameters I found that the `Elasticsearch` class [has very many parameters](https://github.com/elastic/elasticsearch-py/blob/98f2af2134dfc3f2b2054b87a4056e2508b9b8db/elasticsearch/_sync/client/__init__.py#L131-L191 ): Screenshot 2023-12-08 at 4 24 39 PM In order to adapt to more situations, I want to add the kwargs parameter so that users can enter more `Elasticsearch` parameters. Like [redis](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/vectorstores/redis/base.py#L253), [tair](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/vectorstores/tair.py#L32), [myscale](https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/vectorstores/myscale.py#L112) and so on. --- .../langchain_community/vectorstores/elasticsearch.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/vectorstores/elasticsearch.py b/libs/community/langchain_community/vectorstores/elasticsearch.py index 8eb3eab0ab..24c91472c9 100644 --- a/libs/community/langchain_community/vectorstores/elasticsearch.py +++ b/libs/community/langchain_community/vectorstores/elasticsearch.py @@ -256,7 +256,7 @@ class ExactRetrievalStrategy(BaseRetrievalStrategy): elif similarity is DistanceStrategy.DOT_PRODUCT: similarityAlgo = f""" double value = dotProduct(params.query_vector, '{vector_query_field}'); - return sigmoid(1, Math.E, -value); + return sigmoid(1, Math.E, -value); """ else: raise ValueError(f"Similarity {similarity} not supported.") @@ -512,6 +512,7 @@ class ElasticsearchStore(VectorStore): ] ] = None, strategy: BaseRetrievalStrategy = ApproxRetrievalStrategy(), + es_params: Optional[Dict[str, Any]] = None, ): self.embedding = embedding self.index_name = index_name @@ -535,6 +536,7 @@ class ElasticsearchStore(VectorStore): password=es_password, cloud_id=es_cloud_id, api_key=es_api_key, + es_params=es_params, ) else: raise ValueError( @@ -556,6 +558,7 @@ class ElasticsearchStore(VectorStore): api_key: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, + es_params: Optional[Dict[str, Any]] = None, ) -> "Elasticsearch": try: import elasticsearch @@ -584,6 +587,9 @@ class ElasticsearchStore(VectorStore): elif username and password: connection_params["basic_auth"] = (username, password) + if es_params is not None: + connection_params.update(es_params) + es_client = elasticsearch.Elasticsearch( **connection_params, headers={"user-agent": ElasticsearchStore.get_user_agent()},