Ali2206 commited on
Commit
e9dfc7d
·
verified ·
1 Parent(s): cc12a3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -26
app.py CHANGED
@@ -42,35 +42,19 @@ question_examples = [
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)
 
74
  return f"Tool: {tool_name}", content
75
 
76
  # === Format answer in collapsible box
 
42
 
43
  # === Extract tool name and format output
44
  def extract_tool_name_and_clean_content(msg):
45
+ content_raw = msg.get("content") if isinstance(msg, dict) else getattr(msg, "content", "")
46
+
47
+ try:
48
+ parsed = json.loads(content_raw)
49
+ tool_name = parsed.get("tool_name", "Tool Result")
50
+ content = parsed.get("content", "")
51
+ except Exception:
52
+ tool_name = "Tool Result"
53
+ content = content_raw
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  if isinstance(content, (dict, list)):
56
  content = json.dumps(content, indent=2)
57
+
58
  return f"Tool: {tool_name}", content
59
 
60
  # === Format answer in collapsible box