-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathevaluator.ts
More file actions
147 lines (129 loc) · 4.04 KB
/
evaluator.ts
File metadata and controls
147 lines (129 loc) · 4.04 KB
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
/**
* Core evaluation logic for tool usage assessment
*/
import { BaseMessage, AIMessage } from '@langchain/core/messages';
import { ToolUsageScenario, ToolUsageResult, ToolCall } from './types.js';
import { Runnable } from '@langchain/core/runnables';
/**
* Core evaluation logic class
*/
export class ToolUsageEvaluator {
/**
* Evaluate a single scenario
*/
async evaluateScenario(scenario: ToolUsageScenario, agent: Runnable): Promise<ToolUsageResult> {
const messages = [
{
role: 'system',
content:
"You are a document processing assistant. Use the appropriate tools to complete the user's request efficiently.",
},
{ role: 'human', content: scenario.query },
];
const result = await agent.invoke({ messages });
// Extract tool calls
const toolCalls = this.extractToolCalls(result.messages);
const actualTools = toolCalls.map(call => call.name);
// Evaluate correctness
const correctTools = this.evaluateCorrectTools(
actualTools,
scenario.expectedTools,
scenario.allowExtraTools
);
const correctOrder = this.evaluateCorrectOrder(actualTools, scenario.expectedTools);
const efficient = this.evaluateEfficiency(actualTools.length, scenario.maxToolCalls);
// Calculate score
let score = 0;
if (correctTools) score += 0.5; // 50% for using correct tools
if (correctOrder) score += 0.3; // 30% for correct order
if (efficient) score += 0.2; // 20% for efficiency
// Collect issues
const issues: string[] = [];
if (!correctTools) {
issues.push(
`Expected tools: [${scenario.expectedTools.join(', ')}], got: [${actualTools.join(', ')}]`
);
}
if (!correctOrder) {
issues.push(`Tools called in wrong order`);
}
if (!efficient) {
issues.push(`Used ${actualTools.length} tools, max allowed: ${scenario.maxToolCalls}`);
}
return {
scenarioId: scenario.id,
description: scenario.description,
correctTools,
correctOrder,
efficient,
correctParameters: true, // TODO: Implement parameter validation
expectedTools: scenario.expectedTools,
actualTools,
toolCallCount: actualTools.length,
maxAllowed: scenario.maxToolCalls || 999,
score,
issues,
};
}
/**
* Check if correct tools were used
*/
private evaluateCorrectTools(
actualTools: string[],
expectedTools: string[],
allowExtra: boolean = true
): boolean {
// Check that all expected tools were used
for (const expectedTool of expectedTools) {
if (!actualTools.includes(expectedTool)) {
return false;
}
}
// If extra tools are not allowed, check that no extra tools were used
if (!allowExtra) {
for (const actualTool of actualTools) {
if (!expectedTools.includes(actualTool)) {
return false;
}
}
}
return true;
}
/**
* Check if tools were used in correct order
*/
private evaluateCorrectOrder(actualTools: string[], expectedTools: string[]): boolean {
let expectedIndex = 0;
for (const actualTool of actualTools) {
if (expectedIndex < expectedTools.length && actualTool === expectedTools[expectedIndex]) {
expectedIndex++;
}
}
// All expected tools should have been found in order
return expectedIndex === expectedTools.length;
}
/**
* Check if tool usage was efficient
*/
private evaluateEfficiency(actualCount: number, maxAllowed?: number): boolean {
if (!maxAllowed) return true;
return actualCount <= maxAllowed;
}
/**
* Extract tool calls from agent messages
*/
private extractToolCalls(messages: BaseMessage[]): ToolCall[] {
const toolCalls: ToolCall[] = [];
for (const message of messages) {
if (message instanceof AIMessage && message.tool_calls) {
for (const toolCall of message.tool_calls) {
toolCalls.push({
name: toolCall.name,
parameters: toolCall.args || {},
});
}
}
}
return toolCalls;
}
}