OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I get my AI chatbot to remember past answers?

  • Thread starter Thread starter ShadowFly
  • Start date Start date
S

ShadowFly

Guest
I have a problem that I have been struggling with for a long time. In my small pet project, I am creating a chatbot that could respond using data from the database, as well as tools for currency conversion. The problem is that there is no way I can add to it the ability to remember past answers. I tried just passing

Code:
agent_executor.invoke to him({
                "input": question,
                "history:" st.session_state.history
            })

but nothing worked. He just doesn't want to remember anything. Can you tell me how to add the ability for the bot to remember messages? Initially, I planned to do this through ConversationSummaryMemory

Here is code:

Code:
import streamlit as st
from langchain_openai import ChatOpenAI
from sqlalchemy import create_engine
from configs import OAI_MODEL
from langchain.chains.conversation.memory import ConversationSummaryMemory
from langchain_community.utilities import SQLDatabase
from langchain_experimental.sql import SQLDatabaseChain
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
from langchain.agents import create_openai_tools_agent, AgentExecutor
from langchain import hub
from langchain_core.messages import AIMessage, HumanMessage
from langchain.tools import StructuredTool
from langchain_community.callbacks.manager import get_openai_callback
from langchain_experimental.tools.python.tool import PythonREPLTool
from dotenv import find_dotenv, load_dotenv
from custom_agents.exchange import get_exchange_conv_rate, get_latest_currences

load_dotenv(find_dotenv(), override=True)

if "messages" not in st.session_state:
    st.session_state.messages = []

if "history" not in st.session_state:
    st.session_state.history = []

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

prompt = hub.pull("hwchase17/openai-tools-agent")

llm = ChatOpenAI(
    temperature=0.5,
    max_tokens=3000,
    model_name=OAI_MODEL,
    verbose=False
)

memory = ConversationSummaryMemory(llm=llm)

# Existing tools
tools = [
    StructuredTool.from_function(get_exchange_conv_rate),
    StructuredTool.from_function(get_latest_currences),
]

# Add SQL Database tool

# Creating a connection
db_path = "second_project/data.db"
engine = create_engine(f"sqlite:///{db_path}")
database = SQLDatabase(engine)

# Setting up a chain 
sql_chain = SQLDatabaseChain(
    llm=llm,
    database=database,
    verbose=False
)

# Initializing a toolkit
sql_toolkit = SQLDatabaseToolkit(
    db=database,
    llm=llm
)

# Adding db-tools
tools.extend(sql_toolkit.get_tools())

# Add arithmetic operations tool
arithmetic_tool = PythonREPLTool()
tools.append(arithmetic_tool)


# Initialize the agent with the new tools
agent = create_openai_tools_agent(
    llm,
    tools,
    prompt
)


agent_executor = AgentExecutor(
    agent=agent,
    memory=memory,
    tools=tools, 
    verbose=True,
)


# Creating web-application

question = st.chat_input("Please, enter your message")
if question:
    st.session_state.messages.append({"role": "user", "content": question})
    with st.chat_message("user"):
        st.markdown(question)

    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        with get_openai_callback() as cb:
            full_response = agent_executor.invoke({
                "input": question 
            })["output"]
            st.session_state.history.extend([
                HumanMessage(content=question),
                AIMessage(content=full_response)
            ])
            st.write(full_response)
            print(cb)
        st.session_state.messages.append({"role": "assistant", "content": full_response})

As I said earlier, I tried to add a second argument to agent_executor, but it didn't remember anything. Next, I tried to adapt the code from the documentation

Code:
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.utilities import GoogleSearchAPIWrapper

search = GoogleSearchAPIWrapper()
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="useful for when you need to answer questions about current events",
    )
]

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")

llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(
    agent=agent, tools=tools, verbose=True, memory=memory
)

