common/chat: fix DeepSeek V4 parsing/encoding - #25891
Conversation
|
@tarruda What benchmark do you recommend for testing DeepSeek V4 chat template? |
|
@fairydreaming I don't use anything scientific, but there's a personal test that I have been using a lot to evaluate local models. It is an investigation of llama.cpp codebase that is fully detailed in this issue description. |
|
Testing this branch, parallel tool call seems to work fine in pi agent. |
aldehir
left a comment
There was a problem hiding this comment.
It's pretty sloppy.
I say split this up into a separate v4 function and remove all code that exists outside of it, except for the template/test/cap changes.
I did say it was mostly LLM written, so thank you for confirming that. I'm totally fine with closing and letting someone else with actual understanding of how things work here deal with this. Truth be told: If I continue, all I'm going to do is pass your comments to Codex. If that is not acceptable I can close this immediately. |
|
I personally don't mind, especially since I don't have the hardware to run this model. That said, @pwilkin is probably the better person to review since he can. |
Add dedicated DeepSeek V4 handling for its DSML tool format, reasoning modes, and structured responses while retaining V3.2 compatibility. - Opt V4 templates out of differential autoparsing and use the specialized DSML parser. - Parse reasoning and DSML tool calls incrementally from streamed output. - Recognize DSML tool calls encountered before the reasoning end marker. - Encode typed and empty arguments and sort results by tool-call order. - Validate required and unique arguments in arbitrary order with linear parser construction. - Support Max reasoning, preserved thinking history, and chat-mode continuations. - Inject response schemas and allow raw JSON or tool calls for structured output. - Expand V4 prompt, parser, grammar, and reconstruction coverage. These changes align DSv4 parsing/encoding to the official implementation here: https://huggingface.co/deepseek-ai/DeepSeek-V4-Flash/tree/main/encoding Assisted-by: Codex
16cfdcf to
290898e
Compare
|
@aldehir I'm curious about #25544: The main reason I understood it was necessary to opt out of the autoparser was because it was possible to invoke tools without ending the |
I think we'll need a bit more experimentation to figure out how to integrate it with the autoparser. It won't work OOTB at the moment, so using it in a dedicated parser (or two) will help show us what that might look like. |
|
Just FYI I also faced an issue where the model just outputted plain DSML in the output. So there does seem to be some issue here, but I'm not able to reproduce reliably. |
Were you able to reproduce it in this branch? Codex did manage to reproduce a regression and fix it in this branch. IIRC this was the test that reproduced it: I had a similar experience where it would sometimes cause this in the middle of an agentic session, but after that fix I didn't see it again. |
|
FYI the DeepSeek4 dedicated parser fixes are finally merged :) |
Closing it then. There are still two things missing from the template:
But since it is only template tweaks, it doesn't need to go to llama.cpp |
|
@tarruda yeah that's because of the wrong variable name, did a quick PR to fix it. |
Overview
Align DSv4 parsing/encoding to the official implementation here: https://huggingface.co/deepseek-ai/DeepSeek-V4-Flash/tree/main/encoding. Main user visible fixes:
--reasoning-preserve. In the official DSv4 encoding, the drop_thinking option controls this. Here we translate to "preserve_thinking" for--reasoning-preservecompatibility.Additional information
To do this, I've had to opt-out of the autoparser. AFAICT Deepseek is different enough in how it handles tool calls within
<think>blocks to require this, but I could be missing something. (cc @pwilkin @aldehir @fairydreaming @am17an)This fixes Deepseek V4 Flash for agentic multi-turn usage for me.
Requirements