hf2q 0.1.1

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Pure-Rust DeepSeek-V4-Flash-0731 conversation encoding.
//!
//! This is a direct behavioral port of the model repository's
//! `encoding/encoding_dsv4.py`. It intentionally does not execute Python or
//! delegate prompt construction to another process. The encoder owns the
//! stateful pieces that a conventional chat Jinja template cannot express
//! reliably: merging OpenAI tool messages, preserving tool-call order,
//! dropping old reasoning, reasoning effort, and quick-instruction tasks.

mod parser;
#[cfg(test)]
mod tests;
mod types;

pub use parser::{
    parse_completion, parse_tool_calls_body, ParsedAssistant, ParsedToolCall,
    ParsedToolCallFunction,
};
pub use types::{Message, OrderedValue, ToolCall, ToolCallFunction, ToolDefinition};

use std::collections::HashMap;

use types::ContentBlock;

pub const BOS: &str = "<|begin▁of▁sentence|>";
pub const EOS: &str = "<|end▁of▁sentence|>";
pub const USER: &str = "<|User|>";
pub const ASSISTANT: &str = "<|Assistant|>";
pub const LATEST_REMINDER: &str = "<|latest_reminder|>";
pub const THINK_START: &str = "<think>";
pub const THINK_END: &str = "</think>";
pub const DSML: &str = "|DSML|";

const EFFORT_HIGH: &str = "Reasoning Effort: Absolute maximum with no shortcuts permitted.\nYou 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.\nExplicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n";
const EFFORT_MAX: &str = "Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\nYou MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.\nDo not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.\n\n";

const TOOLS_HEAD: &str = "## Tools\n\nYou 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:\n\n<|DSML|tool_calls>\n<|DSML|invoke name=\"$TOOL_NAME\">\n<|DSML|parameter name=\"$PARAMETER_NAME\" string=\"true|false\">$PARAMETER_VALUE</|DSML|parameter>\n...\n</|DSML|invoke>\n<|DSML|invoke name=\"$TOOL_NAME2\">\n...\n</|DSML|invoke>\n</|DSML|tool_calls>\n\nString 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\"`.\n\nIf thinking_mode is enabled (triggered by <think>), you MUST output your complete reasoning inside <think>...</think> BEFORE any tool calls or final response.\n\nOtherwise, output directly after </think> with tool calls or final response.\n\n### Available Tool Schemas\n\n";
const TOOLS_FOOT: &str = "\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n";

