How to use memory in SQLDatabaseChain?

I’m trying to use memory to keep the context for SQLDatabaseChain:

db = SQLDatabase.from_uri("sqlite:///test.db")
llm = ChatOpenAI(temperature=0, verbose=True)
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, memory=ConversationBufferMemory())

Then subsequent calls to the chain should preserve the context but it doesn’t work:

while True:
    query = input ( "Query: " )

Note: by preserving the context of the chat I mean something like in the following imaginary example:

query: what is the top rated movie?
response: the top rated movie is Pulp Fiction
query: and what is its rating?
response: its rating is 8.5 


Welcome to the OpenAI developer forum!

Just a note to say that this is not a Langchain support forum, but I can give you the general idea of where you need to go.

You see that you are printing the well that output needs to be captured to a string and fed back in to the query, quite how that is done with langchain I don’t know as I don’t use it.


This site seems to cover what you are talking about sort of… you may have to mod the supplied code for your exact needs …

1 Like