|
1 |
| -export type ToolCall = { |
2 |
| - toolCallId: string; |
3 |
| - toolName: string; |
4 |
| - parameters: unknown; |
5 |
| -}; |
6 |
| -export type ExpectedToolCall = Omit<ToolCall, "toolCallId">; |
| 1 | +import diff from "microdiff"; |
| 2 | +import { ExpectedToolCall, ActualToolCall } from "./accuracy-snapshot-storage/snapshot-storage.js"; |
7 | 3 |
|
8 |
| -export function toolCallingAccuracyScorer(expectedToolCalls: ExpectedToolCall[], actualToolCalls: ToolCall[]): number { |
9 |
| - if (actualToolCalls.length < expectedToolCalls.length) { |
10 |
| - return 0; |
11 |
| - } |
12 |
| - |
13 |
| - const possibleScore = actualToolCalls.length > expectedToolCalls.length ? 0.75 : 1; |
14 |
| - const checkedToolCallIds = new Set<string>(); |
15 |
| - for (const expectedToolCall of expectedToolCalls) { |
16 |
| - const matchingActualToolCall = actualToolCalls.find( |
17 |
| - (actualToolCall) => |
18 |
| - actualToolCall.toolName === expectedToolCall.toolName && |
19 |
| - !checkedToolCallIds.has(actualToolCall.toolCallId) |
20 |
| - ); |
21 |
| - |
22 |
| - if (!matchingActualToolCall) { |
23 |
| - return 0; |
24 |
| - } |
25 |
| - |
26 |
| - checkedToolCallIds.add(matchingActualToolCall.toolCallId); |
27 |
| - } |
28 |
| - |
29 |
| - return possibleScore; |
30 |
| -} |
31 |
| - |
32 |
| -export function parameterMatchingAccuracyScorer( |
| 4 | +export function calculateToolCallingAccuracy( |
33 | 5 | expectedToolCalls: ExpectedToolCall[],
|
34 |
| - actualToolCalls: ToolCall[] |
| 6 | + actualToolCalls: ActualToolCall[] |
35 | 7 | ): number {
|
36 | 8 | if (expectedToolCalls.length === 0) {
|
37 |
| - return 1; |
| 9 | + return actualToolCalls.length === 0 ? 1 : 0.75; |
38 | 10 | }
|
39 | 11 |
|
40 |
| - const usedActualIndexes = new Set<number>(); |
41 |
| - const scores: number[] = []; |
| 12 | + const maxAccuracy = actualToolCalls.length > expectedToolCalls.length ? 0.75 : 1; |
| 13 | + |
| 14 | + const individualAccuracies: number[] = []; |
| 15 | + const checkedActualToolCallIndexes = new Set<number>(); |
42 | 16 |
|
43 | 17 | for (const expectedCall of expectedToolCalls) {
|
44 |
| - // Find all unmatched actual tool calls with the same tool name |
45 | 18 | const candidates = actualToolCalls
|
46 | 19 | .map((call, index) => ({ call, index }))
|
47 |
| - .filter(({ call, index }) => !usedActualIndexes.has(index) && call.toolName === expectedCall.toolName); |
48 |
| - |
49 |
| - if (candidates.length === 0) { |
50 |
| - scores.push(0); |
51 |
| - continue; |
52 |
| - } |
53 |
| - |
54 |
| - // Pick the candidate with the best parameter match |
55 |
| - let bestScore = -1; |
56 |
| - let bestIndex = -1; |
57 |
| - for (const { call, index } of candidates) { |
58 |
| - const score = compareParams(expectedCall.parameters, call.parameters); |
59 |
| - if (score > bestScore) { |
60 |
| - bestScore = score; |
61 |
| - bestIndex = index; |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - usedActualIndexes.add(bestIndex); |
66 |
| - scores.push(bestScore); |
67 |
| - } |
68 |
| - |
69 |
| - const totalScore = scores.reduce((sum, score) => sum + score, 0); |
70 |
| - return totalScore / scores.length; |
| 20 | + .filter( |
| 21 | + ({ call, index }) => !checkedActualToolCallIndexes.has(index) && call.toolName === expectedCall.toolName |
| 22 | + ) |
| 23 | + .map(({ call, index }) => ({ |
| 24 | + call, |
| 25 | + index, |
| 26 | + score: compareParams(expectedCall.parameters, call.parameters), |
| 27 | + })) |
| 28 | + .filter(({ score }) => score >= 0.75) |
| 29 | + .sort((a, b) => b.score - a.score); |
| 30 | + |
| 31 | + const bestMatch = candidates[0]; |
| 32 | + if (!bestMatch) { |
| 33 | + individualAccuracies.push(0); |
| 34 | + } else { |
| 35 | + checkedActualToolCallIndexes.add(bestMatch.index); |
| 36 | + const individualAccuracy = Math.min(bestMatch.score, maxAccuracy); |
| 37 | + individualAccuracies.push(individualAccuracy); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return Math.min(...individualAccuracies); |
71 | 42 | }
|
72 | 43 |
|
73 |
| -/** |
74 |
| - * Recursively compares expected and actual parameters and returns a score. |
75 |
| - * - 1: Perfect match. |
76 |
| - * - 0.75: All expected parameters are present and match, but there are extra actual parameters. |
77 |
| - * - 0: Missing parameters or mismatched values. |
78 |
| - */ |
79 |
| -function compareParams(expected: unknown, actual: unknown): number { |
80 |
| - if (expected === null || expected === undefined) { |
81 |
| - return actual === null || actual === undefined ? 1 : 0; |
82 |
| - } |
83 |
| - if (actual === null || actual === undefined) { |
84 |
| - return 0; |
85 |
| - } |
| 44 | +function compareParams(expected: Record<string, unknown>, actual: Record<string, unknown>): number { |
| 45 | + const differences = diff(expected, actual); |
86 | 46 |
|
87 |
| - if (Array.isArray(expected)) { |
88 |
| - if (!Array.isArray(actual) || actual.length < expected.length) { |
89 |
| - return 0; |
90 |
| - } |
91 |
| - let minScore = 1; |
92 |
| - for (let i = 0; i < expected.length; i++) { |
93 |
| - minScore = Math.min(minScore, compareParams(expected[i], actual[i])); |
94 |
| - } |
95 |
| - if (minScore === 0) { |
96 |
| - return 0; |
97 |
| - } |
98 |
| - if (actual.length > expected.length) { |
99 |
| - minScore = Math.min(minScore, 0.75); |
100 |
| - } |
101 |
| - return minScore; |
| 47 | + if (differences.length === 0) { |
| 48 | + return 1; |
102 | 49 | }
|
103 | 50 |
|
104 |
| - if (typeof expected === "object") { |
105 |
| - if (typeof actual !== "object" || Array.isArray(actual)) { |
106 |
| - return 0; |
107 |
| - } |
108 |
| - const expectedKeys = Object.keys(expected as Record<string, unknown>); |
109 |
| - const actualKeys = Object.keys(actual as Record<string, unknown>); |
110 |
| - |
111 |
| - let minScore = 1; |
112 |
| - for (const key of expectedKeys) { |
113 |
| - if (!Object.prototype.hasOwnProperty.call(actual, key)) { |
114 |
| - return 0; |
115 |
| - } |
116 |
| - minScore = Math.min( |
117 |
| - minScore, |
118 |
| - compareParams((expected as Record<string, unknown>)[key], (actual as Record<string, unknown>)[key]) |
119 |
| - ); |
120 |
| - } |
| 51 | + const hasOnlyAdditions = differences.every((d) => d.type === "CREATE"); |
| 52 | + const hasRemovals = differences.some((d) => d.type === "REMOVE"); |
| 53 | + const hasChanges = differences.some((d) => d.type === "CHANGE"); |
121 | 54 |
|
122 |
| - if (minScore === 0) { |
123 |
| - return 0; |
124 |
| - } |
125 |
| - |
126 |
| - if (actualKeys.length > expectedKeys.length) { |
127 |
| - minScore = Math.min(minScore, 0.75); |
128 |
| - } |
129 |
| - return minScore; |
| 55 | + if (hasOnlyAdditions && !hasRemovals && !hasChanges) { |
| 56 | + return 0.75; |
130 | 57 | }
|
131 | 58 |
|
132 |
| - return expected == actual ? 1 : 0; |
| 59 | + return 0; |
133 | 60 | }
|
0 commit comments