Просмотр исходного кода

fix: 大纲生成后按bookScale裁剪章节数,防止LLM超限

LLM生成大纲时不严格遵循章节数配置,缩规模时可能产生过多章节。
现在创建章节前按 getChapterRange(bookScale).max 强制裁剪。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
MyFramework User 3 месяцев назад
Родитель
Сommit
eb556d0b3d
1 измененных файлов с 9 добавлено и 1 удалено
  1. 9 1
      server/src/modules/book-generator/nodes/outline.node.ts

+ 9 - 1
server/src/modules/book-generator/nodes/outline.node.ts

@@ -9,7 +9,7 @@ import { parseOutline } from '../parsers/outline.parser';
 import { buildOutlineMessages, SCALE_CHAPTER_RANGE } from '../prompts/builder';
 import { PROGRESS } from '../utils';
 import { callLLMWithRetry, executeNodeWithTimeout, FAULT_TOLERANCE_CONFIG } from '../fault-tolerance';
-import { getChapterRange } from '../book-type-config';
+import { getChapterRange, calcChapters } from '../book-type-config';
 
 export async function generateOutlineNode(state: typeof GraphState.State): Promise<Partial<typeof GraphState.State>> {
   console.log('[LangGraph] 生成大纲, bookId:', state.bookId, 'scale:', state.bookScale);
@@ -96,6 +96,14 @@ export async function generateOutlineNode(state: typeof GraphState.State): Promi
       }];
     }
 
+    // 根据 bookScale 限制章节数(防止LLM生成超出配置范围的章节)
+    const configuredChapters = calcChapters(parseInt(state.bookScale) || 2000);
+    const chapterRange = getChapterRange(configuredChapters);
+    if (outline.chapters.length > chapterRange.max) {
+      console.log(`[LangGraph] 大纲章节数(${outline.chapters.length})超出上限(${chapterRange.max}),裁剪到${chapterRange.max}章`);
+      outline.chapters = outline.chapters.slice(0, chapterRange.max);
+    }
+
     const totalChapters = outline.chapters.length;
     await bookStore.update(state.bookId, {
       totalChapters,