Update app.py
Browse files
app.py
CHANGED
|
@@ -164,7 +164,7 @@ def retrieve_relevant_chunks(query, top_k=3):
|
|
| 164 |
return [], []
|
| 165 |
|
| 166 |
def chat(message, history):
|
| 167 |
-
"""Chat function -
|
| 168 |
global client
|
| 169 |
|
| 170 |
# Reinitialize client if needed
|
|
@@ -201,10 +201,11 @@ def chat(message, history):
|
|
| 201 |
{"role": "system", "content": "You are a helpful assistant that answers questions based on provided context. Be concise and accurate."}
|
| 202 |
]
|
| 203 |
|
| 204 |
-
# Add history -
|
| 205 |
if history:
|
| 206 |
-
for
|
| 207 |
-
messages.append(
|
|
|
|
| 208 |
|
| 209 |
# Add current query
|
| 210 |
messages.append({
|
|
@@ -236,7 +237,6 @@ def chat(message, history):
|
|
| 236 |
'answer': answer
|
| 237 |
})
|
| 238 |
|
| 239 |
-
# Return just the string - ChatInterface handles adding to history
|
| 240 |
return full_answer
|
| 241 |
|
| 242 |
except Exception as e:
|
|
@@ -255,7 +255,7 @@ def download_history():
|
|
| 255 |
except:
|
| 256 |
return None
|
| 257 |
|
| 258 |
-
# Build interface
|
| 259 |
with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
| 260 |
|
| 261 |
gr.Markdown("""
|
|
@@ -280,10 +280,9 @@ with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
|
| 280 |
download_file = gr.File(label="Download")
|
| 281 |
|
| 282 |
with gr.Column(scale=2):
|
| 283 |
-
#
|
| 284 |
chat_interface = gr.ChatInterface(
|
| 285 |
fn=chat,
|
| 286 |
-
type="messages",
|
| 287 |
chatbot=gr.Chatbot(height=500, label="Conversation"),
|
| 288 |
textbox=gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2),
|
| 289 |
submit_btn="Ask",
|
|
|
|
| 164 |
return [], []
|
| 165 |
|
| 166 |
def chat(message, history):
|
| 167 |
+
"""Chat function - returns response string for ChatInterface"""
|
| 168 |
global client
|
| 169 |
|
| 170 |
# Reinitialize client if needed
|
|
|
|
| 201 |
{"role": "system", "content": "You are a helpful assistant that answers questions based on provided context. Be concise and accurate."}
|
| 202 |
]
|
| 203 |
|
| 204 |
+
# Add history - convert from tuples to message format
|
| 205 |
if history:
|
| 206 |
+
for user_msg, assistant_msg in history[-3:]: # Last 3 exchanges
|
| 207 |
+
messages.append({"role": "user", "content": user_msg})
|
| 208 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 209 |
|
| 210 |
# Add current query
|
| 211 |
messages.append({
|
|
|
|
| 237 |
'answer': answer
|
| 238 |
})
|
| 239 |
|
|
|
|
| 240 |
return full_answer
|
| 241 |
|
| 242 |
except Exception as e:
|
|
|
|
| 255 |
except:
|
| 256 |
return None
|
| 257 |
|
| 258 |
+
# Build interface
|
| 259 |
with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
| 260 |
|
| 261 |
gr.Markdown("""
|
|
|
|
| 280 |
download_file = gr.File(label="Download")
|
| 281 |
|
| 282 |
with gr.Column(scale=2):
|
| 283 |
+
# ChatInterface - REMOVED type="messages" parameter
|
| 284 |
chat_interface = gr.ChatInterface(
|
| 285 |
fn=chat,
|
|
|
|
| 286 |
chatbot=gr.Chatbot(height=500, label="Conversation"),
|
| 287 |
textbox=gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2),
|
| 288 |
submit_btn="Ask",
|