fix(comment): refine AI evaluation method and update comment options

- Updated `evaluateCommentWithAI` to enforce stricter type checks for `aiReviewType`, now only allowing 'binary' or 'score'.
- Added test calls in the constructor of `CommentService` to evaluate comments using AI.
- Enhanced the response handling in `evaluateCommentWithAI` to improve error management and ensure proper parsing of AI responses.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-05-05 21:09:41 +08:00
parent 86fdb9e1f6
commit f8555216b6
2 changed files with 28 additions and 30 deletions

View File

@@ -124,7 +124,7 @@ export class CommentService implements OnModuleInit {
*/ */
private async evaluateCommentWithAI( private async evaluateCommentWithAI(
text: string, text: string,
aiReviewType: 'score' | string, aiReviewType: 'binary' | 'score',
aiReviewThreshold: number, aiReviewThreshold: number,
): Promise<boolean> { ): Promise<boolean> {
const runnable = await this.aiService.getOpenAiChain() const runnable = await this.aiService.getOpenAiChain()
@@ -190,7 +190,8 @@ export class CommentService implements OnModuleInit {
} }
try { try {
const response = await runnable.invoke([spamPrompt], { const response = await runnable
.bind({
tools: [ tools: [
{ {
type: 'function', type: 'function',
@@ -215,16 +216,13 @@ export class CommentService implements OnModuleInit {
}, },
], ],
}) })
.pipe(new JsonOutputToolsParser())
.invoke([spamPrompt])
const content = response.content.toString() if (!response) {
// 提取JSON部分
const jsonMatch = content.match(/\{.*\}/s)
if (!jsonMatch) {
this.logger.warn('AI评审返回格式异常无法解析JSON')
return false return false
} }
const responseData = (response[0] as any)?.args
const responseData = JSON.parse(jsonMatch[0])
// 如果包含敏感内容直接拒绝 // 如果包含敏感内容直接拒绝
if (responseData.hasSensitiveContent) { if (responseData.hasSensitiveContent) {

View File

@@ -151,7 +151,7 @@ export class CommentOptionsDto {
], ],
}, },
}) })
aiReviewType: string aiReviewType: 'binary' | 'score'
@IsInt() @IsInt()
@Transform(({ value: val }) => Number.parseInt(val)) @Transform(({ value: val }) => Number.parseInt(val))