Ali2206 commited on
Commit
f9a6a36
·
verified ·
1 Parent(s): 67e48f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -24
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(raw_content):
41
  import logging
42
  logging.basicConfig(level=logging.INFO)
43
 
44
- if isinstance(raw_content, (dict, list)):
45
- logging.info("[extract_tool_name] Content is dict/list — using default title.")
46
- return "Tool Result", json.dumps(raw_content, indent=2)
47
-
48
- if not isinstance(raw_content, str):
49
- logging.info("[extract_tool_name] Content is not string — using default title.")
50
- return "Tool Result", str(raw_content)
51
-
52
- try:
53
- if '"name": "' in raw_content:
54
- start = raw_content.index('"name": "') + 9
55
- end = raw_content.index('"', start)
56
- tool_name = raw_content[start:end]
57
- title = f"Tool: {tool_name}"
58
- logging.info(f"[extract_tool_name] Parsed tool name: {tool_name}")
59
- else:
60
- title = "Tool Result"
61
- logging.info("[extract_tool_name] No tool name found in content.")
62
- except Exception as e:
63
- title = "Tool Result"
64
- logging.warning(f"[extract_tool_name] Failed to extract tool name: {e}")
65
-
66
- return title, raw_content.strip()
 
 
 
 
 
 
 
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