DsmlSourcedsml — encode DeepSeek-V4 prompts and parse model replies.
open Dsml
let prompt =
encode_messages Thinking
[ system "You are helpful."; user "What is 2+2?" ]
let reply = parse_message_from_completion_text Thinking completionA conversation is a list of messages — system, user, assistant, and tool results. encode_messages renders them into the prompt string a DeepSeek-V4 model expects; parse_message_from_completion_text turns one reply back into its text, reasoning, and tool calls. Use Chat for a direct answer or Thinking to have the model reason first.
Tool calls travel as DSML, the model's tool-call markup:
<|DSML|tool_calls> <|DSML|invoke name="edit"> <|DSML|parameter name="path" string="true">/tmp/x.c</|DSML|parameter> <|DSML|parameter name="line" string="false">42</|DSML|parameter> </|DSML|invoke> </|DSML|tool_calls>
string="true" is a raw-text value; string="false" is JSON. It looks like XML but is not — parameter bodies are raw text. Tool results are supplied as tool messages and fold into the preceding turn. Tool schemas and tool-call arguments are Json.t values (jsont's representation); the tool-call grammar itself is the bidirectional combinator codec Codec.
The model's reserved marker strings.
Reply directly, or reason in <think> first.
Max asks for maximum reasoning; High is accepted but inert.
Quick-instruction tasks for DeepSeek's internal pipeline.
A tool call. arguments is a JSON object string.
type parsed_message = {content : string;reasoning_content : string;tool_calls : tool_call list;}A parsed assistant turn.
A conversation message.
Malformed model output.
system content is a system turn; tools advertises callable tools.
developer content is a developer turn; content must be non-empty.
val assistant :
?content:string ->
?reasoning_content:string ->
?tool_calls:tool_call list ->
?wo_eos:bool ->
?task:task ->
unit ->
messageassistant () is an assistant turn; wo_eos leaves it open for continued generation.
latest_reminder content is a latest-reminder turn (date, locale).
tool_call ~name ~arguments () builds a tool call.
val encode_messages :
?context:message list ->
?drop_thinking:bool ->
?add_default_bos_token:bool ->
?reasoning_effort:reasoning_effort ->
thinking_mode ->
message list ->
stringencode_messages mode messages renders the conversation to the prompt string. context prepends an already-encoded prefix and suppresses the leading token; drop_thinking (default true) drops reasoning from turns before the last user message; reasoning_effort Max maximises reasoning.
parse_message_from_completion_text mode text parses one raw reply, trailing EOS included; raises Parse_error on malformed output.
val encode_messages_to_writer :
Bytesrw.Bytes.Writer.t ->
?context:message list ->
?drop_thinking:bool ->
?add_default_bos_token:bool ->
?reasoning_effort:reasoning_effort ->
thinking_mode ->
message list ->
unitencode_messages_to_writer w mode messages renders the conversation to byte stream w.
parse_message_from_reader mode r parses one reply from byte stream r.