Summary
Dear community,
I am encountering an issue with my Streamlit app where every action on the app triggers the user question to be sent and a response to be returned. I have provided more details on the expected and actual behaviour in the respective sections. Just as an overview, the app allows users to upload documents to a vector store and ask questions about the documents in the vector store leveraging ChatGPT.
Steps to reproduce
Code snippet:
import streamlit as st
from dotenv import load_dotenv
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter
from langchain.memory import ConversationBufferMemory
from langchain.embeddings import OpenAIEmbeddings
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import AzureChatOpenAI
import openai
from langchain.vectorstores import Qdrant
import qdrant_client
import os
from htmlTemplates import css, bot_template, user_template
openai.log = "debug"
client = qdrant_client.QdrantClient(os.getenv("QDRANT_HOST"), api_key=os.getenv("QDRANT_API_KEY"))
def get_pdf_text(pdf_docs):
text = ""
for pdf in pdf_docs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
return text
def get_text_chunks(text):
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = text_splitter.split_text(text)
return chunks
def get_vectorstore(text_chunks):
embeddings = OpenAIEmbeddings(
chunk_size=1,
deployment="embeddings-ada",
model="text-embedding-ada-002",
)
vectorstore = Qdrant(client=client, collection_name=os.getenv("QDRANT_COLLECTION_NAME"), embeddings=embeddings)
return vectorstore
def process_pdfs(pdf_docs, vectorstore):
raw_text = get_pdf_text(pdf_docs)
text_chunks = get_text_chunks(raw_text)
vectorstore.add_texts(text_chunks)
def initialize_conversation_chain(vectorstore):
llm = AzureChatOpenAI(
deployment_name="gpt35turbo",
model_name="gpt-35-turbo"
)
memory = ConversationBufferMemory(
memory_key='chat_history', return_messages=True)
conversation_chain = ConversationalRetrievalChain.from_llm(
llm=llm,
retriever=vectorstore.as_retriever(),
memory=memory
)
return conversation_chain
def handle_userinput(user_question):
response = st.session_state.conversation({'question': user_question})
st.session_state.chat_history = response['chat_history']
for i, message in enumerate(st.session_state.chat_history):
if i % 2 == 0:
st.write(user_template.replace(
"{{MSG}}", message.content), unsafe_allow_html=True)
else:
st.write(bot_template.replace(
"{{MSG}}", message.content), unsafe_allow_html=True)
def main():
load_dotenv()
st.set_page_config(page_title="AML Policies and Procedures",
page_icon=":books:")
st.write(css, unsafe_allow_html=True)
vectorstore = get_vectorstore([]) # Initialize with an empty list
if "conversation" not in st.session_state:
st.session_state.conversation = initialize_conversation_chain(vectorstore)
if "chat_history" not in st.session_state:
st.session_state.chat_history = None
st.header("AML Policies and Procedures :books:")
user_question = st.text_input("Ask a question about your documents:")
if user_question:
handle_userinput(user_question)
with st.sidebar:
st.subheader("Your documents")
pdf_docs = st.file_uploader(
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
if st.button("Process"):
with st.spinner("Processing"):
process_pdfs(pdf_docs, vectorstore)
if __name__ == '__main__':
main()
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
What I am expecting is for the question to be sent only when the user hits the enter key, at which point a response is displayed.
Actual behavior:
What is actually happening is that every action on the app causes the Q & A sequence to happen. For example, if I click on the button to browse for documents, the question in the textbox is sent again and an answer is regenerated. The same thing happens when I click on the process button, which vectorizes the PDF and saves it in my cloud DB. Every action on the app makes the Q&A process run and this is unideal as every API call has a cost and the users will see duplicate question and answer pairs. My hunch is that I am handling the session state wrong. I simply want the conversation chain to be displayed on the app.
Debug info
- Streamlit version:1.24.1
- Python version: 3.10.9
- Using Conda? PipEnv? PyEnv? Pex?
- OS version: Windows 11
- Browser version: Edge
Requirements file
Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.
langchain==0.0.228
PyPDF2==3.0.1
python-dotenv==1.0.0
streamlit==1.24.1
openai==0.27.8
qdrant-client==1.4.0
tiktoken==0.4.0
Links
- Link to your GitHub repo:
- Link to your deployed app:
Additional information
If needed, add any other context about the problem here.