1

I want to create a chain to make query against my database. Also I want to add memory to this chain.

Example of dialogue I want to see:

Query: Who is an owner of website with domain domain.com? Answer: Boba Bobovich Query: Tell me his email Answer: Boba Bobovich's email is boba@boba.com

I have this code:

import os
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain, PromptTemplate
from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
db = SQLDatabase.from_uri(os.getenv("DB_URI"))
llm = OpenAI(temperature=0, verbose=True)
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, memory=memory)

db_chain.run("Who is owner of the website with domain https://damon.name")
db_chain.run("Tell me his email")
print(memory.load_memory_variables({}))

It gives:

> Entering new  chain...
Who is owner of the website with domain https://damon.name
SQLQuery:SELECT first_name, last_name FROM owners JOIN websites ON owners.id = websites.owner_id WHERE domain = 'https://damon.name' LIMIT 5;
SQLResult: [('Geo', 'Mertz')]
Answer:Geo Mertz is the owner of the website with domain https://damon.name.
> Finished chain.
    
> Entering new  chain...
Tell me his email
SQLQuery:SELECT email FROM owners WHERE first_name = 'Westley' AND last_name = 'Waters'
SQLResult: [('Ken70@hotmail.com',)]
Answer:Westley Waters' email is Ken70@hotmail.com.
> Finished chain.
{'history': "Human: Who is owner of the website with domain https://damon.name\nAI: Geo Mertz is the owner of the website with domain https://damon.name.\nHuman: Tell me his email\nAI: Westley Waters' email is Ken70@hotmail.com."}

Well, it saves context to memory but chain doesn't use it to give a proper answer (wrong email). How to fix it?

Also I don't want to use an agent because I want to manage to do this with a simple chain first. Tell me if it's impossible with simple chain.

bleschunov
  • 41
  • 6

2 Answers2

0

If you don't want to use an agent then you can add a template to your llm and that has a chat history field and then add that as a memory key in the ConversationBufferMemory().

Like this :

template = """You are a chatbot having a conversation with a human.

{chat_history} Human: {human_input} Chatbot:"""

prompt = PromptTemplate(input_variables=["chat_history", "human_input"], template=template ) 
memory = ConversationBufferMemory(memory_key="chat_history")

llm_chain = LLMChain(
    llm=OpenAI(),
    prompt=prompt,
    verbose=True,
    memory=memory)

See official docs : https://python.langchain.com/docs/modules/memory/how_to/adding_memory

If you change your mind and chose to use an agent here's how to do it using a chat_history in the suffix:

prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" 
suffix = """Begin!"

{chat_history} Question: {input} {agent_scratchpad}"""

prompt = ZeroShotAgent.create_prompt(
    tools,
    prefix=prefix,
    suffix=suffix,
    input_variables=["input", "chat_history", "agent_scratchpad"], )

memory = ConversationBufferMemory(memory_key="chat_history", chat_memory=message_history )

See official docs : https://python.langchain.com/docs/modules/memory/how_to/agent_with_memory_in_db

0

Though I have not tried this myself, I think we can approach this problem by generating single query by looking at all of user's historical questions at once. In your case, the first query is deemed accurate.,and let us assume you have user's first question in memory. Upon the second question, you pass both these questions as single prompt to LLM to generate an SQL query.,which ideally should geenrate query like:

select email from owners JOIN websites ON owners.id = websites.owner_id WHERE domain = 'https://damon.name' LIMIT 5;

My3
  • 187
  • 1
  • 8