Nodes/ComfyUI-ZML-Image/ZML_LLM 过滤思考
ComfyUI Node

ZML_LLM 过滤思考

Strip the reasoning out of your LLM reply

By zml-w·Created about a year ago·Updated 2 months ago· 218
ZML_LLM 过滤思考
    • 过滤后文本
    input_text

    Reasoning models leak. A lot of them - DeepSeek-R1 and its cousins - think out loud inside <think>...</think> tags before answering, and unless you strip that scaffolding, the "reply" your workflow receives is a wall of the model's internal deliberation with the actual answer buried at the bottom. ZML_LLM_ThoughtFilter is the ZML pack's one-trick fix: input_text in, 过滤后文本 (filtered text) out, with everything between the think tags removed.

    The mechanism is a single regex under the hood: re.sub(r'<think>.*?</think>', '', input_text, re.DOTALL). So it's not an LLM doing the filtering, it's a plain text operation, and it's about as reliable as a regex on XML-ish tags gets. The .*? is non-greedy, which matters: with nested or multiple think blocks it stops at the first closing tag, and DOTALL means it catches multiline reasoning. Anything outside the tags passes through untouched.

    When would you actually want this? If you're running DeepSeek-chat or another model that emits reasoning, and you're feeding the reply into a JSON extractor, a text-block saver, or a prompt encoder. Dangling think-tags will break json.loads downstream, and they look awful in saved captions. It's also just the right habit for the KB's llm-in-comfyui warning: reasoning models are more likely to leak scratch-work into output you didn't ask for, so the filter is a cheap belt-and-suspenders layer even when the API doesn't give you a clean "content only" mode.

    The honest limitations: it only knows the <think> convention. Models that emit reasoning in a different wrapper (some use [REASONING], some use markdown blocks, some just ramble without tags) won't be touched. And if the whole reply is inside one unclosed think block, the regex won't match and you get everything back - which is arguably the safe failure. If you find yourself needing more, this is a sign you want a proper "extract just the answer" node (the pack's JSON extractor does a similar cleanup for JSON replies).

    It's a 200-word node and happy to be one. Single input, single output, no model, no API, no config. Install the pack once:

    cd ComfyUI/custom_nodes
    git clone https://github.com/zml-w/ComfyUI-ZML-Image
    

    and it appears under ZML 图像 → LLM alongside the rest. If your provider already returns clean content, you never need it - but the first time a DeepSeek reply dumps its entire chain-of-thought into your prompt text, you'll be glad it's there.

    Categoryimage/ZML_图像/LLM

    Inputs (1)

    NameTypeDefaultDescription
    input_textSTRING

    Outputs (1)

    NameTypeDescription
    过滤后文本STRING