-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluationController.ts
More file actions
478 lines (416 loc) · 13.2 KB
/
evaluationController.ts
File metadata and controls
478 lines (416 loc) · 13.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
import type { Request, Response } from 'express';
import evaluationService, {
type CreateEvaluationParams,
} from '@/service/EvaluationService';
import {
addressFrom,
catchError,
isPoolManager,
validateRequest,
} from '@/utils';
import { createLogger } from '@/logger';
import applicationService from '@/service/ApplicationService';
import poolService from '@/service/PoolService';
import {
type PromptEvaluationQuestions,
requestEvaluation,
requestEvaluationQuestions,
} from '@/ext/openai';
import {
type ApplicationMetadata,
indexerClient,
type RoundMetadata,
type RoundWithApplications,
} from '@/ext/indexer';
import { type Evaluation, EVALUATOR_TYPE } from '@/entity/Evaluation';
import { IsNullError, NotFoundError } from '@/errors';
import { type Hex } from 'viem';
import type {
PoolIdChainId,
PoolIdChainIdApplicationId,
PoolIdChainIdApplicationIdBody,
PoolIdChainIdBody,
} from './types';
import evaluationQuestionService from '@/service/EvaluationQuestionService';
import { rateLimit } from 'express-rate-limit';
const logger = createLogger();
interface EvaluationBody extends CreateEvaluationParams {
signature: Hex;
}
export const evaluationRateLimiter = rateLimit({
windowMs: 60 * 1000,
max: 20, // 20 requests per minute
message: 'Too many evaluation requests',
});
export const recreateEvaluationQuestionsLimiter = rateLimit({
windowMs: 60 * 1000,
max: 5, // More restrictive for question recreation
message: 'Too many question recreation requests',
});
export const triggerLLMEvaluationLimiter = rateLimit({
windowMs: 60 * 1000,
max: 10, // 10 requests per minute
message: 'Too many LLM evaluation requests',
});
export const recreateEvaluationQuestions = async (
req: Request,
res: Response
): Promise<void> => {
const { chainId, alloPoolId, signature } = req.body as PoolIdChainIdBody;
const isAllowed = await isPoolManager<PoolIdChainId>(
{ alloPoolId, chainId },
signature,
chainId,
alloPoolId
);
if (!isAllowed) {
logger.warn(
`User with address: ${signature} is not allowed to evaluate application`
);
res.status(403).json({ message: 'Unauthorized' });
return;
}
const [error, roundWithApplications] = await catchError(
indexerClient.getRoundWithApplications({
chainId,
roundId: alloPoolId,
})
);
if (
error !== undefined ||
roundWithApplications === undefined ||
roundWithApplications?.roundMetadata === undefined
) {
logger.error('Failed to fetch round with applications');
res
.status(404)
.json({ message: 'Failed to fetch round with applications' });
return;
}
const evaluationQuestions = await requestEvaluationQuestions(
roundWithApplications.roundMetadata
);
if (evaluationQuestions === null || evaluationQuestions.length === 0) {
logger.error('Failed to get evaluation questions');
res.status(404).json({ message: 'Failed to get evaluation questions' });
return;
}
const [errorReset] = await catchError(
evaluationQuestionService.resetEvaluationQuestions(
chainId,
alloPoolId,
evaluationQuestions
)
);
if (errorReset !== undefined) {
logger.error('Failed to reset evaluation questions', { errorReset });
res.status(500).json({ message: 'Failed to reset evaluation questions' });
return;
}
const [errorClean] = await catchError(evaluationService.cleanEvaluations());
if (errorClean !== undefined) {
logger.error('Failed to clean evaluations', { errorClean });
res.status(500).json({ message: 'Failed to clean evaluations' });
return;
}
res.status(200).json(evaluationQuestions);
};
export const evaluateApplication = async (
req: Request,
res: Response
): Promise<void> => {
validateRequest(req, res);
const {
alloPoolId,
alloApplicationId,
cid,
evaluator,
evaluationStatus,
summaryInput,
chainId,
signature,
}: EvaluationBody = req.body;
logger.info(
`Received evaluation request for alloApplicationId: ${alloApplicationId} in poolId: ${alloPoolId}`
);
const createEvaluationParams: CreateEvaluationParams = {
chainId,
alloPoolId,
alloApplicationId,
cid,
evaluator,
evaluationStatus,
summaryInput,
};
const [isAllowedError, isAllowed] = await catchError(
isPoolManager<CreateEvaluationParams>(
createEvaluationParams,
signature,
chainId,
alloPoolId
)
);
if (isAllowedError !== undefined || isAllowed === undefined) {
logger.warn(
`User with address: ${evaluator} is not allowed to evaluate application`
);
res.status(403).json({ message: 'Unauthorized' });
return;
}
const [errorFetching, application] = await catchError(
applicationService.getApplicationByChainIdPoolIdApplicationId(
alloPoolId,
chainId,
alloApplicationId
)
);
if (errorFetching !== undefined || application === undefined) {
logger.warn(
`No application found for alloApplicationId: ${alloApplicationId}`
);
res.status(404).json({ message: 'Application not found' });
return;
}
const [errorGetPool, pool] = await catchError(
poolService.getPoolByChainIdAndAlloPoolId(chainId, alloPoolId)
);
if (errorGetPool !== undefined || pool === undefined) {
logger.warn(`No pool found for poolId: ${alloPoolId}`);
res.status(404).json({ message: 'Pool not found' });
return;
}
const [evaluationError, evaluationResponse] = await catchError(
createEvaluation(createEvaluationParams)
);
if (evaluationError !== undefined || evaluationResponse === undefined) {
logger.error(
'Evaluation creation failed:',
evaluationError ?? 'Unknown error'
);
res.status(500).json({
message: 'Error creating evaluation',
error: evaluationError?.message ?? 'Evaluation creation failed.',
});
return;
}
logger.info(`Evaluation created for alloApplicationId: ${alloApplicationId}`);
res.status(200).json({
message: 'Evaluation successfully created',
evaluationId: evaluationResponse?.id,
});
};
// Second function to handle creating the evaluation and error checking
export const createEvaluation = async (
params: CreateEvaluationParams
): Promise<Evaluation> => {
// Create evaluation with answers
const [evaluationError, evaluation] = await catchError(
evaluationService.createEvaluationWithAnswers(params)
);
if (evaluationError !== undefined || evaluation === undefined) {
logger.error('Failed to create evaluation: Evaluation is null.undefined');
throw new IsNullError('Evaluation is null/undefined');
}
return evaluation;
};
export interface CreateLLMEvaluationParams {
chainId: number;
alloPoolId: string;
alloApplicationId: string;
cid: string;
evaluator: string;
roundMetadata?: RoundMetadata;
applicationMetadata?: ApplicationMetadata;
questions?: PromptEvaluationQuestions;
}
export const triggerLLMEvaluation = async (
req: Request,
res: Response
): Promise<void> => {
validateRequest(req, res);
const { alloPoolId, chainId, alloApplicationId, signature } =
req.body as PoolIdChainIdApplicationIdBody;
const isAllowed = await isPoolManager<PoolIdChainIdApplicationId>(
{ alloPoolId, chainId, alloApplicationId },
signature,
chainId,
alloPoolId
);
if (!isAllowed) {
logger.warn(
`User with address: ${signature} is not allowed to evaluate application`
);
res.status(403).json({ message: 'Unauthorized' });
return;
}
const questions = await evaluationService.getQuestionsByChainAndAlloPoolId(
chainId,
alloPoolId
);
const [errorFetching, indexerApplicationData] = await catchError(
indexerClient.getApplicationWithRound({
chainId,
roundId: alloPoolId,
applicationId: alloApplicationId,
})
);
if (
errorFetching !== undefined ||
indexerApplicationData === undefined ||
indexerApplicationData === null
) {
logger.warn(
`No pool found for chainId: ${chainId}, alloPoolId: ${alloPoolId}`
);
res.status(404).json({ message: 'Pool not found on indexer' });
throw new NotFoundError(`Pool not found on indexer`);
}
const data: CreateLLMEvaluationParams = {
chainId,
alloPoolId,
alloApplicationId,
cid: indexerApplicationData.metadataCid,
evaluator: addressFrom(1),
roundMetadata: indexerApplicationData.round.roundMetadata,
applicationMetadata: indexerApplicationData.metadata,
questions,
};
const [error] = await catchError(createLLMEvaluations([data]));
if (error !== undefined) {
logger.error('Failed to create evaluations:', error);
res.status(500).json({
message: 'Failed to create evaluations',
error: error.message,
});
}
logger.info('LLM evaluation triggered successfully');
res.status(200).json({ message: 'LLM evaluation triggered successfully' });
};
const batchPromises = <T>(array: T[], batchSize: number): T[][] => {
const batches: T[][] = [];
for (let i = 0; i < array.length; i += batchSize) {
batches.push(array.slice(i, i + batchSize));
}
return batches;
};
export const createLLMEvaluations = async (
paramsArray: CreateLLMEvaluationParams[]
): Promise<string[]> => {
const roundCache: Record<string, RoundWithApplications> = {};
const failedProjects: string[] = [];
// Increase batch size and add delay between batches
const BATCH_SIZE = 25;
const BATCH_DELAY = 2000; // 2 seconds between batches
// Deduplicate params array based on unique application IDs
const uniqueParams = paramsArray.filter(
(param, index, self) =>
index ===
self.findIndex(
p =>
p.alloApplicationId === param.alloApplicationId &&
p.chainId === param.chainId
)
);
const batchedParams = batchPromises(uniqueParams, BATCH_SIZE);
for (const batch of batchedParams) {
try {
// Process batch with concurrency limit
await Promise.all(
batch.map(async params => {
await processSingleEvaluation(params, roundCache, failedProjects);
})
);
// Add delay between batches to prevent overwhelming the system
if (batchedParams.indexOf(batch) < batchedParams.length - 1) {
await new Promise(resolve => setTimeout(resolve, BATCH_DELAY));
}
} catch (batchError) {
logger.error('Error processing batch:', batchError);
continue;
}
}
return failedProjects;
};
async function processSingleEvaluation(
params: CreateLLMEvaluationParams,
roundCache: Record<string, RoundWithApplications>,
failedProjects: string[]
): Promise<void> {
try {
const evaluationQuestions =
params.questions === undefined || params.questions.length === 0
? await evaluationService.getQuestionsByChainAndAlloPoolId(
params.chainId,
params.alloPoolId
)
: params.questions;
if (evaluationQuestions === null || evaluationQuestions.length === 0) {
logger.error('createLLMEvaluations:Failed to get evaluation questions');
throw new Error('Failed to get evaluation questions');
}
let roundMetadata = params.roundMetadata;
let applicationMetadata = params.applicationMetadata;
// Check if the round is already in cache
if (roundMetadata == null || applicationMetadata == null) {
let round: RoundWithApplications | null;
// If the round is cached, use it
if (roundCache[params.alloPoolId] != null) {
round = roundCache[params.alloPoolId];
logger.debug(
`Using cached round data for roundId: ${params.alloPoolId}`
);
} else {
// Fetch the round and store it in the cache
const [error, fetchedRound] = await catchError(
indexerClient.getRoundWithApplications({
chainId: params.chainId,
roundId: params.alloPoolId,
})
);
if (
error !== undefined ||
fetchedRound === undefined ||
fetchedRound === null
) {
logger.error('Failed to fetch round with applications');
throw new Error('Failed to fetch round with applications');
}
round = fetchedRound;
roundCache[params.alloPoolId] = round;
logger.info(
`Fetched and cached round with ID: ${round.id}, which includes ${round.applications.length} applications`
);
}
const application = round.applications.find(
app => app.id === params.alloApplicationId
);
if (application == null) {
logger.error(
`Application with ID: ${params.alloApplicationId} not found in round`
);
throw new NotFoundError(
`Application with ID: ${params.alloApplicationId} not found in round`
);
}
roundMetadata = round.roundMetadata;
applicationMetadata = application.metadata;
}
const evaluation = await requestEvaluation(
roundMetadata,
applicationMetadata,
evaluationQuestions
);
await createEvaluation({
chainId: params.chainId,
alloPoolId: params.alloPoolId,
alloApplicationId: params.alloApplicationId,
cid: params.cid,
evaluator: params.evaluator,
summaryInput: evaluation,
evaluatorType: EVALUATOR_TYPE.LLM_GPT3,
});
} catch (error) {
failedProjects.push(params.alloApplicationId);
throw error;
}
}