perf: LLM 按 prompt 长度自适应思考强度

This commit is contained in:
2026-10-10 17:53:41 +08:00
parent 09d16dfb98
commit fd1193a66f
9 changed files with 321 additions and 6 deletions
+20 -3
View File
@@ -12,9 +12,10 @@
*/
import { randomUUID, createHash } from "node:crypto";
import { chat, isAvailable, loadConfig, LlmError, type ChatMessage } from "./llm/client.js";
import { chat, isAvailable, loadConfig, LlmError, type ChatMessage, type ChatOptions } from "./llm/client.js";
import { withRetry, isRetryableLlmError, type RetryOptions } from "./llm/retry.js";
import { aggregateReportPrompt, compareRecallPrompt, generateMindMapPrompt, guidancePrompt } from "./llm/prompts.js";
import { loadThinkingConfig, promptLength, reasoningEffortFor, type ThinkingConfig } from "./llm/thinking.js";
import { logStore } from "./admin/store.js";
// ---- 类型 ----
@@ -293,7 +294,7 @@ export class TaskQueue {
}
const messages = buildMessages(task);
const callOptions = callOptionsFor(task.type);
const callOptions = callOptionsFor(task.type, messages);
const retryOptions: RetryOptions = {
maxAttempts: this.config.maxAttempts,
@@ -378,13 +379,29 @@ export class TaskQueue {
// ---- 结果解析与 Prompt ----
function callOptionsFor(type: TaskType): { temperature: number; maxTokens: number } {
function baseCallOptions(type: TaskType): ChatOptions {
if (type === "compare-recall") return { temperature: 0.2, maxTokens: 4096 };
if (type === "generate-mind-map") return { temperature: 0.3, maxTokens: 4096 };
if (type === "guidance") return { temperature: 0.7, maxTokens: 1024 };
return { temperature: 0.3, maxTokens: 2048 };
}
/**
* 单次调用参数:温度与输出上限按任务类型固定,思考强度按 prompt 总长度自适应。
*
* 短输入(几句碎片、单轮引导)直接 `none` 关掉思考,不再跟随供应商的默认 `high`,
* 避免为一点点内容付满档思考的额度与延迟;阈值全部走 `LPT_AI_REASONING_*`。
*/
export function callOptionsFor(
type: TaskType,
messages: ChatMessage[],
thinking: ThinkingConfig = loadThinkingConfig(),
): ChatOptions {
const base = baseCallOptions(type);
if (!thinking.enabled) return base;
return { ...base, reasoningEffort: reasoningEffortFor(promptLength(messages), thinking) };
}
/** 把模型原文转成结构化结果 */
/**
* 解析结构化的报告草稿。