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