Incorporate Memory inside ElasticsearchDatabaseChain
Labels: name: area: memory, name: auto:question, name: area: models, name: integration: elasticsearch

### Issue you'd like to raise.

I am working with an elastic search index running locally via a docker container. Now I want to incorporate memory so that an elastic search query can be handled just like a chat. How can I do that so that every time the question I asked from the previous answers? It should prepare the es query accordingly.

Here is the sample code:


```
from elasticsearch import Elasticsearch
from langchain.chat_models import ChatOpenAI
from langchain.chains.elasticsearch_database import ElasticsearchDatabaseChain
import os
import openai
from dotenv import load_dotenv
import json
from langchain.prompts import HumanMessagePromptTemplate, AIMessagePromptTemplate
from langchain.schema import HumanMessage, AIMessage
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate

load_dotenv()

os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY')

ELASTIC_SEARCH_SERVER = "http://localhost:9200"
db = Elasticsearch(ELASTIC_SEARCH_SERVER)

memory = ConversationBufferMemory(memory_key="chat_history")

PROMPT_TEMPLATE = """Given an input question, create a syntactically correct Elasticsearch query to run. Unless the user specifies in their question a specific number of examples they wish to obtain, always limit your query to at most {top_k} results. You can order the results by a relevant column to return the most interesting examples in the database.

Unless told to do not query for all the columns from a specific index, only ask for a the few relevant columns given the question.

Pay attention to use only the column names that you can see in the mapping description. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which index. Return the query as valid json.

Use the following format:

Question: Question here
ESQuery: Elasticsearch Query formatted as json
"""

PROMPT_SUFFIX = """Only use the following Elasticsearch indices:
{indices_info}


Question: {input}
ESQuery:"""
query_Prompt=PROMPT_TEMPLATE + PROMPT_SUFFIX
PROMPT = PromptTemplate.from_template(
    query_Prompt,
)



DEFAULT_ANSWER_TEMPLATE = """Given an input question and relevant data from a database, answer the user question.
Only provide answer to me.

Use the following format:

Question: {input}
Data: {data}

Answer:"""

ANSWER_PROMPT = PromptTemplate.from_template(DEFAULT_ANSWER_TEMPLATE)

llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
chain = ElasticsearchDatabaseChain.from_llm(llm=llm, database=db, query_prompt=PROMPT, answer_prompt=ANSWER_PROMPT ,verbose=True, memory=memory)

while True:
    # Get user input
    user_input = input("Ask a question (or type 'exit' to end): ")

    if user_input.lower() == 'exit':
        break


    # Invoke the chat model with the user's question
    chain_answer = chain.invoke(user_input)

```

What could be the solution? Currently, it is not working if the following question is related to the previous answer.

### Suggestion:

_No response_