ZENLLC commited on
Commit
24761a4
·
verified ·
1 Parent(s): 681132c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -16
app.py CHANGED
@@ -303,6 +303,9 @@ def build_knowledge_base(
303
  - Fallback to simple HTTP fetch if Firecrawl not available
304
  - Raw text
305
  - Files
 
 
 
306
  """
307
  api_key = (api_key or "").strip()
308
  if not api_key:
@@ -355,17 +358,21 @@ def chat_with_rag(
355
  api_key: str,
356
  kb: List[Dict[str, Any]],
357
  system_prompt: str,
358
- history: List[Dict[str, str]],
359
  ):
 
 
 
 
360
  user_message = (user_message or "").strip()
361
  api_key = (api_key or "").strip()
362
  system_prompt = (system_prompt or "").strip()
363
 
364
  if not user_message:
365
- return history, history, "❌ Please enter a question."
366
 
367
  if not api_key:
368
- return history, history, "❌ Please save your OpenAI API key first."
369
 
370
  if not system_prompt:
371
  system_prompt = DEFAULT_SYSTEM_PROMPT
@@ -394,11 +401,11 @@ def chat_with_rag(
394
  )
395
  messages.append({"role": "system", "content": context_block})
396
 
397
- # Add truncated history
398
- recent_history = history[-10:] if history else []
399
- for msg in recent_history:
400
- if msg.get("role") in ("user", "assistant"):
401
- messages.append(msg)
402
 
403
  # Current user message
404
  messages.append({"role": "user", "content": user_message})
@@ -410,20 +417,18 @@ def chat_with_rag(
410
  messages=messages,
411
  max_completion_tokens=900, # GPT-5-compatible param
412
  )
413
- answer = resp.choices[0].message.content
414
  except Exception as e:
415
  answer = f"⚠️ OpenAI API error: {e}"
416
 
417
- new_history = history + [
418
- {"role": "user", "content": user_message},
419
- {"role": "assistant", "content": answer},
420
- ]
421
 
422
  return new_history, new_history, debug_retrieval
423
 
424
 
425
  def clear_chat():
426
- return [], [], ""
427
 
428
 
429
  # -------------------- UI LAYOUT --------------------
@@ -444,7 +449,7 @@ with gr.Blocks(title="RAG Chatbot — GPT-5 + URLs / Files / Text + Firecrawl")
444
  api_key_state = gr.State("")
445
  firecrawl_key_state = gr.State("")
446
  kb_state = gr.State([])
447
- chat_state = gr.State([])
448
 
449
  # default preset on load -> ZEN
450
  default_preset_name = "ZEN Sites Deep QA (zenai.world + AI Arena)"
@@ -515,9 +520,9 @@ with gr.Blocks(title="RAG Chatbot — GPT-5 + URLs / Files / Text + Firecrawl")
515
  with gr.Column(scale=2):
516
  gr.Markdown("### 💬 RAG Chat")
517
 
 
518
  chatbot = gr.Chatbot(
519
  label="RAG Chatbot (GPT-5)",
520
- type="messages",
521
  height=450,
522
  )
523
 
 
303
  - Fallback to simple HTTP fetch if Firecrawl not available
304
  - Raw text
305
  - Files
306
+
307
+ Note: api_key is kept in the signature for symmetry and potential future use,
308
+ but not required for lexical-only indexing.
309
  """
310
  api_key = (api_key or "").strip()
311
  if not api_key:
 
358
  api_key: str,
359
  kb: List[Dict[str, Any]],
360
  system_prompt: str,
361
+ history_pairs: List[List[str]],
362
  ):
363
+ """
364
+ history_pairs: list of [user_str, assistant_str] pairs for the UI Chatbot.
365
+ We'll rebuild OpenAI messages from this each time.
366
+ """
367
  user_message = (user_message or "").strip()
368
  api_key = (api_key or "").strip()
369
  system_prompt = (system_prompt or "").strip()
370
 
371
  if not user_message:
372
+ return history_pairs, history_pairs, "❌ Please enter a question."
373
 
374
  if not api_key:
375
+ return history_pairs, history_pairs, "❌ Please save your OpenAI API key first."
376
 
377
  if not system_prompt:
378
  system_prompt = DEFAULT_SYSTEM_PROMPT
 
401
  )
402
  messages.append({"role": "system", "content": context_block})
403
 
404
+ # Rebuild conversation history from pairs (last few turns)
405
+ recent_pairs = history_pairs[-5:] if history_pairs else []
406
+ for u, a in recent_pairs:
407
+ messages.append({"role": "user", "content": u})
408
+ messages.append({"role": "assistant", "content": a})
409
 
410
  # Current user message
411
  messages.append({"role": "user", "content": user_message})
 
417
  messages=messages,
418
  max_completion_tokens=900, # GPT-5-compatible param
419
  )
420
+ answer = resp.choices[0].message.content or ""
421
  except Exception as e:
422
  answer = f"⚠️ OpenAI API error: {e}"
423
 
424
+ # Update UI history as list of [user, assistant] pairs
425
+ new_history = history_pairs + [[user_message, answer]]
 
 
426
 
427
  return new_history, new_history, debug_retrieval
428
 
429
 
430
  def clear_chat():
431
+ return [], [], "Chat cleared."
432
 
433
 
434
  # -------------------- UI LAYOUT --------------------
 
449
  api_key_state = gr.State("")
450
  firecrawl_key_state = gr.State("")
451
  kb_state = gr.State([])
452
+ chat_state = gr.State([]) # list of [user, assistant] pairs
453
 
454
  # default preset on load -> ZEN
455
  default_preset_name = "ZEN Sites Deep QA (zenai.world + AI Arena)"
 
520
  with gr.Column(scale=2):
521
  gr.Markdown("### 💬 RAG Chat")
522
 
523
+ # Classic Chatbot format: list of [user, assistant] pairs
524
  chatbot = gr.Chatbot(
525
  label="RAG Chatbot (GPT-5)",
 
526
  height=450,
527
  )
528