manabb commited on
Commit
7be439f
·
verified ·
1 Parent(s): 9ef27b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -89,19 +89,37 @@ def chat(message, history):
89
  return history, ""
90
  #============starting extract_docx_text
91
  def respond(message, history):
92
- answer = qa_chain.invoke(message)
93
- docs = retriever.invoke(message)
94
- refs = [f"Page {d.metadata.get('page', 'N/A')}" for d in docs]
95
- full_answer = f"{answer}\n\n**References:**\n" + "\n".join(refs)
96
 
97
- # CRITICAL: Append ONLY pure dicts - no metadata, tuples, or extras
98
- new_history = history + [ # Or history.append() then return history
99
- {"role": "user", "content": message},
100
- {"role": "assistant", "content": full_answer}
101
- ]
102
-
103
- # Clear input
104
- return "", new_history # Return cleared msg, updated history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  #====================
106
  def extract_docx_text(file_path):
107
  doc = Document(file_path)
 
89
  return history, ""
90
  #============starting extract_docx_text
91
  def respond(message, history):
92
+ word_count = len(message.strip().split())
 
 
 
93
 
94
+ # If less than 3 words, do not call LLM, just ask user to clarify
95
+ if word_count < 3:
96
+ correction_msg = "Please **clarify** or expand your question (at least 3 words)."
97
+ new_history = history + [
98
+ {"role": "user", "content": message},
99
+ {"role": "assistant", "content": correction_msg},
100
+ ]
101
+ return "", new_history
102
+ else:
103
+ answer = qa_chain.invoke(message)
104
+ docs = retriever.invoke(message)
105
+ refs = [f"Page {d.metadata.get('page', 'N/A')}" for d in docs]
106
+ full_answer = f"{answer}\n\n**References:**\n" + "\n".join(refs)
107
+
108
+ # CRITICAL: Append ONLY pure dicts - no metadata, tuples, or extras
109
+ new_history = history + [ # Or history.append() then return history
110
+ {"role": "user", "content": message},
111
+ {"role": "assistant", "content": full_answer}
112
+ ]
113
+ history_string = "\n".join([
114
+ f"{item['role']}: {item['content']}"
115
+ for item in new_history
116
+ ])
117
+ # Clear input
118
+ try:
119
+ update_log("\nFrom Chat: "+datetime.now().isoformat()+"\n"+history_string+"\n")
120
+ except Exception as ee:
121
+ print(f"Error: {ee} - not saved the log")
122
+ return "", new_history # Return cleared msg, updated history
123
  #====================
124
  def extract_docx_text(file_path):
125
  doc = Document(file_path)