Update app.py
Browse files
app.py
CHANGED
@@ -42,18 +42,32 @@ question_examples = [
|
|
42 |
|
43 |
# === Extract tool name and format output
|
44 |
def extract_tool_name_and_clean_content(msg):
|
|
|
|
|
45 |
tool_name = "Tool Result"
|
46 |
content = msg.get("content") if isinstance(msg, dict) else getattr(msg, "content", "")
|
47 |
tool_calls = msg.get("tool_calls") if isinstance(msg, dict) else getattr(msg, "tool_calls", None)
|
48 |
|
|
|
49 |
if tool_calls:
|
50 |
try:
|
51 |
if isinstance(tool_calls, str):
|
52 |
tool_calls = json.loads(tool_calls)
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
-
logging.warning(f"[extract_tool_name] Failed
|
57 |
|
58 |
if isinstance(content, (dict, list)):
|
59 |
content = json.dumps(content, indent=2)
|
|
|
42 |
|
43 |
# === Extract tool name and format output
|
44 |
def extract_tool_name_and_clean_content(msg):
|
45 |
+
import re
|
46 |
+
|
47 |
tool_name = "Tool Result"
|
48 |
content = msg.get("content") if isinstance(msg, dict) else getattr(msg, "content", "")
|
49 |
tool_calls = msg.get("tool_calls") if isinstance(msg, dict) else getattr(msg, "tool_calls", None)
|
50 |
|
51 |
+
# Try to extract tool name from tool_calls JSON
|
52 |
if tool_calls:
|
53 |
try:
|
54 |
if isinstance(tool_calls, str):
|
55 |
tool_calls = json.loads(tool_calls)
|
56 |
+
if isinstance(tool_calls, list) and tool_calls:
|
57 |
+
tool_name = tool_calls[0].get("name", "Tool Result")
|
58 |
+
except Exception as e:
|
59 |
+
logging.warning(f"[extract_tool_name] Failed tool_calls parsing: {e}")
|
60 |
+
|
61 |
+
# Try fallback: extract from [TOOL_CALLS] JSON inside raw content
|
62 |
+
if "TOOL_CALLS" in str(content):
|
63 |
+
try:
|
64 |
+
match = re.search(r"\[TOOL_CALLS\](\[.*?\])", str(content))
|
65 |
+
if match:
|
66 |
+
embedded = json.loads(match.group(1))
|
67 |
+
if isinstance(embedded, list) and embedded:
|
68 |
+
tool_name = embedded[0].get("name", "Tool Result")
|
69 |
except Exception as e:
|
70 |
+
logging.warning(f"[extract_tool_name] Failed TOOL_CALLS content parse: {e}")
|
71 |
|
72 |
if isinstance(content, (dict, list)):
|
73 |
content = json.dumps(content, indent=2)
|