Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -101,39 +101,39 @@ if "OPENAI_API_KEY" in os.environ:
|
|
| 101 |
return retriever, document_chain
|
| 102 |
|
| 103 |
# Load components
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
| 107 |
# Initialize memory for each session
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
# Chat interface
|
| 112 |
-
|
| 113 |
|
| 114 |
# Initialize chat history
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
# Display chat messages from history on app rerun
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
|
| 123 |
# React to user input
|
| 124 |
-
|
| 125 |
-
|
| 126 |
st.chat_message("user").markdown(prompt)
|
| 127 |
-
|
| 128 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 129 |
-
|
| 130 |
with st.chat_message("assistant"):
|
| 131 |
message_placeholder = st.empty()
|
| 132 |
-
|
| 133 |
-
|
| 134 |
docs = retriever.get_relevant_documents(prompt)
|
| 135 |
-
|
| 136 |
-
|
| 137 |
response = document_chain.invoke(
|
| 138 |
{
|
| 139 |
"context": docs,
|
|
@@ -142,17 +142,17 @@ if "OPENAI_API_KEY" in os.environ:
|
|
| 142 |
HumanMessage(content=prompt)
|
| 143 |
],
|
| 144 |
}
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
else:
|
| 158 |
st.warning("Please enter your OpenAI API Key in the sidebar to start the chatbot.")
|
|
|
|
| 101 |
return retriever, document_chain
|
| 102 |
|
| 103 |
# Load components
|
| 104 |
+
with st.spinner("Initializing Assistant..."):
|
| 105 |
+
retriever, document_chain = initialize_components()
|
| 106 |
|
| 107 |
# Initialize memory for each session
|
| 108 |
+
if "memory" not in st.session_state:
|
| 109 |
+
st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 110 |
|
| 111 |
# Chat interface
|
| 112 |
+
st.subheader("Chat AI Assistant")
|
| 113 |
|
| 114 |
# Initialize chat history
|
| 115 |
+
if "messages" not in st.session_state:
|
| 116 |
+
st.session_state.messages = []
|
| 117 |
|
| 118 |
# Display chat messages from history on app rerun
|
| 119 |
+
for message in st.session_state.messages:
|
| 120 |
+
with st.chat_message(message["role"]):
|
| 121 |
+
st.markdown(message["content"])
|
| 122 |
|
| 123 |
# React to user input
|
| 124 |
+
if prompt := st.chat_input("What would you like to know about Document?"):
|
| 125 |
+
# Display user message in chat message container
|
| 126 |
st.chat_message("user").markdown(prompt)
|
| 127 |
+
# Add user message to chat history
|
| 128 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 129 |
+
|
| 130 |
with st.chat_message("assistant"):
|
| 131 |
message_placeholder = st.empty()
|
| 132 |
+
|
| 133 |
+
# Retrieve relevant documents
|
| 134 |
docs = retriever.get_relevant_documents(prompt)
|
| 135 |
+
|
| 136 |
+
# Generate response
|
| 137 |
response = document_chain.invoke(
|
| 138 |
{
|
| 139 |
"context": docs,
|
|
|
|
| 142 |
HumanMessage(content=prompt)
|
| 143 |
],
|
| 144 |
}
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# The response is already a string, so we can use it directly
|
| 148 |
+
full_response = response
|
| 149 |
+
message_placeholder.markdown(full_response)
|
| 150 |
+
|
| 151 |
+
# Add assistant response to chat history
|
| 152 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 153 |
+
|
| 154 |
+
# Update memory
|
| 155 |
+
st.session_state.memory.save_context({"input": prompt}, {"output": full_response})
|
| 156 |
|
| 157 |
else:
|
| 158 |
st.warning("Please enter your OpenAI API Key in the sidebar to start the chatbot.")
|