Update app.py
Browse files
app.py
CHANGED
|
@@ -68,7 +68,7 @@ def get_vectorstore(text_chunks):
|
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
-
def get_conversation_chain():
|
| 72 |
|
| 73 |
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
| 74 |
|
|
@@ -90,33 +90,18 @@ def get_conversation_chain():
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
prompt = hub.pull("rlm/rag-prompt")
|
| 99 |
-
rag_chain = prompt | llm | StrOutputParser()
|
| 100 |
|
| 101 |
|
| 102 |
return rag_chain
|
| 103 |
|
| 104 |
-
def sidebar():
|
| 105 |
-
with st.sidebar:
|
| 106 |
-
st.subheader("Your documents")
|
| 107 |
-
pdf_docs = st.file_uploader("For Chatbot to get alive, upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
| 108 |
-
|
| 109 |
-
if st.button("Process"):
|
| 110 |
-
if pdf_docs:
|
| 111 |
-
with st.spinner("Processing"):
|
| 112 |
-
# ... your processing code ...
|
| 113 |
-
vectorstore = get_vectorstore(text_chunks)
|
| 114 |
-
conversation = get_conversation_chain()
|
| 115 |
-
st.success("Files have been processed into a vector store.")
|
| 116 |
-
else:
|
| 117 |
-
st.write("Kazkas neto")
|
| 118 |
|
| 119 |
-
return vectorstore, conversation
|
| 120 |
|
| 121 |
|
| 122 |
|
|
@@ -141,7 +126,24 @@ def main():
|
|
| 141 |
handle_userinput(user_question, vectorstore, conversation)
|
| 142 |
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
|
| 147 |
|
|
@@ -165,7 +167,7 @@ def handle_userinput(user_question,vectorstore,conversation ):
|
|
| 165 |
st.session_state.chat_history.append({"role": "user", "content": user_question})
|
| 166 |
|
| 167 |
|
| 168 |
-
retriever =
|
| 169 |
docs = retriever.invoke(user_question)
|
| 170 |
|
| 171 |
|
|
@@ -173,7 +175,7 @@ def handle_userinput(user_question,vectorstore,conversation ):
|
|
| 173 |
doc_txt = [doc.page_content for doc in docs]
|
| 174 |
|
| 175 |
# Invoke conversation chain
|
| 176 |
-
response = conversation.invoke({"context": docs, "question": user_question})
|
| 177 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 178 |
|
| 179 |
for i, message in enumerate(st.session_state.chat_history):
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
+
def get_conversation_chain(vectorstore):
|
| 72 |
|
| 73 |
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
| 74 |
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
+
retriever = vectorstore.as_retriever(search_type='mmr', k=7)
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
prompt = hub.pull("rlm/rag-prompt")
|
| 99 |
+
rag_chain = retriever | prompt | llm | StrOutputParser()
|
| 100 |
|
| 101 |
|
| 102 |
return rag_chain
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
|
|
|
|
| 126 |
handle_userinput(user_question, vectorstore, conversation)
|
| 127 |
|
| 128 |
|
| 129 |
+
with st.sidebar:
|
| 130 |
+
st.subheader("Your documents")
|
| 131 |
+
pdf_docs = st.file_uploader(
|
| 132 |
+
"Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
|
| 133 |
+
if st.button("Process"):
|
| 134 |
+
with st.spinner("Processing"):
|
| 135 |
+
# get pdf text
|
| 136 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 137 |
+
|
| 138 |
+
# get the text chunks
|
| 139 |
+
text_chunks = get_text_chunks(raw_text)
|
| 140 |
+
|
| 141 |
+
# create vector store
|
| 142 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 143 |
|
| 144 |
+
# create conversation chain
|
| 145 |
+
st.session_state.conversation = get_conversation_chain(
|
| 146 |
+
vectorstore)
|
| 147 |
|
| 148 |
|
| 149 |
|
|
|
|
| 167 |
st.session_state.chat_history.append({"role": "user", "content": user_question})
|
| 168 |
|
| 169 |
|
| 170 |
+
retriever = st.session_state.conversation.retriever()
|
| 171 |
docs = retriever.invoke(user_question)
|
| 172 |
|
| 173 |
|
|
|
|
| 175 |
doc_txt = [doc.page_content for doc in docs]
|
| 176 |
|
| 177 |
# Invoke conversation chain
|
| 178 |
+
response = st.session_state.conversation.invoke({"context": docs, "question": user_question})
|
| 179 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
| 180 |
|
| 181 |
for i, message in enumerate(st.session_state.chat_history):
|