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
+13 -1
View File
@@ -7,11 +7,21 @@
* - 超时保护,避免长时间挂起
*/
import type { ReasoningEffort } from "./thinking.js";
export interface ChatMessage {
role: "system" | "user" | "assistant";
content: string;
}
/** 单次调用的采样参数:温度与输出上限按任务类型定,思考强度按 prompt 长度定 */
export interface ChatOptions {
temperature?: number;
maxTokens?: number;
/** 思考强度;不传时不带 `reasoning_effort`,由供应商按自己的默认值处理 */
reasoningEffort?: ReasoningEffort;
}
export interface LlmConfig {
apiUrl: string;
apiKey: string;
@@ -76,7 +86,7 @@ export function parseRetryAfter(value: string | null, now: number = Date.now()):
export async function chat(
config: LlmConfig,
messages: ChatMessage[],
options?: { temperature?: number; maxTokens?: number },
options?: ChatOptions,
): Promise<string> {
if (!isAvailable(config)) {
throw new LlmError("LLM API Key 未配置", 503);
@@ -98,6 +108,8 @@ export async function chat(
temperature: options?.temperature ?? 0.3,
max_tokens: options?.maxTokens ?? 2048,
stream: false,
// 思考强度交给调用方按内容长度决定;不传表示由供应商默认(DeepSeek 为 high)
...(options?.reasoningEffort ? { reasoning_effort: options.reasoningEffort } : {}),
}),
signal: controller.signal,
});