-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsystematic_optimization.js
More file actions
604 lines (496 loc) Β· 26.2 KB
/
systematic_optimization.js
File metadata and controls
604 lines (496 loc) Β· 26.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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// Systematic Prompt and Workflow Optimization Script
// Following the user's exact methodology
import dotenv from 'dotenv';
dotenv.config();
import { query } from './utils/query.js';
import axios from 'axios';
import { readFile, writeFile } from 'fs/promises';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Configuration
const BASE_URL = `http://localhost:${process.env.PORT || 3000}`;
const TEST_USER_ID = 'optimization_test_user';
const TEST_AUTH_TOKEN = 'Bearer optimization_test_token';
// Axios instance for API calls
const api = axios.create({
baseURL: BASE_URL,
timeout: 30000,
headers: {
'Content-Type': 'application/json',
'Authorization': TEST_AUTH_TOKEN,
'X-User-ID': TEST_USER_ID
}
});
console.log('π SYSTEMATIC PROMPT AND WORKFLOW OPTIMIZATION');
console.log('π Following exact user methodology');
console.log('=' .repeat(70));
class OptimizationEngine {
constructor() {
this.iterationCount = 0;
this.maxIterations = 10; // Prevent infinite loops
this.optimizationResults = [];
this.currentPromptVersions = {};
}
// Step 1: Generate message in style of updateChat() function
generateTestMessage() {
console.log('\nπ Step 1: Generating test message in updateChat() style...');
// Based on updateChat() function structure
const testMessages = [
{
message: "I've been feeling overwhelmed with work stress lately and keep procrastinating on important projects. This seems to be a recurring pattern in my life. Can you help me understand what's happening and organize my thoughts better?",
selected: "feeling overwhelmed with work stress lately and keep procrastinating on important projects",
description: "Complex emotional and behavioral pattern analysis"
},
{
message: "Yesterday I had another argument with my partner about household responsibilities. I notice this happens whenever I'm stressed at work - I seem to take it out on the people closest to me. I want to break this cycle but don't know how.",
selected: "take it out on the people closest to me",
description: "Relationship pattern recognition and emotional regulation"
},
{
message: "I've been journaling about my anxiety before public speaking for months now. Looking at my entries, I see the same fears coming up repeatedly - fear of judgment, fear of making mistakes, imposter syndrome. I need help identifying patterns and creating an action plan.",
selected: "fear of judgment, fear of making mistakes, imposter syndrome",
description: "Long-term pattern analysis with action planning"
}
];
return testMessages[this.iterationCount % testMessages.length];
}
// Step 2: Use query() function and wait patiently for response
async sendQueryAndWait(testMessage) {
console.log('\nπ Step 2: Sending query and waiting patiently for response...');
console.log(`π Test Message: "${testMessage.message.substring(0, 100)}..."`);
console.log(`π― Focus Area: "${testMessage.selected}"`);
console.log(`π Test Type: ${testMessage.description}`);
let retryCount = 0;
const maxRetries = 3;
while (retryCount < maxRetries) {
try {
console.log(`β³ Attempt ${retryCount + 1}/${maxRetries} - Processing with AI agents...`);
// Construct message in updateChat() style
let requestMessage = testMessage.message;
if (testMessage.selected) {
requestMessage += `\n\nfollowing is the selected context that I would like to reference (the selected context is what I would like to emphasize. If you need to read something, feel free to read anything I provided with you. But if you need to add, remove, or replace something, that should be inside my selected context.): {\n${testMessage.selected}\n}`;
}
// Add optimization context
requestMessage += `\n\nOptimization Context: This is iteration ${this.iterationCount + 1} of systematic optimization. Please provide comprehensive analysis with backend actions and journal modifications as appropriate.`;
const startTime = Date.now();
const result = await Promise.race([
query(requestMessage, {
userId: TEST_USER_ID,
authToken: TEST_AUTH_TOKEN
}),
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Query timeout after 45 seconds')), 45000)
)
]);
const endTime = Date.now();
const responseTime = endTime - startTime;
console.log(`β
Response received in ${responseTime}ms`);
return {
success: true,
result,
responseTime,
requestMessage,
testMessage
};
} catch (error) {
retryCount++;
console.error(`β Attempt ${retryCount} failed: ${error.message}`);
if (retryCount < maxRetries) {
console.log(`β° Waiting 5 seconds before retry...`);
await new Promise(resolve => setTimeout(resolve, 5000));
} else {
console.error(`π₯ All ${maxRetries} attempts failed. Moving to next iteration.`);
return { success: false, error: error.message };
}
}
}
}
// Step 3: Analyze response quality and check journal modifications
async analyzeResponseAndCheckModifications(queryResponse) {
console.log('\nπ Step 3: Analyzing response quality and checking journal modifications...');
if (!queryResponse.success) {
console.log('β Query failed, skipping analysis');
return { needsOptimization: true, reason: 'Query failure' };
}
const { result, responseTime, testMessage } = queryResponse;
// Extract response content
let responseContent = '';
let hasStructuredData = false;
let structuredActions = [];
if (typeof result === 'object') {
responseContent = result.combined_response || result.output_text || JSON.stringify(result);
hasStructuredData = responseContent.includes('<!--STRUCTURED_DATA_START-->');
if (hasStructuredData) {
try {
const structuredMatch = responseContent.match(/<!--STRUCTURED_DATA_START-->([\s\S]*?)<!--STRUCTURED_DATA_END-->/);
if (structuredMatch) {
const structuredData = JSON.parse(structuredMatch[1]);
structuredActions = structuredData.actions || [];
}
} catch (e) {
console.log('β οΈ Failed to parse structured data');
}
}
} else {
responseContent = result;
hasStructuredData = responseContent.includes('<!--STRUCTURED_DATA_START-->');
}
// Quality Analysis
const qualityAnalysis = this.analyzeResponseQuality(responseContent, testMessage, responseTime);
console.log('\nπ QUALITY ANALYSIS RESULTS:');
console.log(`β‘ Response Time: ${responseTime}ms (${responseTime < 10000 ? 'EXCELLENT' : responseTime < 20000 ? 'GOOD' : 'SLOW'})`);
console.log(`π― Overall Quality Score: ${qualityAnalysis.overallScore}/10`);
console.log(`π§ Emotional Intelligence: ${qualityAnalysis.emotionalIntelligence ? 'β
' : 'β'}`);
console.log(`π€ Personalization: ${qualityAnalysis.personalization ? 'β
' : 'β'}`);
console.log(`π‘ Actionable Insights: ${qualityAnalysis.actionableInsights ? 'β
' : 'β'}`);
console.log(`π Pattern Recognition: ${qualityAnalysis.patternRecognition ? 'β
' : 'β'}`);
console.log(`π Backend Actions: ${hasStructuredData ? 'β
' : 'β'} (${structuredActions.length} actions)`);
// Check for journal modifications
let journalModifications = [];
const journalCreateActions = structuredActions.filter(action =>
action.tool === 'createJournal' || action.action === 'create_journal'
);
const journalUpdateActions = structuredActions.filter(action =>
action.tool === 'updateJournal' || action.action === 'update_journal'
);
if (journalCreateActions.length > 0 || journalUpdateActions.length > 0) {
console.log('\nπ JOURNAL MODIFICATIONS DETECTED:');
console.log(`π Created: ${journalCreateActions.length} journals`);
console.log(`βοΈ Updated: ${journalUpdateActions.length} journals`);
// Check journal versions (simulated - would need actual journal IDs)
journalModifications = await this.checkJournalModifications(structuredActions);
}
// Determine if optimization is needed
const needsOptimization = this.determineOptimizationNeed(qualityAnalysis, responseTime, journalModifications);
return {
needsOptimization,
qualityAnalysis,
responseTime,
hasStructuredData,
structuredActions,
journalModifications,
responseContent: responseContent.substring(0, 500) + '...'
};
}
// Quality analysis with world-class standards
analyzeResponseQuality(response, testMessage, responseTime) {
const analysis = {
overallScore: 0,
emotionalIntelligence: false,
personalization: false,
actionableInsights: false,
patternRecognition: false,
clarity: false,
empathy: false,
comprehensiveness: false
};
const responseLower = response.toLowerCase();
// Emotional Intelligence (2 points)
const emotionalWords = ['feel', 'emotion', 'understand', 'support', 'acknowledge', 'validate', 'empathy', 'anxiety', 'stress', 'overwhelm'];
const emotionalCount = emotionalWords.filter(word => responseLower.includes(word)).length;
analysis.emotionalIntelligence = emotionalCount >= 3;
if (analysis.emotionalIntelligence) analysis.overallScore += 2;
// Personalization (2 points)
const personalWords = ['you', 'your', 'you\'re', 'for you', 'help you', 'you mentioned', 'you\'ve', 'you notice'];
const personalCount = personalWords.filter(word => responseLower.includes(word)).length;
analysis.personalization = personalCount >= 4;
if (analysis.personalization) analysis.overallScore += 2;
// Actionable Insights (2 points)
const actionWords = ['try', 'consider', 'suggest', 'recommend', 'practice', 'strategy', 'technique', 'approach', 'step', 'action'];
const actionCount = actionWords.filter(word => responseLower.includes(word)).length;
analysis.actionableInsights = actionCount >= 3;
if (analysis.actionableInsights) analysis.overallScore += 2;
// Pattern Recognition (2 points)
const patternWords = ['pattern', 'recurring', 'repeatedly', 'cycle', 'trend', 'consistently', 'often', 'tends to'];
const patternCount = patternWords.filter(word => responseLower.includes(word)).length;
analysis.patternRecognition = patternCount >= 2;
if (analysis.patternRecognition) analysis.overallScore += 2;
// Clarity (1 point)
const hasGoodLength = response.length > 200 && response.length < 2000;
const hasStructure = response.includes('β’') || response.includes('-') || response.includes('1.') || response.includes('\n\n');
analysis.clarity = hasGoodLength && hasStructure;
if (analysis.clarity) analysis.overallScore += 1;
// Empathy (1 point)
const empathyWords = ['understand', 'makes sense', 'natural', 'common', 'valid', 'difficult', 'challenging'];
const empathyCount = empathyWords.filter(word => responseLower.includes(word)).length;
analysis.empathy = empathyCount >= 2;
if (analysis.empathy) analysis.overallScore += 1;
return analysis;
}
// Check journal modifications using getJournalVersions
async checkJournalModifications(structuredActions) {
console.log('π Checking journal modifications...');
const modifications = [];
// Simulate journal version checking (would need actual journal IDs in real implementation)
for (const action of structuredActions) {
if (action.tool === 'createJournal' || action.action === 'create_journal') {
modifications.push({
type: 'create',
quality: 'good', // Would analyze actual content
needsUndo: false
});
} else if (action.tool === 'updateJournal' || action.action === 'update_journal') {
modifications.push({
type: 'update',
quality: 'good', // Would analyze using getJournalVersions
needsUndo: false // Would use setVersionById if needed
});
}
}
return modifications;
}
// Determine if optimization is needed
determineOptimizationNeed(qualityAnalysis, responseTime, journalModifications) {
const worldClassThresholds = {
minQualityScore: 8.5,
maxResponseTime: 10000,
minActionsTriggered: 1
};
let needsOptimization = false;
const reasons = [];
if (qualityAnalysis.overallScore < worldClassThresholds.minQualityScore) {
needsOptimization = true;
reasons.push(`Quality score ${qualityAnalysis.overallScore} below threshold ${worldClassThresholds.minQualityScore}`);
}
if (responseTime > worldClassThresholds.maxResponseTime) {
needsOptimization = true;
reasons.push(`Response time ${responseTime}ms above threshold ${worldClassThresholds.maxResponseTime}ms`);
}
const badModifications = journalModifications.filter(mod => mod.needsUndo);
if (badModifications.length > 0) {
needsOptimization = true;
reasons.push(`${badModifications.length} poor journal modifications detected`);
}
console.log(`\nπ― OPTIMIZATION DECISION: ${needsOptimization ? 'NEEDED' : 'NOT NEEDED'}`);
if (reasons.length > 0) {
console.log('π Reasons:');
reasons.forEach(reason => console.log(` - ${reason}`));
}
return needsOptimization;
}
// Optimize prompts and workflow
async optimizePromptsAndWorkflow(analysisResult) {
console.log('\nπ§ OPTIMIZING PROMPTS AND WORKFLOW...');
const { qualityAnalysis, responseTime, structuredActions } = analysisResult;
// Identify specific optimization targets
const optimizations = [];
if (!qualityAnalysis.emotionalIntelligence) {
optimizations.push('emotion');
}
if (!qualityAnalysis.personalization || !qualityAnalysis.empathy) {
optimizations.push('supervisor');
}
if (!qualityAnalysis.actionableInsights) {
optimizations.push('enhancement');
}
if (!qualityAnalysis.patternRecognition) {
optimizations.push('memory');
}
if (structuredActions.length === 0) {
optimizations.push('tags'); // Tags agent handles backend actions
}
if (responseTime > 10000) {
optimizations.push('workflow'); // Optimize query.js workflow
}
console.log(`π― Optimization Targets: ${optimizations.join(', ')}`);
// Apply optimizations
for (const target of optimizations) {
await this.applyOptimization(target, qualityAnalysis);
}
console.log('β
Optimizations applied successfully');
}
// Apply specific optimization to prompts or workflow
async applyOptimization(target, qualityAnalysis) {
console.log(`π§ Applying ${target} optimization...`);
try {
if (target === 'workflow') {
await this.optimizeWorkflow();
} else {
await this.optimizePrompt(target, qualityAnalysis);
}
} catch (error) {
console.error(`β Failed to optimize ${target}:`, error.message);
}
}
// Optimize specific prompt
async optimizePrompt(agentName, qualityAnalysis) {
const promptPath = join(__dirname, 'prompts', `${agentName}_worldclass.md`);
try {
const currentPrompt = await readFile(promptPath, 'utf-8');
// Create enhanced version based on quality analysis
const optimizedPrompt = this.enhancePrompt(currentPrompt, agentName, qualityAnalysis);
// Save optimized version
const optimizedPath = join(__dirname, 'prompts', `${agentName}_optimized_v${this.iterationCount + 1}.md`);
await writeFile(optimizedPath, optimizedPrompt, 'utf-8');
console.log(` β
Enhanced ${agentName} prompt saved to ${agentName}_optimized_v${this.iterationCount + 1}.md`);
} catch (error) {
console.error(` β Failed to optimize ${agentName} prompt:`, error.message);
}
}
// Enhance prompt based on quality analysis
enhancePrompt(currentPrompt, agentName, qualityAnalysis) {
let enhanced = currentPrompt;
// Add performance enhancements based on missing qualities
const enhancements = [];
if (!qualityAnalysis.emotionalIntelligence && agentName === 'emotion') {
enhancements.push(`
EMOTIONAL INTELLIGENCE ENHANCEMENT:
- Use more empathetic language and emotional validation
- Include emotional pattern recognition phrases
- Focus on emotional support and understanding`);
}
if (!qualityAnalysis.personalization && agentName === 'supervisor') {
enhancements.push(`
PERSONALIZATION ENHANCEMENT:
- Use more personal pronouns and direct addressing
- Reference user's specific situation and context
- Create responses that feel tailored to the individual`);
}
if (!qualityAnalysis.actionableInsights && agentName === 'enhancement') {
enhancements.push(`
ACTIONABLE INSIGHTS ENHANCEMENT:
- Always include specific, actionable recommendations
- Provide concrete strategies and techniques
- Offer step-by-step approaches`);
}
if (!qualityAnalysis.patternRecognition && agentName === 'memory') {
enhancements.push(`
PATTERN RECOGNITION ENHANCEMENT:
- Focus on identifying recurring patterns and cycles
- Use pattern-related vocabulary consistently
- Connect past experiences to current situations`);
}
if (enhancements.length > 0) {
enhanced += `\n\n<!-- OPTIMIZATION ITERATION ${this.iterationCount + 1} ENHANCEMENTS -->\n`;
enhanced += enhancements.join('\n\n');
}
return enhanced;
}
// Optimize workflow in query.js
async optimizeWorkflow() {
console.log(' π§ Optimizing query.js workflow for better performance...');
// Read current query.js
const queryPath = join(__dirname, 'utils', 'query.js');
const currentContent = await readFile(queryPath, 'utf-8');
// Apply workflow optimizations (simplified example)
const optimizedContent = currentContent.replace(
/timeout: 10000/g,
'timeout: 8000' // Reduce timeout for faster responses
);
// Save optimized version for testing
const optimizedPath = join(__dirname, 'utils', `query_optimized_v${this.iterationCount + 1}.js`);
await writeFile(optimizedPath, optimizedContent, 'utf-8');
console.log(` β
Workflow optimization saved to query_optimized_v${this.iterationCount + 1}.js`);
}
// Main optimization loop
async runOptimizationCycle() {
console.log(`\nπ OPTIMIZATION ITERATION ${this.iterationCount + 1}/${this.maxIterations}`);
console.log('=' .repeat(60));
// Step 1: Generate test message
const testMessage = this.generateTestMessage();
// Step 2: Send query and wait for response
const queryResponse = await this.sendQueryAndWait(testMessage);
// Step 3: Analyze response and check modifications
const analysisResult = await this.analyzeResponseAndCheckModifications(queryResponse);
// Store results
this.optimizationResults.push({
iteration: this.iterationCount + 1,
testMessage: testMessage.description,
queryResponse: queryResponse.success,
analysisResult,
timestamp: new Date().toISOString()
});
// Check if optimization is needed
if (analysisResult.needsOptimization) {
console.log('\nπ§ OPTIMIZATION NEEDED - Applying improvements...');
await this.optimizePromptsAndWorkflow(analysisResult);
this.iterationCount++;
// Continue if under max iterations
if (this.iterationCount < this.maxIterations) {
console.log('\nβ° Waiting 10 seconds before next iteration...');
await new Promise(resolve => setTimeout(resolve, 10000));
return this.runOptimizationCycle();
} else {
console.log('\nβ οΈ Maximum iterations reached');
return this.generateFinalReport();
}
} else {
console.log('\nπ WORLD-CLASS PERFORMANCE ACHIEVED!');
return this.generateFinalReport();
}
}
// Generate final optimization report
async generateFinalReport() {
console.log('\nπ FINAL OPTIMIZATION REPORT');
console.log('=' .repeat(60));
const report = {
totalIterations: this.iterationCount + 1,
finalStatus: this.optimizationResults[this.optimizationResults.length - 1]?.analysisResult?.needsOptimization ? 'MAX_ITERATIONS_REACHED' : 'WORLD_CLASS_ACHIEVED',
results: this.optimizationResults,
summary: this.generateSummary()
};
// Save report
const reportPath = join(__dirname, `optimization_report_${Date.now()}.json`);
await writeFile(reportPath, JSON.stringify(report, null, 2), 'utf-8');
console.log(`π Optimization Summary:`);
console.log(` π Total Iterations: ${report.totalIterations}`);
console.log(` π― Final Status: ${report.finalStatus}`);
console.log(` π Quality Improvements: ${report.summary.qualityImprovement}%`);
console.log(` β‘ Speed Improvements: ${report.summary.speedImprovement}%`);
console.log(` π Report saved to: optimization_report_${Date.now()}.json`);
if (report.finalStatus === 'WORLD_CLASS_ACHIEVED') {
console.log('\nπ CONGRATULATIONS! Your AI system is now optimized to world-class standards!');
console.log('π Ready to serve 7.2 billion people with excellence!');
} else {
console.log('\nπ Significant improvements made. Consider running additional optimization cycles.');
}
return report;
}
generateSummary() {
if (this.optimizationResults.length === 0) return { qualityImprovement: 0, speedImprovement: 0 };
const firstResult = this.optimizationResults[0];
const lastResult = this.optimizationResults[this.optimizationResults.length - 1];
const qualityImprovement = lastResult.analysisResult.qualityAnalysis ?
((lastResult.analysisResult.qualityAnalysis.overallScore - (firstResult.analysisResult.qualityAnalysis?.overallScore || 5)) / 10) * 100 : 0;
const speedImprovement = lastResult.analysisResult.responseTime && firstResult.analysisResult.responseTime ?
((firstResult.analysisResult.responseTime - lastResult.analysisResult.responseTime) / firstResult.analysisResult.responseTime) * 100 : 0;
return {
qualityImprovement: Math.max(0, Math.round(qualityImprovement)),
speedImprovement: Math.max(0, Math.round(speedImprovement))
};
}
}
// Run the optimization engine
async function main() {
console.log('π― Starting systematic optimization process...');
console.log('π Methodology: Generate message β Query AI β Analyze β Optimize β Repeat');
const engine = new OptimizationEngine();
try {
const report = await engine.runOptimizationCycle();
console.log('\nβ
Optimization process completed successfully!');
return report;
} catch (error) {
console.error('\nπ₯ Optimization process failed:', error.message);
console.error(error.stack);
return null;
}
}
// Execute if run directly
if (import.meta.url === `file://${process.argv[1]}`) {
main()
.then(report => {
if (report) {
console.log('\nπ Optimization mission accomplished!');
process.exit(0);
} else {
console.log('\nβ Optimization mission failed');
process.exit(1);
}
})
.catch(error => {
console.error('π₯ Fatal error:', error);
process.exit(1);
});
}
export { OptimizationEngine };