Archived
arch/common: DeepSeek-V4.1's reply format, DSML tool calls with typed argument tags
The checkpoint's own encoder writes an assistant turn as reasoning followed by a sectioned DSML call block, with each argument tag naming string or JSON. The template analysis cannot state those typed tags, so the format is declared as data and chatparse_test reads it back. Split from #266; no model plugin or kernels.
This commit is contained in:
1 parent
94c90b17ad
commit
9cf8d3dad8
2 files changed
+143
No files matched your search
@@ -0,0 +1,63 @@
|
|||||||
|
/* rad_deepseek41_chat.h -- DeepSeek-V4.1's reply format (reasoning and DSML tool calls) as data.
|
||||||
|
*
|
||||||
|
* The model's own encoder (encoding/encoding.py in the checkpoint) writes an assistant turn as
|
||||||
|
*
|
||||||
|
* [reasoning</think>]content\n\n<|DSML| calls>
|
||||||
|
* <|DSML| invoke name="NAME">
|
||||||
|
* <|DSML| parameter name="ARG" string="true">raw text</|DSML| parameter>
|
||||||
|
* <|DSML| parameter name="ARG" string="false">JSON</|DSML| parameter>
|
||||||
|
* </|DSML| invoke>
|
||||||
|
* ...
|
||||||
|
* </|DSML| calls><|end▁of▁sentence|>
|
||||||
|
*
|
||||||
|
* Every call sits in one section, each argument's tag says whether its value is a string or JSON,
|
||||||
|
* and the tag names carry a leading space (V4 wrote `<|DSML|tool_calls>`, V3.2
|
||||||
|
* `<|DSML|function_calls>`). The generation prompt ends in `<think>` with thinking on and in a
|
||||||
|
* bare `</think>` with it off, so the reply starts inside the block only when the prompt opened
|
||||||
|
* it.
|
||||||
|
*
|
||||||
|
* The template analysis cannot state the typed argument tags, so the plugin declares the format;
|
||||||
|
* the reply-format tests read this same declaration. */
|
||||||
|
#pragma once
|
||||||
|
#include "rad_builder.h"
|
||||||
|
|
||||||
|
namespace rad {
|
||||||
|
namespace arch {
|
||||||
|
|
||||||
|
inline const RadChatFormat* deepseek41_chat_format() {
|
||||||
|
static const char* const kBreaks[] = { "<|DSML| calls>" };
|
||||||
|
static const char* const kTriggers[] = { "<|DSML| calls>" };
|
||||||
|
static const RadChatFormat f = [] {
|
||||||
|
RadChatFormat c{};
|
||||||
|
c.struct_size = (uint32_t)sizeof(RadChatFormat);
|
||||||
|
c.name = "plugin:deepseek41";
|
||||||
|
c.reasoning_open = "<think>";
|
||||||
|
c.reasoning_close = "</think>";
|
||||||
|
/* A section that starts inside an unclosed reasoning block ends it there. */
|
||||||
|
c.reasoning_breaks = kBreaks;
|
||||||
|
c.n_reasoning_breaks = 1;
|
||||||
|
c.reasoning_in_prompt = RAD_CHAT_THINK_FROM_PROMPT;
|
||||||
|
|
||||||
|
c.section_open = "<|DSML| calls>";
|
||||||
|
c.section_close = "</|DSML| calls>";
|
||||||
|
c.name_prefix = "<|DSML| invoke name=\"";
|
||||||
|
c.name_suffix = "\">\n";
|
||||||
|
c.call_end = "</|DSML| invoke>";
|
||||||
|
c.arg_prefix = "<|DSML| parameter name=\"";
|
||||||
|
c.arg_name_suffix = "\"";
|
||||||
|
c.arg_value_prefix = " string=\"true\">";
|
||||||
|
c.arg_json_value_prefix = " string=\"false\">";
|
||||||
|
c.arg_value_suffix = "</|DSML| parameter>";
|
||||||
|
c.string_value = RAD_CHAT_VALUE_RAW;
|
||||||
|
c.other_value = RAD_CHAT_VALUE_JSON;
|
||||||
|
c.value_newlines = 0;
|
||||||
|
|
||||||
|
c.triggers = kTriggers;
|
||||||
|
c.n_triggers = 1;
|
||||||
|
return c;
|
||||||
|
}();
|
||||||
|
return &f;
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* namespace arch */
|
||||||
|
} /* namespace rad */
|
||||||
@@ -32,6 +32,8 @@
|
|||||||
#include "text/chatfmt.h"
|
#include "text/chatfmt.h"
|
||||||
#include "text/chatparse.h"
|
#include "text/chatparse.h"
|
||||||
|
|
||||||
|
#include <arch/rad_deepseek41_chat.h>
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -2019,6 +2021,84 @@ TEST(typed_argument_tags_are_refused_where_they_cannot_be_read) {
|
|||||||
CHECK(g.arg_json_value_prefix.empty());
|
CHECK(g.arg_json_value_prefix.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* DeepSeek-V4.1's declared format reads what the model's encoder (encoding/encoding.py) writes for
|
||||||
|
* an assistant turn: test_encoding.py's V41_TOOL_CALL_OUTPUT and the calls of golden case 1. */
|
||||||
|
TEST(deepseek41_reads_the_dsml_its_encoder_writes) {
|
||||||
|
ChatFormat f;
|
||||||
|
std::string why;
|
||||||
|
CHECK_OK(chat_format_from_abi(rad::arch::deepseek41_chat_format(), &f, &why));
|
||||||
|
CHECK_OK(chat_format_check(f, &why));
|
||||||
|
const json tools = json::parse(R"([{"type":"function","function":{"name":"lookup",
|
||||||
|
"parameters":{"type":"object","properties":{"query":{"type":"string"},"limit":{"type":"integer"}}}}},
|
||||||
|
{"type":"function","function":{"name":"get_weather","parameters":{"type":"object",
|
||||||
|
"properties":{"location":{"type":"string"},"unit":{"type":"string","enum":["celsius","fahrenheit"]}},
|
||||||
|
"required":["location"]}}}])");
|
||||||
|
ReplyOptions ro;
|
||||||
|
ro.tools = &tools;
|
||||||
|
ro.generation_prompt = "<|Assistant|><think>";
|
||||||
|
const auto thinking = parser_of(f, ro);
|
||||||
|
REQUIRE(thinking != nullptr);
|
||||||
|
const std::string v41 =
|
||||||
|
" reason </think>summary\n\n"
|
||||||
|
"<|DSML| calls>\n"
|
||||||
|
"<|DSML| invoke name=\"lookup\">\n"
|
||||||
|
"<|DSML| parameter name=\"query\" string=\"true\">value</|DSML| parameter>\n"
|
||||||
|
"<|DSML| parameter name=\"limit\" string=\"false\">2</|DSML| parameter>\n"
|
||||||
|
"</|DSML| invoke>\n"
|
||||||
|
"</|DSML| calls>";
|
||||||
|
bool strict = false;
|
||||||
|
const ReplyMessage m = reply_parse(*thinking, v41, &strict);
|
||||||
|
CHECK(strict);
|
||||||
|
CHECK_EQ(m.reasoning, std::string(" reason "));
|
||||||
|
CHECK_EQ(m.content, std::string("summary"));
|
||||||
|
REQUIRE(m.calls.size() == 1);
|
||||||
|
CHECK_EQ(m.calls[0].name, std::string("lookup"));
|
||||||
|
CHECK_EQ(m.calls[0].arguments, std::string("{\"query\":\"value\",\"limit\":2}"));
|
||||||
|
CHECK(m.calls[0].closed);
|
||||||
|
std::string err = stream_all(*thinking, v41);
|
||||||
|
if (!err.empty()) ::radtest::fail(__FILE__, __LINE__, err);
|
||||||
|
|
||||||
|
/* Thinking off: the prompt closed the block, and calls follow the (here empty) answer. */
|
||||||
|
ro.generation_prompt = "<|Assistant|></think>";
|
||||||
|
const auto chat = parser_of(f, ro);
|
||||||
|
REQUIRE(chat != nullptr);
|
||||||
|
const std::string golden1 =
|
||||||
|
"\n\n<|DSML| calls>\n"
|
||||||
|
"<|DSML| invoke name=\"get_weather\">\n"
|
||||||
|
"<|DSML| parameter name=\"location\" string=\"true\">Beijing</|DSML| parameter>\n"
|
||||||
|
"<|DSML| parameter name=\"unit\" string=\"true\">celsius</|DSML| parameter>\n"
|
||||||
|
"</|DSML| invoke>\n"
|
||||||
|
"<|DSML| invoke name=\"lookup\">\n"
|
||||||
|
"<|DSML| parameter name=\"query\" string=\"true\">北京 天气</|DSML| parameter>\n"
|
||||||
|
"</|DSML| invoke>\n"
|
||||||
|
"</|DSML| calls>";
|
||||||
|
const ReplyMessage g = reply_parse(*chat, golden1, &strict);
|
||||||
|
CHECK(strict);
|
||||||
|
CHECK_EQ(g.content, std::string());
|
||||||
|
CHECK_EQ(g.reasoning, std::string());
|
||||||
|
REQUIRE(g.calls.size() == 2);
|
||||||
|
CHECK_EQ(g.calls[0].arguments, std::string("{\"location\":\"Beijing\",\"unit\":\"celsius\"}"));
|
||||||
|
CHECK_EQ(g.calls[1].arguments, std::string("{\"query\":\"北京 天气\"}"));
|
||||||
|
err = stream_all(*chat, golden1);
|
||||||
|
if (!err.empty()) ::radtest::fail(__FILE__, __LINE__, err);
|
||||||
|
|
||||||
|
/* No calls: the answer is content. A call tag outside the section is prose. */
|
||||||
|
const ReplyMessage plain = reply_parse(*chat, "Paris.");
|
||||||
|
CHECK_EQ(plain.content, std::string("Paris."));
|
||||||
|
CHECK(plain.calls.empty());
|
||||||
|
const std::string quoted = "Write <|DSML| invoke name=\"lookup\"> to call it.";
|
||||||
|
const ReplyMessage q = reply_parse(*chat, quoted);
|
||||||
|
CHECK(q.calls.empty());
|
||||||
|
CHECK_EQ(q.content, quoted);
|
||||||
|
|
||||||
|
ChatGrammarRequest gq;
|
||||||
|
gq.tools = &tools;
|
||||||
|
ChatGrammar gr;
|
||||||
|
CHECK_OK(chat_format_grammar(f, gq, &gr, &why));
|
||||||
|
CHECK(gr.lazy);
|
||||||
|
CHECK(gr.trigger_words == std::vector<std::string>{ "<|DSML| calls>" });
|
||||||
|
}
|
||||||
|
|
||||||
TEST(json_arguments_behind_a_name_tag_read_as_one_object) {
|
TEST(json_arguments_behind_a_name_tag_read_as_one_object) {
|
||||||
std::shared_ptr<const ReplyParser> p = object_format(false);
|
std::shared_ptr<const ReplyParser> p = object_format(false);
|
||||||
REQUIRE(p != nullptr);
|
REQUIRE(p != nullptr);
|
||||||
|
|||||||
Reference in new issue
Block a user