File size: 16,253 Bytes
566d03e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
"""
AST-Based Function Call Evaluator
=================================
Evaluates model predictions against ground truth using AST-based matching.
"""
import json
import re
from typing import Any, Dict, List, Optional, Tuple, Union
from dataclasses import dataclass
from .arabic_utils import ArabicNormalizer
@dataclass
class EvaluationResult:
"""Result of evaluating a single sample."""
sample_id: str
category: str
is_correct: bool
score: float
details: Dict[str, Any]
class ArabicASTEvaluator:
"""
AST-based evaluator for Arabic function calling.
Supports multiple evaluation modes:
- exact: Exact match of function name and all arguments
- relaxed: Allows minor variations in argument values
- function_only: Only checks if correct function was called
"""
def __init__(self, mode: str = "exact"):
self.mode = mode
self.normalizer = ArabicNormalizer()
def parse_function_call(self, response: str) -> Optional[Dict]:
"""
Parse a function call from model response.
Handles multiple formats:
- JSON: {"name": "func", "arguments": {...}}
- OpenAI style: {"function_call": {"name": "func", "arguments": "..."}}
- Plain text: func(arg1, arg2)
"""
if not response:
return None
response = response.strip()
# Try JSON format first
try:
data = json.loads(response)
if isinstance(data, dict):
# Direct format
if 'name' in data and 'arguments' in data:
args = data['arguments']
if isinstance(args, str):
args = json.loads(args)
return {'name': data['name'], 'arguments': args}
# OpenAI format
if 'function_call' in data:
fc = data['function_call']
args = fc.get('arguments', {})
if isinstance(args, str):
args = json.loads(args)
return {'name': fc['name'], 'arguments': args}
# Tool calls format
if 'tool_calls' in data and data['tool_calls']:
tc = data['tool_calls'][0]
func = tc.get('function', tc)
args = func.get('arguments', {})
if isinstance(args, str):
args = json.loads(args)
return {'name': func['name'], 'arguments': args}
except (json.JSONDecodeError, KeyError, TypeError):
pass
# Try extracting JSON from text
json_match = re.search(r'\{[^{}]*"name"[^{}]*\}', response, re.DOTALL)
if json_match:
try:
data = json.loads(json_match.group())
if 'name' in data:
args = data.get('arguments', data.get('parameters', {}))
if isinstance(args, str):
args = json.loads(args)
return {'name': data['name'], 'arguments': args}
except (json.JSONDecodeError, KeyError):
pass
# Try plain text function call format: func(args)
func_match = re.match(r'(\w+)\s*\((.*)\)', response, re.DOTALL)
if func_match:
name = func_match.group(1)
args_str = func_match.group(2).strip()
try:
# Try parsing as JSON
if args_str.startswith('{'):
args = json.loads(args_str)
else:
# Parse as key=value pairs
args = {}
for pair in args_str.split(','):
if '=' in pair:
k, v = pair.split('=', 1)
args[k.strip()] = self._parse_value(v.strip())
return {'name': name, 'arguments': args}
except:
pass
return None
def parse_multiple_calls(self, response: str) -> List[Dict]:
"""Parse multiple function calls from response."""
calls = []
if not response:
return calls
# Try JSON array
try:
data = json.loads(response)
if isinstance(data, list):
for item in data:
parsed = self.parse_function_call(json.dumps(item))
if parsed:
calls.append(parsed)
return calls
elif isinstance(data, dict) and 'tool_calls' in data:
for tc in data['tool_calls']:
func = tc.get('function', tc)
args = func.get('arguments', {})
if isinstance(args, str):
args = json.loads(args)
calls.append({'name': func['name'], 'arguments': args})
return calls
except (json.JSONDecodeError, KeyError, TypeError):
pass
# Try finding multiple JSON objects
json_pattern = r'\{[^{}]*"name"[^{}]*\}'
matches = re.findall(json_pattern, response, re.DOTALL)
for match in matches:
parsed = self.parse_function_call(match)
if parsed:
calls.append(parsed)
# If no calls found, try single call
if not calls:
single = self.parse_function_call(response)
if single:
calls.append(single)
return calls
def _parse_value(self, value: str) -> Any:
"""Parse a string value to appropriate type."""
value = value.strip().strip('"\'')
# Try numeric
try:
if '.' in value:
return float(value)
return int(value)
except ValueError:
pass
# Boolean
if value.lower() in ('true', 'false'):
return value.lower() == 'true'
# None
if value.lower() in ('none', 'null'):
return None
return value
def normalize_value(self, value: Any) -> Any:
"""Normalize a value for comparison."""
if isinstance(value, str):
return self.normalizer.normalize(value)
if isinstance(value, (list, tuple)):
return [self.normalize_value(v) for v in value]
if isinstance(value, dict):
return {k: self.normalize_value(v) for k, v in value.items()}
return value
def compare_arguments(
self,
predicted: Dict[str, Any],
expected: Dict[str, Any],
strict: bool = True
) -> Tuple[bool, float, Dict]:
"""
Compare predicted arguments against expected.
Returns: (is_match, score, details)
"""
if not expected:
return len(predicted) == 0, 1.0 if len(predicted) == 0 else 0.0, {}
details = {'matched': [], 'mismatched': [], 'missing': [], 'extra': []}
expected_keys = set(expected.keys())
predicted_keys = set(predicted.keys())
# Check for missing and extra keys
missing = expected_keys - predicted_keys
extra = predicted_keys - expected_keys
details['missing'] = list(missing)
details['extra'] = list(extra)
# Compare common keys
common_keys = expected_keys & predicted_keys
matched_count = 0
for key in common_keys:
exp_val = self.normalize_value(expected[key])
pred_val = self.normalize_value(predicted[key])
if exp_val == pred_val:
details['matched'].append(key)
matched_count += 1
else:
# Try numeric comparison with tolerance
if isinstance(exp_val, (int, float)) and isinstance(pred_val, (int, float)):
if abs(exp_val - pred_val) < 0.001:
details['matched'].append(key)
matched_count += 1
continue
details['mismatched'].append({
'key': key,
'expected': expected[key],
'predicted': predicted[key]
})
# Calculate score
total_expected = len(expected_keys)
if strict:
# All must match, no extras
is_match = (matched_count == total_expected and len(extra) == 0)
score = matched_count / max(total_expected, len(predicted_keys)) if predicted_keys else 0.0
else:
# Partial credit
is_match = matched_count == total_expected
score = matched_count / total_expected if total_expected > 0 else 1.0
return is_match, score, details
def evaluate_single_call(
self,
predicted: Optional[Dict],
expected: Dict
) -> EvaluationResult:
"""Evaluate a single function call prediction."""
if predicted is None:
return EvaluationResult(
sample_id="",
category="",
is_correct=False,
score=0.0,
details={'error': 'Failed to parse prediction'}
)
# Check function name
pred_name = self.normalizer.normalize(predicted.get('name', ''))
exp_name = self.normalizer.normalize(expected.get('name', ''))
if pred_name != exp_name:
return EvaluationResult(
sample_id="",
category="",
is_correct=False,
score=0.0,
details={
'error': 'Function name mismatch',
'expected_name': expected.get('name'),
'predicted_name': predicted.get('name')
}
)
# Compare arguments
pred_args = predicted.get('arguments', {})
exp_args = expected.get('arguments', {})
is_match, score, details = self.compare_arguments(
pred_args, exp_args, strict=(self.mode == 'exact')
)
return EvaluationResult(
sample_id="",
category="",
is_correct=is_match,
score=score,
details=details
)
def evaluate_parallel_calls(
self,
predicted: List[Dict],
expected: List[Dict]
) -> EvaluationResult:
"""
Evaluate parallel function calls (order-agnostic).
Uses bipartite matching for optimal pairing.
"""
if len(predicted) == 0 and len(expected) == 0:
return EvaluationResult(
sample_id="",
category="",
is_correct=True,
score=1.0,
details={'matched_calls': 0}
)
if len(predicted) == 0:
return EvaluationResult(
sample_id="",
category="",
is_correct=False,
score=0.0,
details={'error': 'No predictions', 'expected_count': len(expected)}
)
# Build score matrix
scores = []
for pred in predicted:
row = []
for exp in expected:
result = self.evaluate_single_call(pred, exp)
row.append(result.score)
scores.append(row)
# Greedy matching (could use Hungarian algorithm for optimal)
matched = 0
total_score = 0.0
used_expected = set()
match_details = []
for i, pred in enumerate(predicted):
best_j = -1
best_score = -1
for j, exp in enumerate(expected):
if j not in used_expected and scores[i][j] > best_score:
best_score = scores[i][j]
best_j = j
if best_j >= 0 and best_score > 0:
used_expected.add(best_j)
total_score += best_score
if best_score == 1.0:
matched += 1
match_details.append({
'predicted': pred,
'matched_to': expected[best_j],
'score': best_score
})
# Calculate overall score
max_possible = max(len(predicted), len(expected))
avg_score = total_score / max_possible if max_possible > 0 else 0.0
is_correct = (matched == len(expected) and len(predicted) == len(expected))
return EvaluationResult(
sample_id="",
category="",
is_correct=is_correct,
score=avg_score,
details={
'matched_calls': matched,
'expected_count': len(expected),
'predicted_count': len(predicted),
'matches': match_details
}
)
def evaluate_irrelevance(
self,
predicted: Union[str, Dict, List],
expected_no_call: bool = True
) -> EvaluationResult:
"""
Evaluate irrelevance detection (should not call any function).
"""
# Check if model made any function calls
if isinstance(predicted, str):
calls = self.parse_multiple_calls(predicted)
elif isinstance(predicted, list):
calls = predicted
elif isinstance(predicted, dict):
calls = [predicted] if 'name' in predicted else []
else:
calls = []
made_call = len(calls) > 0
if expected_no_call:
is_correct = not made_call
score = 1.0 if is_correct else 0.0
details = {
'expected': 'no_call',
'actual': 'call_made' if made_call else 'no_call',
'calls_made': calls
}
else:
is_correct = made_call
score = 1.0 if is_correct else 0.0
details = {
'expected': 'call_required',
'actual': 'call_made' if made_call else 'no_call'
}
return EvaluationResult(
sample_id="",
category="irrelevance",
is_correct=is_correct,
score=score,
details=details
)
def evaluate(
self,
sample: Dict,
prediction: str
) -> EvaluationResult:
"""
Main evaluation entry point.
Dispatches to appropriate evaluator based on category.
"""
category = sample.get('category', 'simple')
sample_id = sample.get('id', '')
# Parse ground truth
ground_truth = sample.get('ground_truth')
if isinstance(ground_truth, str) and ground_truth:
try:
ground_truth = json.loads(ground_truth)
except json.JSONDecodeError:
ground_truth = None
# Handle irrelevance
if category == 'irrelevance':
result = self.evaluate_irrelevance(prediction, expected_no_call=True)
result.sample_id = sample_id
return result
# Parse prediction
if category in ('parallel', 'parallel_multiple'):
pred_calls = self.parse_multiple_calls(prediction)
if ground_truth and 'calls' in ground_truth:
exp_calls = ground_truth['calls']
else:
exp_calls = []
result = self.evaluate_parallel_calls(pred_calls, exp_calls)
else:
pred_call = self.parse_function_call(prediction)
if ground_truth:
if 'calls' in ground_truth and ground_truth['calls']:
exp_call = ground_truth['calls'][0]
else:
exp_call = ground_truth
else:
# No ground truth available
result = EvaluationResult(
sample_id=sample_id,
category=category,
is_correct=False,
score=0.0,
details={'error': 'No ground truth available'}
)
return result
result = self.evaluate_single_call(pred_call, exp_call)
result.sample_id = sample_id
result.category = category
return result
|