Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions Memory/src/model/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class HttpLlmClient implements LlmClient {
const thinkingBudget = thinking.enabled && thinkingUsesEnableThinking(this.config.vendor ?? "", base, model)
? this.config.thinkingBudget
: undefined;
const thinkingPayload = openAiCompatibleThinkingPayload(thinking, this.config.extraBody);
const agentRegion = resolveMemoryAgentRegion(this.config.sourceProvider);
const response = await postJsonWithRetry<OpenAiChatResponse>({
actualModelContext: this.config.actualModelContext,
Expand All @@ -323,10 +324,10 @@ class HttpLlmClient implements LlmClient {
...(!omitTemperature ? { temperature: options.temperature ?? this.config.temperature } : {}),
max_tokens: options.maxTokens ?? this.config.maxTokens,
stream: false,
...thinking.fields,
...thinkingPayload.fields,
...(thinkingBudget !== undefined ? { thinking_budget: thinkingBudget } : {}),
...(options.jsonMode && !omitJsonMode ? { response_format: { type: "json_object" } } : {}),
...(this.config.extraBody ?? {})
...thinkingPayload.extraBody
}
});
const choice = response.choices?.[0];
Expand Down Expand Up @@ -561,6 +562,39 @@ class HttpLlmClient implements LlmClient {
}
}

function openAiCompatibleThinkingPayload(
thinking: ThinkingControl,
configuredExtraBody: Record<string, unknown> | undefined
): { fields: Record<string, unknown>; extraBody: Record<string, unknown> } {
const extraBody = configuredExtraBody ?? {};
const configuredChatTemplateKwargs = extraBody.chat_template_kwargs;
if (
!("enable_thinking" in thinking.fields) ||
!isUnknownRecord(configuredChatTemplateKwargs)
) {
return { fields: thinking.fields, extraBody };
}

const fields = { ...thinking.fields };
delete fields.enable_thinking;
return {
fields,
extraBody: {
...extraBody,
chat_template_kwargs: {
...configuredChatTemplateKwargs,
...(!Object.hasOwn(configuredChatTemplateKwargs, "enable_thinking")
? { enable_thinking: thinking.enabled }
: {})
}
}
};
}

function isUnknownRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}

function openAiCompatibleThinkingControl(input: {
vendor: string;
endpoint: string;
Expand Down
33 changes: 33 additions & 0 deletions Memory/tests/llm-thinking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ describe("memory LLM thinking configuration", () => {
expect(requestBody(fetchMock)).not.toHaveProperty("thinking_budget");
});

it("routes Qwen thinking through chat_template_kwargs when the endpoint opts in", async () => {
const fetchMock = openAiFetch();
vi.stubGlobal("fetch", fetchMock);
const client = createLlmClient(llmConfig({
vendor: "qwen",
endpoint: "https://sglang.example/v1",
model: "qwen3.8-27b",
enableThinking: true,
extraBody: { chat_template_kwargs: { tokenizer_option: "preserved" } }
}));

await client.complete([{ role: "user", content: "filter" }], {
operation: "retrieval.filter",
thinkingMode: "disabled"
});
expect(requestBody(fetchMock)).toMatchObject({
chat_template_kwargs: {
enable_thinking: false,
tokenizer_option: "preserved"
}
});
expect(requestBody(fetchMock)).not.toHaveProperty("enable_thinking");

fetchMock.mockClear();
await client.complete([{ role: "user", content: "evolve" }], {
operation: "evolution.induction",
thinkingMode: "enabled"
});
expect(requestBody(fetchMock)).toMatchObject({
chat_template_kwargs: { enable_thinking: true }
});
});

it.each([
["DeepSeek", "deepseek", "https://api.deepseek.com", "deepseek-v4-pro"],
["Zhipu", "zhipu", "https://open.bigmodel.cn/api/paas/v4", "glm-5.1"],
Expand Down