Spaces:
Paused
Paused
Update app_web.py
Browse files- app_web.py +47 -33
app_web.py
CHANGED
|
@@ -173,17 +173,34 @@ def render_chat():
|
|
| 173 |
cat_cfg = cfg.get("query_categorization", {})
|
| 174 |
max_history = cat_cfg.get("max_history", 6)
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
prior_messages = st.session_state.messages[:-1]
|
| 177 |
history = [
|
| 178 |
{"role": m["role"], "content": m["content"]}
|
| 179 |
for m in prior_messages[-max_history:]
|
| 180 |
]
|
| 181 |
try:
|
| 182 |
-
qcat_result = categorize_query(
|
| 183 |
-
except Exception
|
| 184 |
-
print(e)
|
| 185 |
qcat_result = {"category": "general", "action": "continue"}
|
| 186 |
-
|
| 187 |
if qcat_result.get("category") == "contact_and_info" and qcat_result.get("action") == "contact":
|
| 188 |
with st.chat_message("assistant"):
|
| 189 |
st.markdown("For additional assistance, please contact the WEAI Helpdesk at IFPRI-WEAI@cgiar.org.\
|
|
@@ -213,8 +230,11 @@ def render_chat():
|
|
| 213 |
return
|
| 214 |
|
| 215 |
if qcat_result.get("category") == "stata":
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
file_path = ""
|
| 220 |
if qcat_result.get("action") == "do":
|
|
@@ -241,35 +261,29 @@ def render_chat():
|
|
| 241 |
|
| 242 |
if qcat_result.get("action") == "error":
|
| 243 |
options = {"explain the error":"explain",
|
| 244 |
-
"suggest fix(es) for the error":"suggestfix"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
with st.chat_message("assistant"):
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
# "capture": True if "capture" in selected_options else False,
|
| 263 |
-
# "verbose": True if "verbose" in selected_options else False,
|
| 264 |
-
"lines": None#lines if "lines" in selected_options else None
|
| 265 |
-
}
|
| 266 |
-
result = explain_(qcat_result.get("action"), prompt, cfg, file_path, opts)
|
| 267 |
-
st.session_state.messages.append({"role": "assistant", "content": result})
|
| 268 |
-
st.markdown(result)
|
| 269 |
-
return
|
| 270 |
-
else:
|
| 271 |
-
st.warning("Please select at least one option you need help with.")
|
| 272 |
-
|
| 273 |
# ββ Check if this is a clarification response βββββββββββββββββββ
|
| 274 |
pending = st.session_state.pending_clarification
|
| 275 |
if pending is not None:
|
|
|
|
| 173 |
cat_cfg = cfg.get("query_categorization", {})
|
| 174 |
max_history = cat_cfg.get("max_history", 6)
|
| 175 |
|
| 176 |
+
# ββ Check if this is a clarification response βββββββββββββββββββ
|
| 177 |
+
unresolved = st.session_state.unresolved_category
|
| 178 |
+
if unresolved is not None:
|
| 179 |
+
# This prompt is the user's clarification answer
|
| 180 |
+
# Include the clarification question for context
|
| 181 |
+
unresolved_question = st.session_state.get("unresolved_category_question", "")
|
| 182 |
+
if unresolved_question:
|
| 183 |
+
combined_cat = f"{unresolved} (Clarification: Q: {unresolved_question} A: {prompt})"
|
| 184 |
+
else:
|
| 185 |
+
combined_cat = f"{unresolved} β {prompt}"
|
| 186 |
+
st.session_state.unresolved_category_question = None
|
| 187 |
+
original_query_cat = unresolved
|
| 188 |
+
st.session_state.unresolved_category = None
|
| 189 |
+
else:
|
| 190 |
+
combined_cat = prompt
|
| 191 |
+
original_query_cat = prompt
|
| 192 |
+
st.session_state.resolution_rounds = 0
|
| 193 |
+
|
| 194 |
prior_messages = st.session_state.messages[:-1]
|
| 195 |
history = [
|
| 196 |
{"role": m["role"], "content": m["content"]}
|
| 197 |
for m in prior_messages[-max_history:]
|
| 198 |
]
|
| 199 |
try:
|
| 200 |
+
qcat_result = categorize_query(combined_cat, cfg, history)
|
| 201 |
+
except Exception:
|
|
|
|
| 202 |
qcat_result = {"category": "general", "action": "continue"}
|
| 203 |
+
|
| 204 |
if qcat_result.get("category") == "contact_and_info" and qcat_result.get("action") == "contact":
|
| 205 |
with st.chat_message("assistant"):
|
| 206 |
st.markdown("For additional assistance, please contact the WEAI Helpdesk at IFPRI-WEAI@cgiar.org.\
|
|
|
|
| 230 |
return
|
| 231 |
|
| 232 |
if qcat_result.get("category") == "stata":
|
| 233 |
+
# Ask clarification β store original query and question for context
|
| 234 |
+
st.session_state.unresolved_category = original_query_cat
|
| 235 |
+
st.session_state.messages.append(
|
| 236 |
+
{"role": "assistant", "content": clarification_msg}
|
| 237 |
+
)
|
| 238 |
|
| 239 |
file_path = ""
|
| 240 |
if qcat_result.get("action") == "do":
|
|
|
|
| 261 |
|
| 262 |
if qcat_result.get("action") == "error":
|
| 263 |
options = {"explain the error":"explain",
|
| 264 |
+
"suggest fix(es) for the error":"suggestfix"}
|
| 265 |
+
|
| 266 |
+
st.session_state.unresolved_category_question = f"Please let me know what you need help with: {'or '.join(options.keys())}. List all options that apply."
|
| 267 |
+
st.session_state.resolution_rounds += 1
|
| 268 |
+
resolution_msg = f"**Before I search, could you clarify?** Please let me know what you need help with: {'or '.join(options.keys())}. List all options that apply."
|
| 269 |
+
st.session_state.messages.append({"role": "user", "content": resolution_msg})
|
| 270 |
|
| 271 |
with st.chat_message("assistant"):
|
| 272 |
+
selection = qcat_result.get("sub-action", ["explain"])
|
| 273 |
+
selected_options = [options.get(s) for s in selection]
|
| 274 |
+
opts = {
|
| 275 |
+
"rewrite": True if "rewrite" in selected_options else False,
|
| 276 |
+
"explain": True if "explain" in selected_options else False,
|
| 277 |
+
"suggestfix": True if "suggestfix" in selected_options else False,
|
| 278 |
+
# "capture": True if "capture" in selected_options else False,
|
| 279 |
+
# "verbose": True if "verbose" in selected_options else False,
|
| 280 |
+
"lines": None#lines if "lines" in selected_options else None
|
| 281 |
+
}
|
| 282 |
+
result = explain_(qcat_result.get("action"), prompt, cfg, file_path, opts)
|
| 283 |
+
st.session_state.messages.append({"role": "assistant", "content": result})
|
| 284 |
+
st.markdown(result)
|
| 285 |
+
return
|
| 286 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
# ββ Check if this is a clarification response βββββββββββββββββββ
|
| 288 |
pending = st.session_state.pending_clarification
|
| 289 |
if pending is not None:
|