Module DsmlSource

dsml — encode DeepSeek-V4 prompts and parse model replies.

Synopsis

open Dsml

let prompt =
  encode_messages Thinking
    [ system "You are helpful."; user "What is 2+2?" ]

let reply = parse_message_from_completion_text Thinking completion

Description

A 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.

Tokens

The model's reserved marker strings.

Sourceval bos_token : string
Sourceval eos_token : string
Sourceval thinking_start_token : string
Sourceval thinking_end_token : string
Sourceval dsml_token : string
Sourceval user_token : string
Sourceval assistant_token : string
Sourceval latest_reminder_token : string
Sourcemodule Json : sig ... end

Types

Sourcetype thinking_mode =
  1. | Chat
  2. | Thinking

Reply directly, or reason in <think> first.

Sourcetype reasoning_effort =
  1. | High
  2. | Max

Max asks for maximum reasoning; High is accepted but inert.

Sourcetype task =
  1. | Action
  2. | Query
  3. | Authority
  4. | Domain
  5. | Title
  6. | Read_url

Quick-instruction tasks for DeepSeek's internal pipeline.

Sourcetype tool_call = {
  1. id : string option;
  2. name : string;
  3. arguments : string;
}

A tool call. arguments is a JSON object string.

Sourcetype parsed_message = {
  1. content : string;
  2. reasoning_content : string;
  3. tool_calls : tool_call list;
}

A parsed assistant turn.

Sourcetype message

A conversation message.

Sourceexception Parse_error of string

Malformed model output.

Messages

Sourceval system : ?tools:Json.t list -> ?response_format:Json.t -> string -> message

system content is a system turn; tools advertises callable tools.

Sourceval developer : ?tools:Json.t list -> ?response_format:Json.t -> string -> message

developer content is a developer turn; content must be non-empty.

Sourceval user : ?task:task -> string -> message

user content is a user turn.

Sourceval assistant : ?content:string -> ?reasoning_content:string -> ?tool_calls:tool_call list -> ?wo_eos:bool -> ?task:task -> unit -> message

assistant () is an assistant turn; wo_eos leaves it open for continued generation.

Sourceval tool : id:string -> string -> message

tool ~id result is a tool result for call id.

Sourceval latest_reminder : string -> message

latest_reminder content is a latest-reminder turn (date, locale).

Sourceval tool_call : ?id:string -> name:string -> arguments:string -> unit -> tool_call

tool_call ~name ~arguments () builds a tool call.

Encode

Sourceval encode_messages : ?context:message list -> ?drop_thinking:bool -> ?add_default_bos_token:bool -> ?reasoning_effort:reasoning_effort -> thinking_mode -> message list -> string

encode_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

Sourceval parse_message_from_completion_text : thinking_mode -> string -> parsed_message

parse_message_from_completion_text mode text parses one raw reply, trailing EOS included; raises Parse_error on malformed output.

Sourcemodule Codec : sig ... end

Tools

Sourcemodule Tool : sig ... end

Build the OpenAI tool objects passed to system and developer.

Sourcemodule Stream : sig ... end

Byte streams

Sourceval 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 -> unit

encode_messages_to_writer w mode messages renders the conversation to byte stream w.

Sourceval parse_message_from_reader : thinking_mode -> Bytesrw.Bytes.Reader.t -> parsed_message

parse_message_from_reader mode r parses one reply from byte stream r.