SyedZainAliShah commited on
Commit
4edc0b4
·
verified ·
1 Parent(s): 86c5bb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
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 already in messages format [{"role": "user", "content": "..."}, ...]
267
- # Take last 6 messages (3 user + 3 assistant pairs)
268
- recent_history = history[-6:] if len(history) > 6 else history
269
- for msg in recent_history:
270
- if isinstance(msg, dict) and "role" in msg and "content" in msg:
271
  messages.append({
272
- "role": msg["role"],
273
- "content": msg["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, type="messages")
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
- # Add user message
403
- history.append({"role": "user", "content": question})
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