-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaLearningService.ts
More file actions
462 lines (385 loc) · 15.7 KB
/
Copy pathMetaLearningService.ts
File metadata and controls
462 lines (385 loc) · 15.7 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
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
import { OpenAI } from 'langchain/llms/openai';
import { PromptTemplate } from 'langchain/prompts';
import { LLMChain } from 'langchain/chains';
import AgentModel from '../models/AgentModel';
import AgentLogModel from '../models/AgentLogModel';
import AgentPerformanceMetricModel from '../models/AgentPerformanceMetricModel';
import KnowledgeBaseService from './KnowledgeBaseService';
class MetaLearningService {
private model: OpenAI;
constructor() {
this.model = new OpenAI({
modelName: 'gpt-4',
temperature: 0.2,
openAIApiKey: process.env.OPENAI_API_KEY
});
}
async analyzeSystemPerformance(): Promise<any> {
try {
// Get all agents
const agents = await AgentModel.find({});
// Get performance metrics for all agents
const allMetrics = [];
for (const agent of agents) {
const metrics = await AgentPerformanceMetricModel.find({ agentId: agent.id })
.sort({ timestamp: -1 })
.limit(100);
allMetrics.push({
agentId: agent.id,
agentName: agent.name,
agentType: agent.type,
metrics
});
}
// Analyze global patterns
const globalPatterns = this.analyzeGlobalPatterns(allMetrics);
// Identify cross-agent learning opportunities
const learningOpportunities = await this.identifyCrossAgentLearning(allMetrics);
return {
globalPatterns,
learningOpportunities,
timestamp: new Date()
};
} catch (error) {
console.error('Error analyzing system performance:', error);
throw error;
}
}
private analyzeGlobalPatterns(allMetricsData: any[]): any {
// Calculate average metrics across all agents
let totalErrorRate = 0;
let totalResponseTime = 0;
let totalMemoryUsage = 0;
let totalMetricsCount = 0;
// Group agents by type
const agentsByType: Record<string, any[]> = {};
for (const agentData of allMetricsData) {
const { agentType, metrics } = agentData;
if (!agentsByType[agentType]) {
agentsByType[agentType] = [];
}
agentsByType[agentType].push(agentData);
for (const metric of metrics) {
totalErrorRate += metric.errorRate;
totalResponseTime += metric.responseTime;
totalMemoryUsage += metric.memoryUsage;
totalMetricsCount++;
}
}
// Calculate global averages
const globalAverages = totalMetricsCount > 0 ? {
errorRate: totalErrorRate / totalMetricsCount,
responseTime: totalResponseTime / totalMetricsCount,
memoryUsage: totalMemoryUsage / totalMetricsCount
} : {
errorRate: 0,
responseTime: 0,
memoryUsage: 0
};
// Calculate averages by agent type
const averagesByType: Record<string, any> = {};
for (const [type, agentsData] of Object.entries(agentsByType)) {
let typeErrorRate = 0;
let typeResponseTime = 0;
let typeMemoryUsage = 0;
let typeMetricsCount = 0;
for (const agentData of agentsData) {
for (const metric of agentData.metrics) {
typeErrorRate += metric.errorRate;
typeResponseTime += metric.responseTime;
typeMemoryUsage += metric.memoryUsage;
typeMetricsCount++;
}
}
averagesByType[type] = typeMetricsCount > 0 ? {
errorRate: typeErrorRate / typeMetricsCount,
responseTime: typeResponseTime / typeMetricsCount,
memoryUsage: typeMemoryUsage / typeMetricsCount,
agentCount: agentsData.length
} : {
errorRate: 0,
responseTime: 0,
memoryUsage: 0,
agentCount: agentsData.length
};
}
// Identify best performing agent types
const bestPerformingType = Object.entries(averagesByType)
.sort(([, a], [, b]) => a.errorRate - b.errorRate)[0]?.[0] || null;
// Identify worst performing agent types
const worstPerformingType = Object.entries(averagesByType)
.sort(([, a], [, b]) => b.errorRate - a.errorRate)[0]?.[0] || null;
return {
globalAverages,
averagesByType,
bestPerformingType,
worstPerformingType
};
}
private async identifyCrossAgentLearning(allMetricsData: any[]): Promise<any[]> {
// Get successful agents (low error rate)
const successfulAgents = allMetricsData
.filter(agentData => {
if (agentData.metrics.length === 0) return false;
const avgErrorRate = agentData.metrics.reduce((sum: number, m: any) => sum + m.errorRate, 0) / agentData.metrics.length;
return avgErrorRate < 3; // Less than 3% error rate
})
.map(agentData => ({
agentId: agentData.agentId,
agentName: agentData.agentName,
agentType: agentData.agentType
}));
// Get struggling agents (high error rate)
const strugglingAgents = allMetricsData
.filter(agentData => {
if (agentData.metrics.length === 0) return false;
const avgErrorRate = agentData.metrics.reduce((sum: number, m: any) => sum + m.errorRate, 0) / agentData.metrics.length;
return avgErrorRate > 8; // More than 8% error rate
})
.map(agentData => ({
agentId: agentData.agentId,
agentName: agentData.agentName,
agentType: agentData.agentType
}));
// If we have both successful and struggling agents, identify learning opportunities
if (successfulAgents.length > 0 && strugglingAgents.length > 0) {
const learningOpportunities = [];
for (const strugglingAgent of strugglingAgents) {
// Find successful agents of the same type
const successfulPeers = successfulAgents.filter(a => a.agentType === strugglingAgent.agentType);
if (successfulPeers.length > 0) {
// For each successful peer, extract knowledge that could help the struggling agent
for (const successfulPeer of successfulPeers) {
const knowledge = await this.extractTransferableKnowledge(successfulPeer.agentId, strugglingAgent.agentId);
if (knowledge) {
learningOpportunities.push({
sourceAgentId: successfulPeer.agentId,
sourceAgentName: successfulPeer.agentName,
targetAgentId: strugglingAgent.agentId,
targetAgentName: strugglingAgent.agentName,
agentType: strugglingAgent.agentType,
transferableKnowledge: knowledge
});
}
}
}
}
return learningOpportunities;
}
return [];
}
private async extractTransferableKnowledge(sourceAgentId: string, targetAgentId: string): Promise<any> {
try {
// Get configurations for both agents
const sourceAgent = await AgentModel.findById(sourceAgentId);
const targetAgent = await AgentModel.findById(targetAgentId);
if (!sourceAgent || !targetAgent) {
return null;
}
// Get successful interactions from source agent
const successfulLogs = await AgentLogModel.find({
agentId: sourceAgentId,
level: 'info',
// Only get logs that indicate successful responses
message: { $regex: /successfully processed/i }
})
.sort({ timestamp: -1 })
.limit(20);
if (successfulLogs.length === 0) {
return null;
}
// Extract key differences in configuration
const configDifferences = this.compareAgentConfigurations(sourceAgent, targetAgent);
// Use LLM to analyze what makes the successful agent effective
const template = `You are an AI meta-learning system analyzing what makes one AI agent more successful than another.
Source Agent: ${sourceAgent.name} (Type: ${sourceAgent.type})
- Configuration: ${JSON.stringify(sourceAgent.properties)}
- Error Rate: ${sourceAgent.errorRate}%
Target Agent: ${targetAgent.name} (Type: ${targetAgent.type})
- Configuration: ${JSON.stringify(targetAgent.properties)}
- Error Rate: ${targetAgent.errorRate}%
Key Configuration Differences:
${configDifferences.map(diff => `- ${diff.property}: Source=${diff.sourceValue}, Target=${diff.targetValue}`).join('\n')}
Examples of successful interactions from the source agent:
${successfulLogs.map(log => `- ${log.message}`).join('\n')}
Based on this information, identify:
1. What specific configuration parameters from the source agent should be transferred to the target agent?
2. What patterns or strategies make the source agent successful that could be applied to the target agent?
3. Provide specific, actionable recommendations for improving the target agent.
Format your response as a JSON object with the following structure:
{
"configTransfers": [
{"property": "property_name", "value": "recommended_value", "reason": "explanation"}
],
"strategies": [
{"name": "strategy_name", "description": "strategy_description", "implementation": "how_to_implement"}
],
"recommendations": [
{"description": "recommendation", "priority": "high/medium/low"}
]
}`;
const promptTemplate = new PromptTemplate({
template,
inputVariables: []
});
const chain = new LLMChain({
llm: this.model,
prompt: promptTemplate
});
const result = await chain.call({});
// Parse the JSON response
try {
const knowledge = JSON.parse(result.text);
return knowledge;
} catch (error) {
console.error('Error parsing LLM response as JSON:', error);
return null;
}
} catch (error) {
console.error('Error extracting transferable knowledge:', error);
return null;
}
}
private compareAgentConfigurations(sourceAgent: any, targetAgent: any): any[] {
const differences = [];
// Compare properties
const sourceProps = sourceAgent.properties || [];
const targetProps = targetAgent.properties || [];
// Create maps for easier comparison
const sourcePropMap = new Map(sourceProps.map((p: any) => [p.id, p.value]));
const targetPropMap = new Map(targetProps.map((p: any) => [p.id, p.value]));
// Check all source properties
for (const [propId, sourceValue] of sourcePropMap.entries()) {
if (targetPropMap.has(propId)) {
const targetValue = targetPropMap.get(propId);
// If values are different, record the difference
if (sourceValue !== targetValue) {
differences.push({
property: propId,
sourceValue,
targetValue
});
}
} else {
// Property exists in source but not in target
differences.push({
property: propId,
sourceValue,
targetValue: 'undefined'
});
}
}
// Check for properties in target that aren't in source
for (const [propId, targetValue] of targetPropMap.entries()) {
if (!sourcePropMap.has(propId)) {
differences.push({
property: propId,
sourceValue: 'undefined',
targetValue
});
}
}
return differences;
}
async applyMetaLearning(learningOpportunity: any): Promise<boolean> {
try {
const { targetAgentId, transferableKnowledge } = learningOpportunity;
// Get the target agent
const targetAgent = await AgentModel.findById(targetAgentId);
if (!targetAgent) {
throw new Error(`Target agent with ID ${targetAgentId} not found`);
}
// Apply configuration transfers
let modified = false;
if (transferableKnowledge.configTransfers && transferableKnowledge.configTransfers.length > 0) {
for (const transfer of transferableKnowledge.configTransfers) {
const propertyIndex = targetAgent.properties.findIndex((p: any) => p.id === transfer.property);
if (propertyIndex >= 0) {
// Update existing property
targetAgent.properties[propertyIndex].value = transfer.value;
modified = true;
} else {
// Add new property if it doesn't exist
targetAgent.properties.push({
id: transfer.property,
name: transfer.property, // Use property ID as name
type: typeof transfer.value === 'boolean' ? 'boolean' :
typeof transfer.value === 'number' ? 'number' : 'text',
value: transfer.value
});
modified = true;
}
}
}
// Store strategies in the knowledge base
if (transferableKnowledge.strategies && transferableKnowledge.strategies.length > 0) {
for (const strategy of transferableKnowledge.strategies) {
await KnowledgeBaseService.storeAgentMemory(
targetAgentId,
`strategy:${strategy.name}`,
strategy
);
modified = true;
}
}
// Save the modified agent
if (modified) {
await targetAgent.save();
// Log the meta-learning application
await AgentLogModel.create({
agentId: targetAgentId,
level: 'info',
message: `Applied meta-learning from cross-agent knowledge transfer`,
metadata: {
sourceAgentId: learningOpportunity.sourceAgentId,
sourceAgentName: learningOpportunity.sourceAgentName,
appliedTransfers: transferableKnowledge.configTransfers,
appliedStrategies: transferableKnowledge.strategies.map((s: any) => s.name)
}
});
}
return modified;
} catch (error) {
console.error('Error applying meta-learning:', error);
return false;
}
}
async improveSystemPrompts(): Promise<any> {
try {
// Get all agents
const agents = await AgentModel.find({});
const results = [];
for (const agent of agents) {
// Skip agents with low error rates
if (agent.errorRate < 3) continue;
// Get recent logs for this agent
const recentLogs = await AgentLogModel.find({ agentId: agent.id })
.sort({ timestamp: -1 })
.limit(50);
if (recentLogs.length < 10) continue; // Need enough data
// Extract successful and failed interactions
const successfulInteractions = recentLogs.filter(log =>
log.level === 'info' && log.message.includes('successfully processed')
);
const failedInteractions = recentLogs.filter(log =>
log.level === 'error'
);
if (successfulInteractions.length < 3 || failedInteractions.length < 3) continue;
// Use LLM to generate improved prompts
const template = `You are an AI prompt engineering expert. Your task is to analyze successful and failed interactions for an AI agent and suggest improvements to its prompting strategy.
Agent Type: ${agent.type}
Current Configuration: ${JSON.stringify(agent.properties)}
Examples of SUCCESSFUL interactions:
${successfulInteractions.slice(0, 5).map((log, i) => `${i+1}. ${log.message}`).join('\n')}
Examples of FAILED interactions:
${failedInteractions.slice(0, 5).map((log, i) => `${i+1}. ${log.message}`).join('\n')}
Based on this information:
1. Identify patterns in successful vs. failed interactions
2. Suggest specific improvements to the agent's prompting strategy
3. Provide a new, improved prompt template that would help the agent be more successful
Format your response as a JSON object with the following structure:
{
"analysis": "Your analysis of patterns in successful vs. failed interactions",
"improvements": [
{"description": "Specific improvement
(Content truncated due to size limit. Use line ranges to read in chunks)