But it didn't work out either. I found few articles on this topic, and nothing at all on my topic. Please help me figure it out
<p>I have a problem that I have been struggling with for a long time. In my small pet project, I am creating a chatbot that could respond using data from the database, as well as tools for currency conversion. The problem is that there is no way I can add to it the ability to remember past answers. I tried just passing</p>
<pre><code>agent_executor.invoke to him({
"input": question,
"history:" st.session_state.history
})
</code></pre>
<p>but nothing worked. He just doesn't want to remember anything. Can you tell me how to add the ability for the bot to remember messages? Initially, I planned to do this through ConversationSummaryMemory</p>
<p>Here is code:</p>
<pre><code>import streamlit as st
from langchain_openai import ChatOpenAI
from sqlalchemy import create_engine
from configs import OAI_MODEL
from langchain.chains.conversation.memory import ConversationSummaryMemory
from langchain_community.utilities import SQLDatabase
from langchain_experimental.sql import SQLDatabaseChain
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
from langchain.agents import create_openai_tools_agent, AgentExecutor
from langchain import hub
from langchain_core.messages import AIMessage, HumanMessage
from langchain.tools import StructuredTool
from langchain_community.callbacks.manager import get_openai_callback
from langchain_experimental.tools.python.tool import PythonREPLTool
from dotenv import find_dotenv, load_dotenv
from custom_agents.exchange import get_exchange_conv_rate, get_latest_currences

load_dotenv(find_dotenv(), override=True)

if "messages" not in st.session_state:
st.session_state.messages = []

if "history" not in st.session_state:
st.session_state.history = []

for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])

prompt = hub.pull("hwchase17/openai-tools-agent")

llm = ChatOpenAI(
temperature=0.5,
max_tokens=3000,
model_name=OAI_MODEL,
verbose=False
)

memory = ConversationSummaryMemory(llm=llm)

# Existing tools
tools = [
StructuredTool.from_function(get_exchange_conv_rate),
StructuredTool.from_function(get_latest_currences),
]

# Add SQL Database tool

# Creating a connection
db_path = "second_project/data.db"
engine = create_engine(f"sqlite:///{db_path}")
database = SQLDatabase(engine)

# Setting up a chain
sql_chain = SQLDatabaseChain(
llm=llm,
database=database,
verbose=False
)

# Initializing a toolkit
sql_toolkit = SQLDatabaseToolkit(
db=database,
llm=llm
)

# Adding db-tools
tools.extend(sql_toolkit.get_tools())

# Add arithmetic operations tool
arithmetic_tool = PythonREPLTool()
tools.append(arithmetic_tool)


# Initialize the agent with the new tools
agent = create_openai_tools_agent(
llm,
tools,
prompt
)


agent_executor = AgentExecutor(
agent=agent,
memory=memory,
tools=tools,
verbose=True,
)


# Creating web-application

question = st.chat_input("Please, enter your message")
if question:
st.session_state.messages.append({"role": "user", "content": question})
with st.chat_message("user"):
st.markdown(question)

with st.chat_message("assistant"):
message_placeholder = st.empty()
with get_openai_callback() as cb:
full_response = agent_executor.invoke({
"input": question
})["output"]
st.session_state.history.extend([
HumanMessage(content=question),
AIMessage(content=full_response)
])
st.write(full_response)
print(cb)
st.session_state.messages.append({"role": "assistant", "content": full_response})
</code></pre>
<p>As I said earlier, I tried to add a second argument to agent_executor, but it didn't remember anything. Next, I tried to adapt the code from the documentation</p>
<pre><code>from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.utilities import GoogleSearchAPIWrapper

search = GoogleSearchAPIWrapper()
tools = [
Tool(
name="Search",
func=search.run,
description="useful for when you need to answer questions about current events",
)
]

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")

llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(
agent=agent, tools=tools, verbose=True, memory=memory
)
</code></pre>
<p>But it didn't work out either. I found few articles on this topic, and nothing at all on my topic. Please help me figure it out</p>
 

Latest posts

Top