Skip to content

Commit 84b5e6b

Browse files
authored
update route and prompt (#33)
* update route and prompt * pretty * rm log * rm files
1 parent fbad3be commit 84b5e6b

File tree

4 files changed

+135
-93
lines changed

4 files changed

+135
-93
lines changed

src/controllers/evaluationController.ts

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ const batchPromises = <T>(array: T[], batchSize: number): T[][] => {
300300

301301
export const createLLMEvaluations = async (
302302
paramsArray: CreateLLMEvaluationParams[]
303-
): Promise<void> => {
303+
): Promise<string[]> => {
304304
const roundCache: Record<string, RoundWithApplications> = {};
305+
const failedProjects: string[] = [];
305306

306307
// Split the paramsArray into batches of 10
307308
const batchedParams = batchPromises(paramsArray, 10);
@@ -310,86 +311,95 @@ export const createLLMEvaluations = async (
310311
try {
311312
// Process each batch of promises concurrently
312313
const evaluationPromises = batch.map(async params => {
313-
const evaluationQuestions =
314-
params.questions === undefined || params.questions.length === 0
315-
? await evaluationService.getQuestionsByChainAndAlloPoolId(
316-
params.chainId,
317-
params.alloPoolId
318-
)
319-
: params.questions;
320-
321-
if (evaluationQuestions === null || evaluationQuestions.length === 0) {
322-
logger.error(
323-
'createLLMEvaluations:Failed to get evaluation questions'
324-
);
325-
throw new Error('Failed to get evaluation questions');
326-
}
327-
328-
let roundMetadata = params.roundMetadata;
329-
let applicationMetadata = params.applicationMetadata;
330-
331-
// Check if the round is already in cache
332-
if (roundMetadata == null || applicationMetadata == null) {
333-
let round: RoundWithApplications | null;
334-
335-
// If the round is cached, use it
336-
if (roundCache[params.alloPoolId] != null) {
337-
round = roundCache[params.alloPoolId];
338-
logger.debug(
339-
`Using cached round data for roundId: ${params.alloPoolId}`
340-
);
341-
} else {
342-
// Fetch the round and store it in the cache
343-
const [error, fetchedRound] = await catchError(
344-
indexerClient.getRoundWithApplications({
345-
chainId: params.chainId,
346-
roundId: params.alloPoolId,
347-
})
314+
try {
315+
const evaluationQuestions =
316+
params.questions === undefined || params.questions.length === 0
317+
? await evaluationService.getQuestionsByChainAndAlloPoolId(
318+
params.chainId,
319+
params.alloPoolId
320+
)
321+
: params.questions;
322+
323+
if (
324+
evaluationQuestions === null ||
325+
evaluationQuestions.length === 0
326+
) {
327+
logger.error(
328+
'createLLMEvaluations:Failed to get evaluation questions'
348329
);
330+
throw new Error('Failed to get evaluation questions');
331+
}
349332

350-
if (error !== undefined || fetchedRound == null) {
351-
logger.error('Failed to fetch round with applications');
352-
throw new Error('Failed to fetch round with applications');
333+
let roundMetadata = params.roundMetadata;
334+
let applicationMetadata = params.applicationMetadata;
335+
336+
// Check if the round is already in cache
337+
if (roundMetadata == null || applicationMetadata == null) {
338+
let round: RoundWithApplications | null;
339+
340+
// If the round is cached, use it
341+
if (roundCache[params.alloPoolId] != null) {
342+
round = roundCache[params.alloPoolId];
343+
logger.debug(
344+
`Using cached round data for roundId: ${params.alloPoolId}`
345+
);
346+
} else {
347+
// Fetch the round and store it in the cache
348+
const [error, fetchedRound] = await catchError(
349+
indexerClient.getRoundWithApplications({
350+
chainId: params.chainId,
351+
roundId: params.alloPoolId,
352+
})
353+
);
354+
355+
if (error !== undefined || fetchedRound == null) {
356+
logger.error('Failed to fetch round with applications');
357+
throw new Error('Failed to fetch round with applications');
358+
}
359+
360+
round = fetchedRound;
361+
roundCache[params.alloPoolId] = round;
362+
logger.info(
363+
`Fetched and cached round with ID: ${round.id}, which includes ${round.applications.length} applications`
364+
);
353365
}
354366

355-
round = fetchedRound;
356-
roundCache[params.alloPoolId] = round;
357-
logger.info(
358-
`Fetched and cached round with ID: ${round.id}, which includes ${round.applications.length} applications`
367+
const application = round.applications.find(
368+
app => app.id === params.alloApplicationId
359369
);
370+
if (application == null) {
371+
logger.error(
372+
`Application with ID: ${params.alloApplicationId} not found in round`
373+
);
374+
throw new NotFoundError(
375+
`Application with ID: ${params.alloApplicationId} not found in round`
376+
);
377+
}
378+
379+
roundMetadata = round.roundMetadata;
380+
applicationMetadata = application.metadata;
360381
}
361382

362-
const application = round.applications.find(
363-
app => app.id === params.alloApplicationId
383+
const evaluation = await requestEvaluation(
384+
roundMetadata,
385+
applicationMetadata,
386+
evaluationQuestions
364387
);
365-
if (application == null) {
366-
logger.error(
367-
`Application with ID: ${params.alloApplicationId} not found in round`
368-
);
369-
throw new NotFoundError(
370-
`Application with ID: ${params.alloApplicationId} not found in round`
371-
);
372-
}
373388

374-
roundMetadata = round.roundMetadata;
375-
applicationMetadata = application.metadata;
389+
await createEvaluation({
390+
chainId: params.chainId,
391+
alloPoolId: params.alloPoolId,
392+
alloApplicationId: params.alloApplicationId,
393+
cid: params.cid,
394+
evaluator: params.evaluator,
395+
summaryInput: evaluation,
396+
evaluatorType: EVALUATOR_TYPE.LLM_GPT3,
397+
});
398+
} catch (error) {
399+
// If an error occurs, add the project ID to the failedProjects array
400+
failedProjects.push(params.alloApplicationId);
401+
throw error;
376402
}
377-
378-
const evaluation = await requestEvaluation(
379-
roundMetadata,
380-
applicationMetadata,
381-
evaluationQuestions
382-
);
383-
384-
await createEvaluation({
385-
chainId: params.chainId,
386-
alloPoolId: params.alloPoolId,
387-
alloApplicationId: params.alloApplicationId,
388-
cid: params.cid,
389-
evaluator: params.evaluator,
390-
summaryInput: evaluation,
391-
evaluatorType: EVALUATOR_TYPE.LLM_GPT3,
392-
});
393403
});
394404

395405
await Promise.all(evaluationPromises);
@@ -405,4 +415,6 @@ export const createLLMEvaluations = async (
405415
continue;
406416
}
407417
}
418+
419+
return failedProjects;
408420
};

src/controllers/poolController.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,34 @@ export const syncPool = async (req: Request, res: Response): Promise<void> => {
101101

102102
// ---- LLM evaluation ----
103103
// Trigger LLM evaluation for the pool if there are applications without evaluations
104-
if (skipEvaluation !== true)
105-
await triggerLLMEvaluationByPool(
104+
let failedProjects: string[] = [];
105+
if (skipEvaluation !== true) {
106+
failedProjects = await triggerLLMEvaluationByPool(
106107
alloPoolId,
107108
chainId,
108109
indexerPoolData,
109110
evaluationQuestions
110111
);
111-
// Log success and respond to the request
112-
logger.info('successfully synced pool', pool);
113-
res.status(200).json({ message: 'pool synced successfully' });
112+
}
113+
114+
// Check if there are any failed projects and respond accordingly
115+
if (failedProjects.length > 0) {
116+
logger.info('Pool synced successfully with some projects failing', {
117+
pool,
118+
failedProjects,
119+
});
120+
res.status(207).json({
121+
success: true,
122+
message: 'Pool synced successfully, with some projects failing.',
123+
failedProjects,
124+
});
125+
} else {
126+
logger.info('Successfully synced pool', pool);
127+
res.status(200).json({
128+
success: true,
129+
message: 'Pool synced successfully.',
130+
});
131+
}
114132
};
115133

116134
const handlePoolEvaluationQuestions = async (
@@ -171,7 +189,7 @@ const triggerLLMEvaluationByPool = async (
171189
chainId: number,
172190
indexerPoolData: IndexerRoundWithApplications,
173191
evaluationQuestions: PromptEvaluationQuestions
174-
): Promise<void> => {
192+
): Promise<string[]> => {
175193
const applicationsWithoutLLM =
176194
await applicationService.getApplicationsWithoutLLMEvalutionsByAlloPoolId(
177195
alloPoolId,
@@ -212,5 +230,5 @@ const triggerLLMEvaluationByPool = async (
212230
logger.warn('Some applications were not found in indexerPoolData');
213231
}
214232

215-
await createLLMEvaluations(evaluationParamsArray);
233+
return await createLLMEvaluations(evaluationParamsArray);
216234
};

src/ext/openai/prompt.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ const sanitizeAndReduceMetadata = (
3939
};
4040

4141
const sanitizeRoundMetadata = (metadata: RoundMetadata): string => {
42-
return sanitizeAndReduceMetadata(metadata, essentialRoundFields, 1000);
42+
return sanitizeAndReduceMetadata(metadata, essentialRoundFields, 50000);
4343
};
4444

4545
const sanitizeApplicationMetadata = (metadata: object): string => {
46-
return sanitizeAndReduceMetadata(metadata, essentialApplicationFields, 3000);
46+
return sanitizeAndReduceMetadata(metadata, essentialApplicationFields, 50000);
4747
};
4848
export const createAiEvaluationPrompt = (
4949
roundMetadata: RoundMetadata,
@@ -52,7 +52,7 @@ export const createAiEvaluationPrompt = (
5252
): string => {
5353
const sanitizedRoundMetadata = sanitizeRoundMetadata(roundMetadata);
5454
const sanitizedApplicationMetadata = sanitizeApplicationMetadata(
55-
applicationMetadata.application.project
55+
applicationMetadata.application
5656
);
5757

5858
const questionsString = applicationQuestions

src/routes/poolRoutes.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,43 @@ const router = Router();
1818
* alloPoolId:
1919
* type: string
2020
* description: The ID of the pool to create
21-
* example: "609" # Example of poolId
21+
* example: "609"
2222
* chainId:
2323
* type: number
2424
* description: The chain ID associated with the pool
25-
* example: 42161 # Example of chainId (Arbitrum)
25+
* example: 42161
2626
* skipEvaluation:
27-
* type: boolean
28-
* description: Skip evaluation of the pool
29-
* example: true
27+
* type: boolean
28+
* description: Skip evaluation of the pool
29+
* example: true
3030
* required:
3131
* - alloPoolId
3232
* - chainId
3333
* responses:
3434
* 201:
35-
* description: Pool created successfully
35+
* description: Pool synced successfully
36+
* 207:
37+
* description: Pool synced successfully, with some projects failing
38+
* content:
39+
* application/json:
40+
* schema:
41+
* type: object
42+
* properties:
43+
* success:
44+
* type: boolean
45+
* example: true
46+
* message:
47+
* type: string
48+
* example: Pool synced successfully, with some projects failing.
49+
* failedProjects:
50+
* type: array
51+
* items:
52+
* type: string
53+
* example: "0xprojectId1"
3654
* 400:
3755
* description: Invalid poolId or chainId format
3856
* 500:
3957
* description: Internal server error
40-
* examples:
41-
* application/json:
42-
* - value:
43-
* alloPoolId: "609"
44-
* chainId: "42161"
45-
* skipEvaluation: true
4658
*/
4759
router.post('/', syncPool);
4860

0 commit comments

Comments
 (0)