-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorCorrectionService.ts
More file actions
393 lines (332 loc) · 12.2 KB
/
Copy pathErrorCorrectionService.ts
File metadata and controls
393 lines (332 loc) · 12.2 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
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
import AgentModel from '../models/AgentModel';
import AgentLogModel from '../models/AgentLogModel';
import TestCaseModel from '../models/TestCaseModel';
import AgentOrchestrationService from './AgentOrchestrationService';
class ErrorCorrectionService {
// Error thresholds for intervention
private readonly CONSECUTIVE_ERROR_THRESHOLD = 3;
private readonly ERROR_PATTERN_THRESHOLD = 0.7; // 70% similarity
async detectErrors(agentId: string, input: string, output: string, expectedOutput?: string): Promise<any> {
try {
// Get the agent
const agent = await AgentModel.findById(agentId);
if (!agent) {
throw new Error(`Agent with ID ${agentId} not found`);
}
// Check for obvious errors in the output
const obviousErrors = this.checkForObviousErrors(output);
// If we have an expected output, compare with actual output
let comparisonErrors = [];
if (expectedOutput) {
comparisonErrors = this.compareOutputs(output, expectedOutput);
}
// Check for consecutive errors
const consecutiveErrors = await this.checkConsecutiveErrors(agentId);
// Combine all detected errors
const allErrors = [
...obviousErrors.map(e => ({ type: 'obvious', ...e })),
...comparisonErrors.map(e => ({ type: 'comparison', ...e })),
...(consecutiveErrors ? [{ type: 'consecutive', message: `${consecutiveErrors} consecutive errors detected` }] : [])
];
// Log the errors if any were found
if (allErrors.length > 0) {
await AgentLogModel.create({
agentId,
level: 'error',
message: `Errors detected in agent response: ${allErrors.map(e => e.message).join('; ')}`,
metadata: {
input,
output,
expectedOutput,
errors: allErrors
}
});
}
return {
hasErrors: allErrors.length > 0,
errors: allErrors,
needsIntervention: consecutiveErrors >= this.CONSECUTIVE_ERROR_THRESHOLD || allErrors.length > 2
};
} catch (error) {
console.error('Error detecting errors:', error);
throw error;
}
}
private checkForObviousErrors(output: string): any[] {
const errors = [];
// Check for empty or extremely short responses
if (!output || output.trim().length < 5) {
errors.push({
message: 'Empty or extremely short response',
severity: 'high'
});
}
// Check for error messages in the output
const errorPatterns = [
'error',
'exception',
'failed',
'unable to',
'cannot',
'invalid'
];
const lowercaseOutput = output.toLowerCase();
for (const pattern of errorPatterns) {
if (lowercaseOutput.includes(pattern)) {
// Check if it's actually an error message and not just using the word
const context = this.getErrorContext(lowercaseOutput, pattern);
if (this.isLikelyErrorMessage(context)) {
errors.push({
message: `Potential error message detected: "${context}"`,
severity: 'medium',
context
});
}
}
}
// Check for incomplete responses
if (output.endsWith('...') ||
output.includes('I'll continue') ||
output.includes('to be continued')) {
errors.push({
message: 'Incomplete response detected',
severity: 'low'
});
}
return errors;
}
private getErrorContext(text: string, errorWord: string): string {
const index = text.indexOf(errorWord);
if (index === -1) return '';
// Get 50 characters before and after the error word
const start = Math.max(0, index - 50);
const end = Math.min(text.length, index + errorWord.length + 50);
return text.substring(start, end);
}
private isLikelyErrorMessage(context: string): boolean {
// This is a simplified heuristic - in a real implementation,
// this would use more sophisticated NLP techniques
const errorPhrases = [
'an error occurred',
'error:',
'failed to',
'exception:',
'unable to process',
'cannot complete',
'invalid input'
];
return errorPhrases.some(phrase => context.includes(phrase));
}
private compareOutputs(actual: string, expected: string): any[] {
const errors = [];
// Simple string similarity check
const similarity = this.calculateStringSimilarity(actual, expected);
if (similarity < 0.3) { // Less than 30% similar
errors.push({
message: 'Output significantly different from expected',
severity: 'high',
similarity: similarity
});
} else if (similarity < 0.7) { // Between 30% and 70% similar
errors.push({
message: 'Output partially different from expected',
severity: 'medium',
similarity: similarity
});
}
// Check for missing key information
// This is a simplified approach - a real implementation would use
// more sophisticated NLP techniques to identify key information
const expectedWords = new Set(expected.toLowerCase().split(/\s+/));
const actualWords = new Set(actual.toLowerCase().split(/\s+/));
const missingWords = [...expectedWords].filter(word =>
word.length > 5 && !actualWords.has(word) // Only consider "significant" words
);
if (missingWords.length > 5) {
errors.push({
message: 'Key information missing from output',
severity: 'medium',
missingWords: missingWords.slice(0, 10) // Limit to 10 examples
});
}
return errors;
}
private calculateStringSimilarity(s1: string, s2: string): number {
// Simplified Jaccard similarity for strings
const set1 = new Set(s1.toLowerCase().split(/\s+/));
const set2 = new Set(s2.toLowerCase().split(/\s+/));
const intersection = new Set([...set1].filter(x => set2.has(x)));
const union = new Set([...set1, ...set2]);
return intersection.size / union.size;
}
private async checkConsecutiveErrors(agentId: string): Promise<number> {
// Get recent logs for this agent
const recentLogs = await AgentLogModel.find({
agentId,
level: 'error',
timestamp: { $gte: new Date(Date.now() - 1 * 60 * 60 * 1000) } // Last hour
})
.sort({ timestamp: -1 })
.limit(10);
// Count consecutive errors
let consecutiveCount = 0;
for (const log of recentLogs) {
if (log.level === 'error') {
consecutiveCount++;
} else {
break; // Stop counting when we hit a non-error log
}
}
return consecutiveCount;
}
async correctErrors(agentId: string, input: string, errorOutput: string, errorInfo: any): Promise<string> {
try {
// Get the agent
const agent = await AgentModel.findById(agentId);
if (!agent) {
throw new Error(`Agent with ID ${agentId} not found`);
}
// Log the correction attempt
await AgentLogModel.create({
agentId,
level: 'info',
message: `Attempting to correct errors in agent response`,
metadata: {
input,
errorOutput,
errorInfo
}
});
// Create a modified prompt that includes error information
const correctionPrompt = this.createCorrectionPrompt(input, errorOutput, errorInfo);
// Use the agent orchestration service to process the corrected request
// We're using a modified agent config with lower temperature for more reliable output
const modifiedConfig = {
...agent.toObject(),
properties: agent.properties.map((p: any) => {
if (p.id === 'temperature') {
return { ...p, value: Math.max(0.1, (p.value as number) - 0.3) };
}
return p;
})
};
const correctedOutput = await AgentOrchestrationService.processAgentRequest(
agentId,
correctionPrompt,
modifiedConfig
);
// Log the successful correction
await AgentLogModel.create({
agentId,
level: 'info',
message: `Successfully corrected errors in agent response`,
metadata: {
input,
errorOutput,
correctedOutput
}
});
return correctedOutput;
} catch (error) {
console.error('Error correcting errors:', error);
// Log the failed correction attempt
await AgentLogModel.create({
agentId,
level: 'error',
message: `Failed to correct errors in agent response: ${error.message}`,
metadata: {
input,
errorOutput,
errorInfo
}
});
// Return a fallback response
return `I apologize, but I encountered an issue while processing your request. Please try again or rephrase your question.`;
}
}
private createCorrectionPrompt(input: string, errorOutput: string, errorInfo: any): string {
return `The following is a user request that resulted in an error or inadequate response.
User request: "${input}"
Previous response (with issues): "${errorOutput}"
Issues detected: ${errorInfo.errors.map((e: any) => e.message).join('; ')}
Please provide a corrected and improved response to the user's original request. Ensure your response is complete, accurate, and addresses all aspects of the request.`;
}
async runTestCases(agentId: string): Promise<any> {
try {
// Get all test cases for this agent
const testCases = await TestCaseModel.find({ agentId });
const results = [];
let passCount = 0;
// Run each test case
for (const testCase of testCases) {
// Update test case status to running
testCase.status = 'running';
await testCase.save();
try {
// Get the agent
const agent = await AgentModel.findById(agentId);
if (!agent) {
throw new Error(`Agent with ID ${agentId} not found`);
}
// Process the test input
const output = await AgentOrchestrationService.processAgentRequest(
agentId,
testCase.input,
agent
);
// Check for errors
const errorCheck = await this.detectErrors(agentId, testCase.input, output, testCase.expectedOutput);
// Update test case with results
testCase.actualOutput = output;
testCase.status = errorCheck.hasErrors ? 'failure' : 'success';
await testCase.save();
// Track results
if (!errorCheck.hasErrors) {
passCount++;
}
results.push({
testCaseId: testCase.id,
input: testCase.input,
expectedOutput: testCase.expectedOutput,
actualOutput: output,
success: !errorCheck.hasErrors,
errors: errorCheck.errors
});
} catch (error) {
console.error(`Error running test case ${testCase.id}:`, error);
// Update test case as failed
testCase.status = 'failure';
testCase.actualOutput = `Error: ${error.message}`;
await testCase.save();
results.push({
testCaseId: testCase.id,
input: testCase.input,
expectedOutput: testCase.expectedOutput,
actualOutput: `Error: ${error.message}`,
success: false,
errors: [{ type: 'execution', message: error.message }]
});
}
}
// Calculate error rate
const errorRate = testCases.length > 0
? ((testCases.length - passCount) / testCases.length) * 100
: 0;
// Update agent error rate
if (testCases.length > 0) {
await AgentModel.findByIdAndUpdate(agentId, { errorRate });
}
return {
totalTests: testCases.length,
passCount,
failCount: testCases.length - passCount,
errorRate,
results
};
} catch (error) {
console.error('Error running test cases:', error);
throw error;
}
}
}
export default new ErrorCorrectionService();