I am working on building a chatbot that takes a URL and then you can ask questions and get answers from the chatbot. I ask the user to first submit the URL and press the Submit Button, then ask the question and press the Ask button. As soon as the second button is pressed, the web app resets and does not give me the answer. I have attached the screenshots below of the states. I have also attached the link to my GitHub repository. Please help me resolve this and understand the issue with the flow.
Here is a version that seems to work. Two key changes:
-
Button’s don’t stay “pressed” after something else happens, so a button press should trigger a single event, and should not be required to stay pressed while another interaction happened. I changed the code so that the first button press processes the documents, and the second press asks the question.
-
The session state objects should only be set to and None the first time, once they are already populated, don’t do it again, otherwise they will be overwritten every time you run the app.
import os
import streamlit as st
from langchain import OpenAI
from langchain.chains import RetrievalQAWithSourcesChain
from langchain.document_loaders import UnstructuredURLLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
def process_and_save(urls):
loaders = UnstructuredURLLoader(urls=urls)
data = loaders.load()
text_splitter = CharacterTextSplitter(
separator="\n", chunk_size=1000, chunk_overlap=300
)
docs = text_splitter.split_documents(data)
embeddings = OpenAIEmbeddings()
vectorStore_openAI = FAISS.from_documents(docs, embeddings)
return vectorStore_openAI
if "url_list" not in st.session_state:
# Initialize session state
st.session_state.url_list = []
st.session_state.VectorStore = None
st.session_state.chain = None
st.title("WebChatMate")
st.subheader("Your Conversational URL Companion")
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_KEY"]
url = st.text_input("Enter Your URL")
submit = st.button("Submit")
if submit and url:
if url not in st.session_state.url_list:
st.session_state.url_list += [url]
st.session_state.VectorStore = process_and_save(st.session_state.url_list)
llm = OpenAI(temperature=0)
st.session_state.chain = RetrievalQAWithSourcesChain.from_llm(
llm=llm, retriever=st.session_state.VectorStore.as_retriever()
)
st.write("URLs", st.session_state.url_list)
if st.session_state.chain:
user_question = st.text_input("Enter your question:")
ask = st.button("Ask")
if ask and user_question and st.session_state.chain:
response = st.session_state.chain(
{"question": user_question}, return_only_outputs=True
)
answer = response["answer"].replace("\n", "")
sources = response.get("sources", "")
st.write("Answer:", answer)
st.write("Sources:", sources)
Hi @blackary
I have been working on the code to improve it based on your changes. The code does not have any errors but it does not seem to work correctly as it is providing the next answer without the user entering a question. I am not sure how to fix this as it seems like something Streamlit is doing. Please help.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.