likhonsheikh commited on
Commit
9436565
·
verified ·
1 Parent(s): 7ef800a

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -429,14 +429,33 @@ def parse_thinking_response(text: str) -> tuple:
429
  def parse_tool_use(text: str) -> Optional[Dict[str, Any]]:
430
  """Parse tool use from model response"""
431
  try:
432
- # Look for JSON tool call pattern
433
- json_pattern = r'\{[^{}]*"tool"[^{}]*\}'
434
- matches = re.findall(json_pattern, text, re.DOTALL)
435
- if matches:
436
- for match in matches:
437
- parsed = json.loads(match)
438
- if "tool" in parsed:
439
- return parsed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
  except:
441
  pass
442
  return None
 
429
  def parse_tool_use(text: str) -> Optional[Dict[str, Any]]:
430
  """Parse tool use from model response"""
431
  try:
432
+ # Try to find JSON with "tool" key - handle nested braces
433
+ # First try: the entire text might be JSON
434
+ text_stripped = text.strip()
435
+ if text_stripped.startswith("{") and text_stripped.endswith("}"):
436
+ parsed = json.loads(text_stripped)
437
+ if "tool" in parsed:
438
+ return parsed
439
+
440
+ # Second try: find JSON object containing "tool"
441
+ brace_count = 0
442
+ start_idx = None
443
+ for i, char in enumerate(text):
444
+ if char == '{':
445
+ if brace_count == 0:
446
+ start_idx = i
447
+ brace_count += 1
448
+ elif char == '}':
449
+ brace_count -= 1
450
+ if brace_count == 0 and start_idx is not None:
451
+ json_str = text[start_idx:i+1]
452
+ try:
453
+ parsed = json.loads(json_str)
454
+ if "tool" in parsed:
455
+ return parsed
456
+ except:
457
+ pass
458
+ start_idx = None
459
  except:
460
  pass
461
  return None