DeepSeek-V4 编码格式详解
2026/7/5 17:11:19 网站建设 项目流程

DeepSeek-V4 编码格式详解

【免费下载链接】DeepSeek-V4-FlashDeepSeek-V4-Flash(总参数 284B,激活 13B)主打极致性价比,推理成本仅为前代的十分之一,适合高频对话和大规模部署。两个版本均支持 Thinking/Non-Thinking 双模式,通过创新的混合注意力架构(CSA+HCA)实现 1M 上下文下 10 倍以上的推理效率提升。项目地址: https://ai.gitcode.com/hf_mirrors/deepseek-ai/DeepSeek-V4-Flash

概述

DeepSeek-V4 系列模型采用特定的编码格式来处理多轮对话、工具调用、推理过程和快速指令任务。本文档详细介绍了该编码格式的实现细节。

快速开始

from encoding_dsv4 import encode_messages, parse_message_from_completion_text # 编码对话 messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is 2+2?"}, ] prompt = encode_messages(messages, thinking_mode="thinking") # 解析模型输出回结构化消息 completion = "Simple arithmetic.</think>2 + 2 = 4." parsed = parse_message_from_completion_text(completion, thinking_mode="thinking") # => {"role": "assistant", "reasoning_content": "Simple arithmetic.", "content": "2 + 2 = 4.", "tool_calls": []}

注意:parse_message_from_completion_text函数仅设计用于处理格式良好的模型输出。它不会尝试纠正或恢复模型可能偶尔生成的格式错误的输出。对于生产环境使用,建议添加额外的错误处理。

消息格式

特殊令牌

令牌用途
<|begin▁of▁sentence|>序列开始 (BOS)
<|end▁of▁sentence|>助手回合结束 (EOS)
Human用户回合前缀
Assistant助手回合前缀
<|latest_reminder|>最新提醒(日期、区域设置等)
</think>/<think>推理块分隔符
|DSML|DSML 标记令牌

角色

编码支持以下消息角色:systemuserassistanttoollatest_reminderdeveloper

关于developer角色的说明:developer角色专门用于内部搜索代理管道。对于通用聊天或工具调用任务不需要此角色,官方 API 也不接受具有此角色的消息。

基础聊天

简单的多轮对话编码如下:

<|begin▁of▁sentence|>Human{user_message}<|end▁of▁sentence|>Assistant{response}<|end▁of▁sentence|>Human{user_message_2}<|end▁of▁sentence|>Assistant{response_2}<|end▁of▁sentence|>
  • BOS 令牌在对话的最开始添加。
  • 聊天模式thinking_mode="chat")中,</think>紧接在Assistant之后放置,以立即关闭思考块,使模型直接生成内容。

交错推理模式

推理模式thinking_mode="thinking")中,模型在响应之前在</think>...<think>块中产生显式推理。

<|begin▁of▁sentence|>Human{message}<|end▁of▁sentence|>Assistant</think>{reasoning}<think>{response}<|end▁of▁sentence|>

drop_thinking参数(默认为True)控制是否保留先前回合的推理:

  • 没有工具时drop_thinking生效。从最后一个用户消息之前的助手回合中剥离推理内容。只有最终的助手回合保留其</think>...<think>块。
  • 使用工具时(在系统或开发者消息上):drop_thinking自动禁用。所有回合都保留其推理,因为工具调用对话需要完整的上下文来跟踪跨工具调用的多步推理。

工具调用(DSML 格式)

工具通过tools字段(OpenAI 兼容格式)在systemdeveloper消息上定义。当工具存在时,以下模式块被注入到系统/用户提示中:

## Tools You have access to a set of tools to help answer the user's question. You can invoke tools by writing a "<|DSML|tool_calls>" block like the following: <|DSML|tool_calls> <|DSML|invoke name="$TOOL_NAME"> <|DSML|parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</|DSML|parameter> ... </|DSML|invoke> <|DSML|invoke name="$TOOL_NAME2"> ... </|DSML|invoke> </|DSML|tool_calls> String parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`. If thinking_mode is enabled (triggered by </think>), you MUST output your complete reasoning inside </think>...<think> BEFORE any tool calls or final response. Otherwise, output directly after <think> with tool calls or final response. ### Available Tool Schemas {tool_definitions_json} You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.

助手回合中的实际工具调用如下所示:

<|DSML|tool_calls> <|DSML|invoke name="function_name"> <|DSML|parameter name="param" string="true">string_value</|DSML|parameter> <|DSML|parameter name="count" string="false">5</|DSML|parameter> </|DSML|invoke> </|DSML|tool_calls>
  • string="true":参数值是原始字符串。
  • string="false":参数值是 JSON(数字、布尔值、数组、对象)。

工具执行结果包装在用户消息内的<tool_result>标签中:

Human<tool_result>{result_json}</tool_result><|end▁of▁sentence|>Assistant</think>...

当存在多个工具结果时,它们按照前一个助手消息中相应tool_calls的顺序排序。

推理努力程度

当设置reasoning_effort="max"时,在提示的最开始(系统消息之前)会添加一个特殊前缀,指示模型最大化其推理深度:

Reasoning Effort: Absolute maximum with no shortcuts permitted. You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios. Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.

快速指令特殊令牌

快速指令令牌用于辅助分类和生成任务。它们通过"task"字段附加到消息中,以触发针对单令牌或短格式输出的专门模型行为。

特殊令牌描述格式
<|action|>确定用户提示是否需要网络搜索或可以直接回答。...Human{prompt}<|end▁of▁sentence|>Assistant</think><|action|>
<|title|>在第一个助手响应后生成简洁的对话标题。...Assistant{response}<|end▁of▁sentence|><|title|>
<|query|>为用户提示生成搜索查询。...Human{prompt}<|query|>
<|authority|>对用户提示对来源权威性的需求进行分类。...Human{prompt}<|authority|>
<|domain|>识别用户提示的领域。...Human{prompt}<|domain|>
<|extracted_url|><|read_url|>确定用户提示中的每个 URL 是否应该被获取和读取。...Human{prompt}<|extracted_url|>{url}<|read_url|>

在消息格式中的使用:

  • 用户消息上的action<|action|>令牌放置在助手前缀和思考令牌之后,触发路由决策(例如,"Search" 或 "Answer")。
  • 用户消息上的其他任务queryauthoritydomainread_url):任务令牌直接附加在用户内容之后。
  • 助手消息上的title<|title|>令牌附加在助手的 EOS 之后。下一个助手消息提供生成的标题。

【免费下载链接】DeepSeek-V4-FlashDeepSeek-V4-Flash(总参数 284B,激活 13B)主打极致性价比,推理成本仅为前代的十分之一,适合高频对话和大规模部署。两个版本均支持 Thinking/Non-Thinking 双模式,通过创新的混合注意力架构(CSA+HCA)实现 1M 上下文下 10 倍以上的推理效率提升。项目地址: https://ai.gitcode.com/hf_mirrors/deepseek-ai/DeepSeek-V4-Flash

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询