ArturG9 commited on
Commit
4e6d28b
1 Parent(s): 3c4de7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -23
app.py CHANGED
@@ -123,46 +123,32 @@ def create_conversational_rag_chain(vectorstore):
123
  return create_rag_chain()
124
  conversational_rag_chain = create_conversational_rag_chain()
125
 
126
- def display_chat_history(chat_history):
127
- """Displays the chat history in Streamlit."""
128
- for msg in chat_history.messages:
129
- st.chat_message(msg.type).write(msg.content)
130
-
131
-
132
-
133
- def main_page(conversational_rag_chain):
134
- load_dotenv()
135
- """Main page for the Streamlit app."""
136
  msgs = st.session_state.get("chat_history", StreamlitChatMessageHistory(key="special_app_key"))
137
  chain_with_history = conversational_rag_chain
138
 
139
  st.title("Conversational RAG Chatbot")
140
 
141
- display_chat_history(msgs)
142
-
143
  if prompt := st.chat_input():
144
  st.chat_message("human").write(prompt)
145
 
 
146
  input_dict = {"input": prompt, "chat_history": msgs.messages}
147
  config = {"configurable": {"session_id": "any"}}
148
 
 
149
  response = chain_with_history.invoke(input_dict, config)
150
  st.chat_message("ai").write(response["answer"])
151
 
 
152
  if "docs" in response and response["documents"]:
153
- docs = response["documents"]
154
- def expand_document(index):
155
  st.write(f"Expanding document {index+1}...")
156
- display_documents(docs, expand_document)
157
 
 
158
  st.session_state["chat_history"] = msgs
159
 
160
-
161
- def main():
162
- """Main function for the Streamlit app with page navigation."""
163
-
164
- main_page(conversational_rag_chain)
165
-
166
-
167
  if __name__ == "__main__":
168
- main()
 
123
  return create_rag_chain()
124
  conversational_rag_chain = create_conversational_rag_chain()
125
 
126
+ def main(conversational_rag_chain):
127
+ """Main function for the Streamlit app."""
128
+ # Initialize chat history if not already present in session state
 
 
 
 
 
 
 
129
  msgs = st.session_state.get("chat_history", StreamlitChatMessageHistory(key="special_app_key"))
130
  chain_with_history = conversational_rag_chain
131
 
132
  st.title("Conversational RAG Chatbot")
133
 
 
 
134
  if prompt := st.chat_input():
135
  st.chat_message("human").write(prompt)
136
 
137
+ # Prepare the input dictionary with the correct keys
138
  input_dict = {"input": prompt, "chat_history": msgs.messages}
139
  config = {"configurable": {"session_id": "any"}}
140
 
141
+ # Process user input and handle response
142
  response = chain_with_history.invoke(input_dict, config)
143
  st.chat_message("ai").write(response["answer"])
144
 
145
+ # Display retrieved documents (if any and present in response)
146
  if "docs" in response and response["documents"]:
 
 
147
  st.write(f"Expanding document {index+1}...")
148
+ display_documents(docs, expand_document) # Pass click function
149
 
150
+ # Update chat history in session state
151
  st.session_state["chat_history"] = msgs
152
 
 
 
 
 
 
 
 
153
  if __name__ == "__main__":
154
+ main(conversational_rag_chain)