3

I have a streamlit chatbot that works perfectly fine but does not remember previous chat history. I was trying to add it with langchain ConversationBufferMemory but it does not seem to work.

Here is a sample of chatbot I created:

import streamlit as st
from streamlit_chat import message

from langchain.chains import ConversationChain
from langchain.llms import OpenAI
from langchain.chat_models import AzureChatOpenAI

from langchain.memory import ConversationBufferMemory

from langchain.prompts import (
    ChatPromptTemplate, 
    MessagesPlaceholder, 
    SystemMessagePromptTemplate, 
    HumanMessagePromptTemplate
)


prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know."),
    MessagesPlaceholder(variable_name="history"),
    HumanMessagePromptTemplate.from_template("{input}")
])


def load_chain(prompt):
    """Logic for loading the chain you want to use should go here."""
    llm = AzureChatOpenAI(
                    deployment_name = 'gpt-35-turbo',
                    model_name = 'gpt-35-turbo',
                    temperature = 0,
                    openai_api_key = '.....',
                    openai_api_base = '.....',
                    openai_api_version = "2023-05-15", 
                    openai_api_type="azure"
                    )
    memory = ConversationBufferMemory(return_messages=True)
    chain = ConversationChain(
        llm=llm,
        verbose=True,
        prompt=prompt,
        memory=memory
    )
    return chain

chain = load_chain(prompt)

# From here down is all the StreamLit UI.
st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
st.header("LangChain Demo")

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

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

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

def get_text():
    input_text = st.text_input("You: ", "Hello, how are you?", key="input")
    return input_text


user_input = get_text()

if user_input:
    output = chain.run(input=user_input, history=st.session_state["history"])
    st.session_state["history"].append((user_input, output))
    st.session_state.past.append(user_input)
    st.session_state.generated.append(output)
    st.write(st.session_state["history"])

if st.session_state["generated"]:

    for i in range(len(st.session_state["generated"]) - 1, -1, -1):
        message(st.session_state["generated"][i], key=str(i))
        message(st.session_state["past"][i], is_user=True, key=str(i) + "_user")

It looks like bot ignores ConversationBufferMemory for some reason. Any help would be appreciated.

illuminato
  • 1,057
  • 1
  • 11
  • 33
  • Have you referred to this official [docs](https://python.langchain.com/docs/modules/chains/popular/chat_vector_db#pass-in-chat-history) example? – shaik moeed Jul 11 '23 at 05:41
  • @shaikmoeed Yes, I did. I was trying to change `ConversationBufferMemory(return_messages=True)` in my code to `ConversationBufferMemory(memory_key="history", return_messages=True)` but after first query bot getting in frozen mode (with status running) for some reason. – illuminato Jul 11 '23 at 06:00
  • Streamlit rerenders a page after every button click. All your code is being rerun, which means that all your objects are recreated, including the memory object. One of the suggestions I got is to store the chain in a session. You check if it is there, then you just pull it from the session. The idea is to create it once and then reuse – hiper2d Aug 18 '23 at 19:19

0 Answers0