Spaces:
Build error
Build error
Update src/rag.py
Browse files- src/rag.py +31 -13
src/rag.py
CHANGED
|
@@ -466,21 +466,39 @@ class AACAssistant:
|
|
| 466 |
response = self.chain.invoke(
|
| 467 |
{"question": user_query, "emotion_analysis": emotion_analysis}
|
| 468 |
)
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
#
|
| 473 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
|
|
|
|
| 475 |
if marker_position != -1:
|
| 476 |
-
#
|
| 477 |
-
actual_response =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
else:
|
| 479 |
-
#
|
| 480 |
-
# This
|
| 481 |
-
print(f"
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
return actual_response
|
| 486 |
# return response["answer"]
|
|
|
|
| 466 |
response = self.chain.invoke(
|
| 467 |
{"question": user_query, "emotion_analysis": emotion_analysis}
|
| 468 |
)
|
| 469 |
+
raw_chain_output_answer = response.get("answer", "")
|
| 470 |
+
prompt_end_marker = "Please generate your response as the AAC user, following the instructions above.</s>\n<|assistant|>"
|
| 471 |
+
|
| 472 |
+
# For debugging, let's print what we're searching for and a snippet of where we're searching
|
| 473 |
+
print(f"DEBUG: process_query - Attempting to find marker: [{prompt_end_marker}]")
|
| 474 |
+
# print(f"DEBUG: process_query - Last 200 chars of raw_chain_output_answer: [...{raw_chain_output_answer[-200:]}]")
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
marker_position = raw_chain_output_answer.rfind(prompt_end_marker)
|
| 478 |
|
| 479 |
+
actual_response = ""
|
| 480 |
if marker_position != -1:
|
| 481 |
+
# If the marker is found, take everything AFTER it
|
| 482 |
+
actual_response = raw_chain_output_answer[marker_position + len(prompt_end_marker):].strip()
|
| 483 |
+
print(f"DEBUG: process_query - Marker found. Extracted response before cleaning EOS: '{actual_response}'")
|
| 484 |
+
|
| 485 |
+
# Llama 3 models often output an <|eot_id|> at the end of their turn.
|
| 486 |
+
# Let's remove this if present.
|
| 487 |
+
eot_marker = "<|eot_id|>"
|
| 488 |
+
if actual_response.endswith(eot_marker):
|
| 489 |
+
actual_response = actual_response[:-len(eot_marker)].strip()
|
| 490 |
+
print(f"DEBUG: process_query - Cleaned <|eot_id|>, final response: '{actual_response}'")
|
| 491 |
+
|
| 492 |
else:
|
| 493 |
+
# This block will be hit if the precise prompt_end_marker isn't found.
|
| 494 |
+
# This indicates a mismatch between your defined marker and the actual raw output.
|
| 495 |
+
print(f"ERROR: Precise marker [{prompt_end_marker}] NOT FOUND in raw answer.")
|
| 496 |
+
print(f"DEBUG: process_query - Raw full answer from chain (length {len(raw_chain_output_answer)}):")
|
| 497 |
+
print(f"'''{raw_chain_output_answer}'''") # Print the whole thing for analysis
|
| 498 |
+
actual_response = "Error: Could not parse the assistant's response correctly." # Or return raw_chain_output_answer for debugging in UI
|
| 499 |
+
|
| 500 |
+
# --- END OF CORRECTED PARSING LOGIC ---
|
| 501 |
+
|
| 502 |
+
print(f"DEBUG: process_query - Final extracted assistant response: '{actual_response}'")
|
| 503 |
return actual_response
|
| 504 |
# return response["answer"]
|