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
+66
View File
@@ -0,0 +1,66 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { chat, type LlmConfig } from "./client.js";
/**
* 只验证「请求体契约」:思考强度要真的写进 `reasoning_effort`,
* 不传时不能凭空加上该字段(否则换供应商会被当成非法参数)。
*/
const llmConfig: LlmConfig = {
apiUrl: "https://example.invalid/v1/chat/completions",
apiKey: "test-key",
model: "deepseek-flash",
timeoutMs: 5_000,
};
function mockFetchOnce() {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ choices: [{ message: { content: "ok" } }] }),
headers: new Headers(),
} as unknown as Response);
vi.stubGlobal("fetch", fetchMock);
return fetchMock;
}
function bodyOf(fetchMock: ReturnType<typeof mockFetchOnce>): Record<string, unknown> {
const init = fetchMock.mock.calls[0][1] as RequestInit;
return JSON.parse(String(init.body)) as Record<string, unknown>;
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("chat 请求体", () => {
it("传入 reasoningEffort 时写进 reasoning_effort", async () => {
const fetchMock = mockFetchOnce();
await chat(llmConfig, [{ role: "user", content: "hi" }], {
temperature: 0.2,
maxTokens: 1024,
reasoningEffort: "low",
});
expect(bodyOf(fetchMock)).toMatchObject({
model: "deepseek-flash",
temperature: 0.2,
max_tokens: 1024,
stream: false,
reasoning_effort: "low",
});
});
it("none 也要显式发送,用于关闭思考模式", async () => {
const fetchMock = mockFetchOnce();
await chat(llmConfig, [{ role: "user", content: "hi" }], { reasoningEffort: "none" });
expect(bodyOf(fetchMock).reasoning_effort).toBe("none");
});
it("不传 reasoningEffort 时不带该字段,交回供应商默认", async () => {
const fetchMock = mockFetchOnce();
await chat(llmConfig, [{ role: "user", content: "hi" }]);
expect(bodyOf(fetchMock)).not.toHaveProperty("reasoning_effort");
});
});