Update app.py
Browse files
app.py
CHANGED
@@ -37,33 +37,40 @@ question_examples = [
|
|
37 |
]
|
38 |
|
39 |
# === Helper: extract tool name from content
|
40 |
-
def extract_tool_name_and_clean_content(
|
41 |
import logging
|
42 |
logging.basicConfig(level=logging.INFO)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
# === Helper: formatted collapsible output
|
|
|
37 |
]
|
38 |
|
39 |
# === Helper: extract tool name from content
|
40 |
+
def extract_tool_name_and_clean_content(message_obj):
|
41 |
import logging
|
42 |
logging.basicConfig(level=logging.INFO)
|
43 |
|
44 |
+
tool_name = "Tool Result"
|
45 |
+
content = ""
|
46 |
+
|
47 |
+
if isinstance(message_obj, dict):
|
48 |
+
role = message_obj.get("role", "assistant")
|
49 |
+
content = message_obj.get("content", "")
|
50 |
+
tool_calls = message_obj.get("tool_calls", None)
|
51 |
+
else:
|
52 |
+
role = getattr(message_obj, "role", "assistant")
|
53 |
+
content = getattr(message_obj, "content", "")
|
54 |
+
tool_calls = getattr(message_obj, "tool_calls", None)
|
55 |
+
|
56 |
+
# Try to extract tool name from `tool_calls`
|
57 |
+
if tool_calls:
|
58 |
+
try:
|
59 |
+
if isinstance(tool_calls, str):
|
60 |
+
import json
|
61 |
+
tool_calls = json.loads(tool_calls)
|
62 |
+
tool_name = tool_calls[0].get("name", "Tool Result")
|
63 |
+
logging.info(f"[extract_tool_name] Extracted from tool_calls: {tool_name}")
|
64 |
+
except Exception as e:
|
65 |
+
logging.warning(f"[extract_tool_name] Failed tool_calls parsing: {e}")
|
66 |
+
|
67 |
+
# Format clean output
|
68 |
+
if isinstance(content, (dict, list)):
|
69 |
+
formatted = json.dumps(content, indent=2)
|
70 |
+
else:
|
71 |
+
formatted = str(content)
|
72 |
+
|
73 |
+
return f"Tool: {tool_name}", formatted
|
74 |
|
75 |
|
76 |
# === Helper: formatted collapsible output
|