Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -712,78 +712,78 @@ Your goal: Provide the EXACT answer in the EXACT format requested.
|
|
| 712 |
# This ensures the *next* run of this node is *still* Turn 1.
|
| 713 |
return {"messages": [ai_message, correction_message], "turn": 1}
|
| 714 |
# --- END OF RULE ENFORCEMENT BLOCK ---
|
| 715 |
-
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
re.DOTALL | re.IGNORECASE
|
| 731 |
-
)
|
| 732 |
-
|
| 733 |
-
if func_match:
|
| 734 |
-
try:
|
| 735 |
-
tool_name = func_match.group(1).strip()
|
| 736 |
-
json_str = func_match.group(2)
|
| 737 |
-
tool_input = json.loads(json_str)
|
| 738 |
-
print(f"🔧 Fallback (Format 1): Parsed tool call for '{tool_name}'")
|
| 739 |
-
except json.JSONDecodeError as e:
|
| 740 |
-
print(f"⚠️ Fallback (Format 1): Failed to parse JSON: {e}")
|
| 741 |
-
tool_name = None # Reset
|
| 742 |
-
|
| 743 |
-
# 2. If Format 1 failed, try to parse bare JSON (old fallback)
|
| 744 |
-
if not tool_name:
|
| 745 |
-
json_match = re.search(
|
| 746 |
-
r"```(?:json)?\s*(\{.*?\})\s*```|(\{.*?\})",
|
| 747 |
-
content,
|
| 748 |
re.DOTALL | re.IGNORECASE
|
| 749 |
)
|
| 750 |
-
|
| 751 |
-
|
| 752 |
try:
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
tool_name = parsed_json.get("tool")
|
| 758 |
-
tool_input = parsed_json.get("tool_input", {})
|
| 759 |
-
elif "code" in parsed_json: # Guess code_interpreter
|
| 760 |
-
tool_name = "code_interpreter"
|
| 761 |
-
tool_input = parsed_json
|
| 762 |
-
elif "answer" in parsed_json: # Guess final_answer
|
| 763 |
-
tool_name = "final_answer_tool"
|
| 764 |
-
tool_input = parsed_json
|
| 765 |
-
|
| 766 |
-
if tool_name:
|
| 767 |
-
print(f"🔧 Fallback (Format 2): Parsed tool call for '{tool_name}'")
|
| 768 |
-
|
| 769 |
except json.JSONDecodeError as e:
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 787 |
|
| 788 |
|
| 789 |
# --- Logging ---
|
|
|
|
| 712 |
# This ensures the *next* run of this node is *still* Turn 1.
|
| 713 |
return {"messages": [ai_message, correction_message], "turn": 1}
|
| 714 |
# --- END OF RULE ENFORCEMENT BLOCK ---
|
| 715 |
+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
| 716 |
+
# --- FIX #2: REPLACE THE FALLBACK PARSING BLOCK ---
|
| 717 |
+
#
|
| 718 |
+
# --- Fallback Parsing ---
|
| 719 |
+
# Check if LLM failed to format tool call and put it in 'content'
|
| 720 |
+
if not ai_message.tool_calls and isinstance(ai_message.content, str) and ai_message.content.strip():
|
| 721 |
+
content = ai_message.content
|
| 722 |
+
tool_name = None
|
| 723 |
+
tool_input = None
|
| 724 |
+
|
| 725 |
+
# 1. Try to parse the new <function(tool_name)>{json}</function> format
|
| 726 |
+
# Note: We look for </function> optionally, as it might be truncated
|
| 727 |
+
func_match = re.search(
|
| 728 |
+
r"<function\(([^)]+)\)>(\{.*?\})(?:</function>)?",
|
| 729 |
+
content,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
re.DOTALL | re.IGNORECASE
|
| 731 |
)
|
| 732 |
+
|
| 733 |
+
if func_match:
|
| 734 |
try:
|
| 735 |
+
tool_name = func_match.group(1).strip()
|
| 736 |
+
json_str = func_match.group(2)
|
| 737 |
+
tool_input = json.loads(json_str)
|
| 738 |
+
print(f"🔧 Fallback (Format 1): Parsed tool call for '{tool_name}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
except json.JSONDecodeError as e:
|
| 740 |
+
print(f"⚠️ Fallback (Format 1): Failed to parse JSON: {e}")
|
| 741 |
+
tool_name = None # Reset
|
| 742 |
+
|
| 743 |
+
# 2. If Format 1 failed, try to parse bare JSON (old fallback)
|
| 744 |
+
if not tool_name:
|
| 745 |
+
json_match = re.search(
|
| 746 |
+
r"```(?:json)?\s*(\{.*?\})\s*```|(\{.*?\})",
|
| 747 |
+
content,
|
| 748 |
+
re.DOTALL | re.IGNORECASE
|
| 749 |
+
)
|
| 750 |
+
if json_match:
|
| 751 |
+
json_str = json_match.group(1) or json_match.group(2)
|
| 752 |
+
try:
|
| 753 |
+
parsed_json = json.loads(json_str)
|
| 754 |
+
# This format is less structured; we guess tool from keys
|
| 755 |
+
if isinstance(parsed_json, dict):
|
| 756 |
+
if "tool" in parsed_json and "tool_input" in parsed_json:
|
| 757 |
+
tool_name = parsed_json.get("tool")
|
| 758 |
+
tool_input = parsed_json.get("tool_input", {})
|
| 759 |
+
elif "code" in parsed_json: # Guess code_interpreter
|
| 760 |
+
tool_name = "code_interpreter"
|
| 761 |
+
tool_input = parsed_json
|
| 762 |
+
elif "answer" in parsed_json: # Guess final_answer
|
| 763 |
+
tool_name = "final_answer_tool"
|
| 764 |
+
tool_input = parsed_json
|
| 765 |
+
|
| 766 |
+
if tool_name:
|
| 767 |
+
print(f"🔧 Fallback (Format 2): Parsed tool call for '{tool_name}'")
|
| 768 |
+
|
| 769 |
+
except json.JSONDecodeError as e:
|
| 770 |
+
print(f"⚠️ Fallback (Format 2): Failed to parse JSON: {e}")
|
| 771 |
+
|
| 772 |
+
# --- If any fallback parser succeeded, build the tool call ---
|
| 773 |
+
if tool_name and tool_input is not None and any(t.name == tool_name for t in self.tools):
|
| 774 |
+
print(f"🔧 Fallback SUCCESS: Rebuilding tool call for '{tool_name}'")
|
| 775 |
+
tool_call = ToolCall(
|
| 776 |
+
name=tool_name,
|
| 777 |
+
args=tool_input,
|
| 778 |
+
id=str(uuid.uuid4())
|
| 779 |
+
)
|
| 780 |
+
ai_message.tool_calls = [tool_call]
|
| 781 |
+
ai_message.content = "" # Clear content field
|
| 782 |
+
|
| 783 |
+
elif not tool_name:
|
| 784 |
+
print(f"⚠️ Fallback FAILED: Could not parse any tool call from content:\n{content[:200]}...")
|
| 785 |
+
# --- END OF REPLACEMENT BLOCK ---
|
| 786 |
+
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
| 787 |
|
| 788 |
|
| 789 |
# --- Logging ---
|