#[derive(Debug, thiserror::Error)]
pub enum EncodingError {
    #[error("invalid DeepSeek-V4 message JSON: {0}")]
    Json(#[from] serde_json::Error),
    #[error("DeepSeek-V4 message {index} has unsupported role {role:?}")]
    Role { index: usize, role: String },
    #[error("DeepSeek-V4 message {index}: {detail}")]
    Invalid { index: usize, detail: String },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThinkingMode {
    Chat,
    Thinking,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ReasoningEffort {
    #[default]
    Low,
    High,
    Max,
}

#[derive(Debug, Clone, Copy)]
pub struct EncodeOptions {
    pub thinking_mode: ThinkingMode,
    pub drop_thinking: bool,
    pub add_bos: bool,
    pub reasoning_effort: ReasoningEffort,
}

impl Default for EncodeOptions {
    fn default() -> Self {
        Self {
            thinking_mode: ThinkingMode::Chat,
            drop_thinking: true,
            add_bos: true,
            reasoning_effort: ReasoningEffort::Low,
        }
    }
}

pub fn messages_from_json(json: &str) -> Result<Vec<Message>, EncodingError> {
    Ok(serde_json::from_str(json)?)
}

/// Encode official-message JSON without a Python runtime.
pub fn encode_json(json: &str, options: EncodeOptions) -> Result<String, EncodingError> {
    encode_messages(&messages_from_json(json)?, options)
}

pub fn encode_messages(
    messages: &[Message],
    options: EncodeOptions,
) -> Result<String, EncodingError> {
    let mut messages = merge_tool_messages(messages);
    sort_tool_results(&mut messages);
    let mut drop_thinking = options.drop_thinking;
    if messages.iter().any(|m| !m.tools.is_empty()) {
        drop_thinking = false;
    }
    if options.thinking_mode == ThinkingMode::Thinking && drop_thinking {
        messages = drop_old_thinking(messages);
    }

    let mut out = String::new();
    if options.add_bos {
        out.push_str(BOS);
    }
    for i in 0..messages.len() {
        render_message(&mut out, i, &messages, options, drop_thinking)?;
    }
    Ok(out)
}

fn merge_tool_messages(messages: &[Message]) -> Vec<Message> {
    let mut out: Vec<Message> = Vec::new();
    for original in messages {
        let mut msg = original.clone();
        match msg.role.as_str() {
            "tool" => {
                let block = ContentBlock::ToolResult {
                    id: msg.tool_call_id.clone().unwrap_or_default(),
                    content: tool_content_text(&msg),
                };
                if out
                    .last()
                    .is_some_and(|m| m.role == "user" && !m.content_blocks.is_empty())
                {
                    out.last_mut().unwrap().content_blocks.push(block);
                } else {
                    msg.role = "user".into();
                    msg.content = None;
                    msg.content_blocks = vec![block];
                    out.push(msg);
                }
            }
            "user" => {
                let text = content_text(&msg);
                let block = ContentBlock::Text(text);
                if out.last().is_some_and(|m| {
                    m.role == "user" && !m.content_blocks.is_empty() && m.task.is_none()
                }) {
                    out.last_mut().unwrap().content_blocks.push(block);
                } else {
                    msg.content_blocks = vec![block];
                    out.push(msg);
                }
            }
            _ => out.push(msg),
        }
    }
    out
}

fn sort_tool_results(messages: &mut [Message]) {
    let mut order: HashMap<String, usize> = HashMap::new();
    for msg in messages {
        if msg.role == "assistant" && !msg.tool_calls.is_empty() {
            order.clear();
            for (i, tc) in msg.tool_calls.iter().enumerate() {
                if let Some(id) = &tc.id {
                    order.insert(id.clone(), i);
                }
            }
        } else if msg.role == "user"
            && msg
                .content_blocks
                .iter()
                .filter(|b| matches!(b, ContentBlock::ToolResult { .. }))
                .count()
                > 1
        {
            let mut results: Vec<_> = msg
                .content_blocks
                .iter()
                .filter_map(|b| {
                    if matches!(b, ContentBlock::ToolResult { .. }) {
                        Some(b.clone())
                    } else {
                        None
                    }
                })
                .collect();
            results.sort_by_key(|b| match b {
                ContentBlock::ToolResult { id, .. } => order.get(id).copied().unwrap_or(0),
                _ => 0,
            });
            let mut it = results.into_iter();
            for b in &mut msg.content_blocks {
                if matches!(b, ContentBlock::ToolResult { .. }) {
                    *b = it.next().expect("same tool block count");
                }
            }
        }
    }
}

fn drop_old_thinking(messages: Vec<Message>) -> Vec<Message> {
    let last = last_user_index(&messages);
    messages
        .into_iter()
        .enumerate()
        .filter_map(|(i, mut m)| {
            if matches!(
                m.role.as_str(),
                "user" | "system" | "tool" | "latest_reminder" | "direct_search_results"
            ) || i as isize >= last
            {
                Some(m)
            } else if m.role == "assistant" {
                m.reasoning_content = None;
                Some(m)
            } else {
                None
            }
        })
        .collect()
}

fn last_user_index(messages: &[Message]) -> isize {
    messages
        .iter()
        .rposition(|m| matches!(m.role.as_str(), "user" | "developer"))
        .map(|i| i as isize)
        .unwrap_or(-1)
}

fn content_text(msg: &Message) -> String {
    match msg.content.as_ref() {
        Some(OrderedValue::String(v)) => v.clone(),
        Some(OrderedValue::Array(parts)) => parts
            .iter()
            .filter_map(|part| ordered_object_string(part, "text"))
            .collect::<String>(),
        _ => String::new(),
    }
}

fn tool_content_text(msg: &Message) -> String {
    match msg.content.as_ref() {
        Some(OrderedValue::String(v)) => v.clone(),
        Some(OrderedValue::Array(parts)) => parts
            .iter()
            .map(|part| {
                let kind = ordered_object_string(part, "type").unwrap_or("unknown");
                if kind == "text" {
                    ordered_object_string(part, "text")
                        .unwrap_or("")
                        .to_string()
                } else {
                    format!("[Unsupported {kind}]")
                }
            })
            .collect::<Vec<_>>()
            .join("\n\n"),
        _ => String::new(),
    }
}

fn ordered_object_string<'a>(value: &'a OrderedValue, key: &str) -> Option<&'a str> {
    let OrderedValue::Object(fields) = value else {
        return None;
    };
    fields
        .iter()
        .find(|(name, _)| name == key)
        .and_then(|(_, value)| value.text())
}

fn render_message(
    out: &mut String,
    index: usize,
    messages: &[Message],
    options: EncodeOptions,
    drop_thinking: bool,
) -> Result<(), EncodingError> {
    let msg = &messages[index];
    let last_user = last_user_index(messages);
    if index == 0 && options.thinking_mode == ThinkingMode::Thinking {
        out.push_str(match options.reasoning_effort {
            ReasoningEffort::Low => "",
            ReasoningEffort::High => EFFORT_HIGH,
            ReasoningEffort::Max => EFFORT_MAX,
        });
    }
    match msg.role.as_str() {
        "system" => {
            out.push_str(&content_text(msg));
            append_directives(out, msg);
        }
        "developer" => {
            if content_text(msg).is_empty() {
                return Err(invalid(index, "developer content is empty"));
            }
            out.push_str(USER);
            out.push_str(&content_text(msg));
            append_directives(out, msg);
        }
        "user" => {
            out.push_str(USER);
            if msg.content_blocks.is_empty() {
                out.push_str(&content_text(msg));
            } else {
                for (i, b) in msg.content_blocks.iter().enumerate() {
                    if i > 0 {
                        out.push_str("\n\n");
                    }
                    match b {
                        ContentBlock::Text(s) => out.push_str(s),
                        ContentBlock::ToolResult { content, .. } => {
                            out.push_str("<tool_result>");
                            out.push_str(content);
                            out.push_str("</tool_result>");
                        }
                    }
                }
            }
        }
        "latest_reminder" => {
            out.push_str(LATEST_REMINDER);
            out.push_str(&content_text(msg));
        }
        "assistant" => {
            let prev_has_task = index > 0 && messages[index - 1].task.is_some();
            if options.thinking_mode == ThinkingMode::Thinking
                && !prev_has_task
                && (!drop_thinking || index as isize > last_user)
            {
                out.push_str(msg.reasoning_content.as_deref().unwrap_or(""));
                out.push_str(THINK_END);
            }
            out.push_str(&content_text(msg));
            if !msg.tool_calls.is_empty() {
                render_tool_calls(out, &msg.tool_calls);
            }
            if !msg.wo_eos {
                out.push_str(EOS);
            }
        }
        role => {
            return Err(EncodingError::Role {
                index,
                role: role.into(),
            })
        }
    }

    if index + 1 < messages.len()
        && !matches!(
            messages[index + 1].role.as_str(),
            "assistant" | "latest_reminder"
        )
    {
        return Ok(());
    }
    if let Some(task) = &msg.task {
        let token =
            task_token(task).ok_or_else(|| invalid(index, format!("invalid task {task:?}")))?;
        if task == "action" {
            out.push_str(ASSISTANT);
            out.push_str(if options.thinking_mode == ThinkingMode::Thinking {
                THINK_START
            } else {
                THINK_END
            });
        }
        out.push_str(token);
    } else if matches!(msg.role.as_str(), "user" | "developer") {
        out.push_str(ASSISTANT);
        let open = options.thinking_mode == ThinkingMode::Thinking
            && (!drop_thinking || index as isize >= last_user);
        out.push_str(if open { THINK_START } else { THINK_END });
    }
    Ok(())
}

fn append_directives(out: &mut String, msg: &Message) {
    if !msg.tools.is_empty() {
        out.push_str("\n\n");
        out.push_str(TOOLS_HEAD);
        out.push_str(
            &msg.tools
                .iter()
                .map(ToolDefinition::schema_json)
                .collect::<Vec<_>>()
                .join("\n"),
        );
        out.push('\n');
        out.push_str(TOOLS_FOOT);
    }
    if let Some(v) = &msg.response_format {
        out.push_str("\n\n## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n");
        out.push_str(&v.python_json());
    }
}

fn render_tool_calls(out: &mut String, calls: &[ToolCall]) {
    out.push_str("\n\n<|DSML|tool_calls>\n");
    for call in calls {
        out.push_str("<|DSML|invoke name=\"");
        out.push_str(&call.function.name);
        out.push_str("\">\n");
        let args =
            serde_json::from_str::<OrderedValue>(&call.function.arguments).unwrap_or_else(|_| {
                OrderedValue::Object(vec![(
                    "arguments".into(),
                    OrderedValue::String(call.function.arguments.clone()),
                )])
            });
        if let OrderedValue::Object(fields) = args {
            let empty = fields.is_empty();
            for (k, v) in fields {
                out.push_str("<|DSML|parameter name=\"");
                out.push_str(&k);
                out.push_str("\" string=\"");
                match v {
                    OrderedValue::String(s) => {
                        out.push_str("true\">");
                        out.push_str(&s);
                    }
                    other => {
                        out.push_str("false\">");
                        out.push_str(&other.python_json());
                    }
                }
                out.push_str("</|DSML|parameter>\n");
            }
            if empty {
                out.push('\n');
            }
        }
        out.push_str("</|DSML|invoke>\n");
    }
    out.push_str("</|DSML|tool_calls>");
}

fn task_token(task: &str) -> Option<&'static str> {
    Some(match task {
        "action" => "<|action|>",
        "query" => "<|query|>",
        "authority" => "<|authority|>",
        "domain" => "<|domain|>",
        "title" => "<|title|>",
        "read_url" => "<|read_url|>",
        _ => return None,
    })
}
fn invalid(index: usize, detail: impl Into<String>) -> EncodingError {
    EncodingError::Invalid {
        index,
        detail: detail.into(),
    }
}