From 59d4b80a927677f2168a23ea43dd9f0ba152546b Mon Sep 17 00:00:00 2001 From: Ivan <58596654+istrebitel-1@users.noreply.github.com> Date: Sat, 23 Dec 2023 00:21:34 +0300 Subject: [PATCH] [community]: Elasticsearch chat history encoding (#15055) - Added ensure_ascii property to ElasticsearchChatMessageHistory --------- Co-authored-by: Ivan Chetverikov Co-authored-by: Harrison Chase --- .../chat_message_histories/elasticsearch.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libs/community/langchain_community/chat_message_histories/elasticsearch.py b/libs/community/langchain_community/chat_message_histories/elasticsearch.py index 39a18460224..f98515e99dc 100644 --- a/libs/community/langchain_community/chat_message_histories/elasticsearch.py +++ b/libs/community/langchain_community/chat_message_histories/elasticsearch.py @@ -26,6 +26,7 @@ class ElasticsearchChatMessageHistory(BaseChatMessageHistory): es_password: Password to use when connecting to Elasticsearch. es_api_key: API key to use when connecting to Elasticsearch. es_connection: Optional pre-existing Elasticsearch connection. + esnsure_ascii: Used to escape ASCII symbols in json.dumps. Defaults to True. index: Name of the index to use. session_id: Arbitrary key that is used to store the messages of a single chat session. @@ -42,9 +43,11 @@ class ElasticsearchChatMessageHistory(BaseChatMessageHistory): es_user: Optional[str] = None, es_api_key: Optional[str] = None, es_password: Optional[str] = None, + esnsure_ascii: Optional[bool] = True, ): self.index: str = index self.session_id: str = session_id + self.ensure_ascii: bool = esnsure_ascii # Initialize Elasticsearch client from passed client arg or connection info if es_connection is not None: @@ -172,7 +175,10 @@ class ElasticsearchChatMessageHistory(BaseChatMessageHistory): document={ "session_id": self.session_id, "created_at": round(time() * 1000), - "history": json.dumps(message_to_dict(message)), + "history": json.dumps( + message_to_dict(message), + ensure_ascii=self.ensure_ascii, + ), }, refresh=True, )