Update app.py
Browse files
app.py
CHANGED
|
@@ -261,16 +261,20 @@ Instructions:
|
|
| 261 |
"content": system_prompt
|
| 262 |
})
|
| 263 |
|
| 264 |
-
# Add conversation history (last 3 exchanges)
|
| 265 |
if history and len(history) > 0:
|
| 266 |
-
# History is
|
| 267 |
-
# Take last
|
| 268 |
-
recent_history = history[-
|
| 269 |
-
for
|
| 270 |
-
if isinstance(
|
| 271 |
messages.append({
|
| 272 |
-
"role":
|
| 273 |
-
"content":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
})
|
| 275 |
|
| 276 |
# Add current query with context
|
|
@@ -372,7 +376,7 @@ with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
|
| 372 |
clear_msg = gr.Textbox(label="Status", interactive=False, visible=False)
|
| 373 |
|
| 374 |
with gr.Column(scale=2):
|
| 375 |
-
chatbot = gr.Chatbot(label="Conversation", height=500
|
| 376 |
query_input = gr.Textbox(
|
| 377 |
label="Ask a question",
|
| 378 |
placeholder="Type your question here and press Enter...",
|
|
@@ -399,10 +403,8 @@ with gr.Blocks(title="Enhanced RAG Chatbot") as demo:
|
|
| 399 |
# Generate answer
|
| 400 |
answer = generate_answer(question, history)
|
| 401 |
|
| 402 |
-
#
|
| 403 |
-
history
|
| 404 |
-
# Add assistant response
|
| 405 |
-
history.append({"role": "assistant", "content": answer})
|
| 406 |
|
| 407 |
return history, ""
|
| 408 |
|
|
|
|
| 261 |
"content": system_prompt
|
| 262 |
})
|
| 263 |
|
| 264 |
+
# Add conversation history (last 3 exchanges for context)
|
| 265 |
if history and len(history) > 0:
|
| 266 |
+
# History is in tuple format [[user, bot], [user, bot]]
|
| 267 |
+
# Take last 3 exchanges
|
| 268 |
+
recent_history = history[-3:] if len(history) > 3 else history
|
| 269 |
+
for exchange in recent_history:
|
| 270 |
+
if isinstance(exchange, (list, tuple)) and len(exchange) >= 2:
|
| 271 |
messages.append({
|
| 272 |
+
"role": "user",
|
| 273 |
+
"content": exchange[0]
|
| 274 |
+
})
|
| 275 |
+
messages.append({
|
| 276 |
+
"role": "assistant",
|
| 277 |
+
"content": exchange[1]
|
| 278 |
})
|
| 279 |
|
| 280 |
# Add current query with context
|
|
|
|
| 376 |
clear_msg = gr.Textbox(label="Status", interactive=False, visible=False)
|
| 377 |
|
| 378 |
with gr.Column(scale=2):
|
| 379 |
+
chatbot = gr.Chatbot(label="Conversation", height=500)
|
| 380 |
query_input = gr.Textbox(
|
| 381 |
label="Ask a question",
|
| 382 |
placeholder="Type your question here and press Enter...",
|
|
|
|
| 403 |
# Generate answer
|
| 404 |
answer = generate_answer(question, history)
|
| 405 |
|
| 406 |
+
# Append as tuple (user_msg, bot_msg) - Gradio default format
|
| 407 |
+
history = history + [[question, answer]]
|
|
|
|
|
|
|
| 408 |
|
| 409 |
return history, ""
|
| 410 |
|