ai评论生成

This commit is contained in:
mtvpls
2026-03-14 11:30:17 +08:00
parent ea169593a6
commit 11317abc98
8 changed files with 731 additions and 0 deletions
+4
View File
@@ -57,6 +57,7 @@ export async function POST(request: NextRequest) {
EnableHomepageEntry,
EnableVideoCardEntry,
EnablePlayPageEntry,
EnableAIComments,
AllowRegularUsers,
Temperature,
MaxTokens,
@@ -93,6 +94,7 @@ export async function POST(request: NextRequest) {
EnableHomepageEntry: boolean;
EnableVideoCardEntry: boolean;
EnablePlayPageEntry: boolean;
EnableAIComments: boolean;
AllowRegularUsers: boolean;
Temperature?: number;
MaxTokens?: number;
@@ -132,6 +134,7 @@ export async function POST(request: NextRequest) {
typeof EnableHomepageEntry !== 'boolean' ||
typeof EnableVideoCardEntry !== 'boolean' ||
typeof EnablePlayPageEntry !== 'boolean' ||
typeof EnableAIComments !== 'boolean' ||
typeof AllowRegularUsers !== 'boolean' ||
(Temperature !== undefined && typeof Temperature !== 'number') ||
(MaxTokens !== undefined && typeof MaxTokens !== 'number') ||
@@ -181,6 +184,7 @@ export async function POST(request: NextRequest) {
EnableHomepageEntry,
EnableVideoCardEntry,
EnablePlayPageEntry,
EnableAIComments,
AllowRegularUsers,
Temperature,
MaxTokens,
+113
View File
@@ -0,0 +1,113 @@
import { NextRequest, NextResponse } from 'next/server';
import { generateAIComments, AIComment } from '@/lib/ai-comment-generator';
import { getConfig } from '@/lib/config';
export const runtime = 'nodejs';
interface AICommentsResponse {
comments: AIComment[];
total: number;
movieName: string;
isAiGenerated: true;
generatedAt: string;
}
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams;
const movieName = searchParams.get('name');
const movieInfo = searchParams.get('info');
const count = parseInt(searchParams.get('count') || '10');
// 参数验证
if (!movieName) {
return NextResponse.json(
{ error: '缺少影片名称参数' },
{ status: 400 }
);
}
if (count < 1 || count > 50) {
return NextResponse.json(
{ error: '评论数量必须在1-50之间' },
{ status: 400 }
);
}
// 读取AI配置
const config = await getConfig();
const aiConfig = config.AIConfig;
// 检查AI功能是否启用
if (!aiConfig?.Enabled) {
return NextResponse.json(
{ error: 'AI功能未启用' },
{ status: 403 }
);
}
// 检查AI评论功能是否启用
if (!aiConfig?.EnableAIComments) {
return NextResponse.json(
{ error: 'AI评论功能未启用' },
{ status: 403 }
);
}
// 检查必要的配置
if (!aiConfig.CustomApiKey || !aiConfig.CustomBaseURL || !aiConfig.CustomModel) {
return NextResponse.json(
{ error: 'AI配置不完整,请在管理面板配置' },
{ status: 500 }
);
}
// 生成AI评论
const comments = await generateAIComments({
movieName,
movieInfo: movieInfo || undefined,
count,
aiConfig: {
CustomApiKey: aiConfig.CustomApiKey,
CustomBaseURL: aiConfig.CustomBaseURL,
CustomModel: aiConfig.CustomModel,
Temperature: aiConfig.Temperature,
MaxTokens: aiConfig.MaxTokens,
EnableWebSearch: aiConfig.EnableWebSearch,
WebSearchProvider: aiConfig.WebSearchProvider,
TavilyApiKey: aiConfig.TavilyApiKey,
SerperApiKey: aiConfig.SerperApiKey,
SerpApiKey: aiConfig.SerpApiKey,
},
});
// 返回结果
const response: AICommentsResponse = {
comments,
total: comments.length,
movieName,
isAiGenerated: true,
generatedAt: new Date().toISOString(),
};
return NextResponse.json(response, {
headers: {
'Cache-Control': 'public, max-age=3600, s-maxage=3600', // 缓存1小时
},
});
} catch (error) {
console.error('AI评论生成失败:', error);
// 返回友好的错误信息
const errorMessage = error instanceof Error ? error.message : 'AI评论生成失败';
return NextResponse.json(
{
error: errorMessage,
details: process.env.NODE_ENV === 'development' ? String(error) : undefined
},
{ status: 500 }
);
}
}