Memory in SQLDB chain

72 Views Asked by At

I want to know , if we can you memory in SQL DB chain db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)

Can we introduce Coversational Buffer memory in this ? If not is there any alternative way to achieve this ?

I tried introducing the memory in SQL DB chain like this : db_chain = SQLDatabaseChainWithMemory.from_llm(llm, db, verbose=True, memory=memory)

using a custom class :

from langchain import SQLDatabaseChain
from langchain.memory import ConversationBufferMemory

class SQLDatabaseChainWithMemory(SQLDatabaseChain):
    def __init__(self, \*args, memory: ConversationBufferWindowMemory, \*\*kwargs):
        super().__init__(\*args, \*\*kwargs)
        self.memory = memory

    def run(self, inputs):
        # Add the memory variables to the inputs
        inputs_with_memory = {**inputs, **self.memory.load_memory_variables({})}
        
        # Call the parent class's run method with the updated inputs
        return super().run(inputs_with_memory)

It was able to store the query like this {'query': 'what is the county name at zip code 7643 ', 'memory': [HumanMessage(content='...... ', additional_kwargs={}, example=False), AIMessage(content='Final answer: .......', additional_kwargs={}, example=False)], 'result': 'Final answer here: .......'}

But is was not able to answer the question based on history .

I doubt that memory is not avaiable for SQLDB chain .

0

There are 0 best solutions below