deepseek-recipe 0.1.0

API request conversion and streamed output parsing for DeepSeek models.
Documentation
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Incrementally recognize reasoning, tool-call markup, and stop sequences.

const JSON_BEGIN_LABELS: [&str; 2] = ["```json\n", "```\n"];

/// Position within the reasoning section when parsing begins.
#[derive(Debug, Clone, Copy)]
pub enum ReasoningStage {
    /// Begin reasoning while discarding leading newlines.
    Start,
    /// Continue reasoning while preserving leading newlines.
    Reasoning,
    /// Continue answer content while preserving leading newlines.
    Content,
}

impl ReasoningStage {
    pub fn start_from_reasoning(&self) -> bool {
        matches!(self, Self::Start | Self::Reasoning)
    }
}

const RAW_JSON_BEGIN_LABELS: [&str; 2] = ["{", "["];
const JSON_END_LABEL: &str = "\n```";
const DSML_BEGIN_LABEL: &str = "<|DSML|";
const DSML_END_LABEL: &str = "</|DSML|";
const DSML_TOOL_CALLS_END_LABELS: [&str; 2] = ["</|DSML|tool_calls>", "</|DSML| calls>"];
pub const REASONING_END_LABEL: &str = "</think>";

/// Settings for parsing inference output.
#[derive(Debug, Clone)]
pub struct ParsingOptions {
    /// Parse model-specific tool-call markup.
    pub parse_tool_calls: bool,
    /// Begin parsing inside the tool-call block specified by the prompt.
    /// Takes precedence over `reasoning_initial_stage`.
    pub tool_call_initial_stage: bool,
    /// Recognize JSON fences or a raw `{` or `[` prefix and replace surrounding
    /// text with spaces. This does not validate JSON syntax.
    pub parse_json_output: bool,
    /// Initial reasoning or answer stage. `None` starts answer content with
    /// leading-newline removal.
    pub reasoning_initial_stage: Option<ReasoningStage>,
    /// Match stop sequences in ordinary answer text and detected JSON output.
    /// Reasoning, tool-call markup, and surrounding JSON-mode text are excluded.
    /// The state machine discards empty sequences during construction.
    pub stop_sequences: Vec<String>,
}

impl Default for ParsingOptions {
    fn default() -> Self {
        Self {
            parse_tool_calls: true,
            tool_call_initial_stage: false,
            parse_json_output: false,
            reasoning_initial_stage: None,
            stop_sequences: Vec::new(),
        }
    }
}

/// An incremental parser whose output segments refer to input byte lengths.
pub struct StateMachine {
    options: ParsingOptions,
    state: State,
}

impl StateMachine {
    pub fn new(mut options: ParsingOptions) -> Self {
        options
            .stop_sequences
            .retain(|sequence| !sequence.is_empty());
        let stage = if options.tool_call_initial_stage {
            Stage::ToolCalls
        } else if let Some(reasoning_initial_stage) = &options.reasoning_initial_stage {
            match reasoning_initial_stage {
                ReasoningStage::Start => Stage::Reasoning { is_leading: true },
                ReasoningStage::Reasoning => Stage::Reasoning { is_leading: false },
                ReasoningStage::Content => Stage::Common { is_leading: false },
            }
        } else {
            Stage::Common { is_leading: true }
        };
        let state = State::new(stage, &options);
        Self { options, state }
    }

    /// Parse the next source chunk, retaining incomplete markers for later input.
    pub fn feed(&mut self, content: &str) -> Vec<OutputActionSegment> {
        self.state.feed(content, &self.options)
    }

    /// Process buffered input when the source ends.
    pub fn finish(self) -> Vec<OutputActionSegment> {
        self.state.finish().1
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InvalidTokenKind {
    ExtraEndOfThinking,
    ContentAfterFinished,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OutputAction {
    Raw,
    ToSpace,
    SkipInvalid { kind: InvalidTokenKind },
    Skip,
    StopSequence,
    ToolCallBegin,
    ToolName,
    ToolNameEnd,
    LabelToolCallArguments { label: &'static str, id: u32 },
    RawToolCallArguments { string: bool },
    ToolCallArgumentsEnd { output: Option<&'static str> },
    Reasoning,
    Label(&'static str),
}

#[derive(Debug, Clone)]
pub struct OutputActionSegment {
    pub action: OutputAction,
    pub len: usize,
}

impl OutputActionSegment {
    pub fn new(action: OutputAction, len: usize) -> Self {
        Self { action, len }
    }
}

#[derive(Debug, Clone, Copy)]
enum Stage {
    Common { is_leading: bool },
    Json,
    MatchedJson,
    ToolCalls,
    ToolName,
    ToolCallArguments { is_leading: bool },
    ToolCallParamName,
    ToolCallParamType,
    ToolCallParamValue { string: bool },
    Reasoning { is_leading: bool },
    Finished,
}

#[derive(Debug)]
struct MatchBranch {
    pub state: MatchState,
    pub next_stage: Stage,
    pub action_on_matched: ActionOnMatched,
}

#[derive(Debug)]
struct MatchBranches(Vec<MatchBranch>);

impl MatchBranches {
    fn new() -> Self {
        Self(Vec::new())
    }
    fn new_branch(
        &mut self,
        pattern: MatchPattern,
        next_stage: Stage,
        action_on_matched: ActionOnMatched,
    ) {
        self.0.push(MatchBranch {
            state: pattern.new_state(),
            next_stage,
            action_on_matched,
        });
    }
    fn new_string_branch<T: ToString + ?Sized>(
        &mut self,
        label: &T,
        next_stage: Stage,
        action_on_matched: ActionOnMatched,
    ) {
        self.new_branch(
            MatchPattern::String(label.to_string()),
            next_stage,
            action_on_matched,
        );
    }
    fn new_strings_branch<T: ToString>(
        &mut self,
        labels: &[T],
        next_stage: Stage,
        action_on_matched: ActionOnMatched,
    ) {
        labels
            .iter()
            .for_each(|label| self.new_string_branch(label, next_stage, action_on_matched));
    }
    fn add_tool_call_begin_branches(&mut self) {
        self.new_branch(
            MatchPattern::NewlinesAndString(DSML_BEGIN_LABEL.to_string()),
            Stage::ToolCalls,
            ActionOnMatched::Skip,
        );
    }
}

#[derive(Debug)]
struct State {
    stage: Stage,
    match_branches: MatchBranches,
    action_on_unmatched: ActionOnUnmatched,
    stashed_size: usize,
}

type ActionOnMatched = OutputAction;
type ActionOnUnmatched = OutputAction;

impl State {
    fn new(stage: Stage, options: &ParsingOptions) -> Self {
        let mut match_branches = MatchBranches::new();
        let mut add_stop_sequence_branches = || {
            match_branches.new_strings_branch(
                &options.stop_sequences,
                Stage::Finished,
                ActionOnMatched::StopSequence,
            );
        };
        let action_on_unmatched = match stage {
            Stage::Common { is_leading } => {
                if options.parse_json_output {
                    match_branches.new_strings_branch(
                        &JSON_BEGIN_LABELS,
                        Stage::Json,
                        ActionOnMatched::Skip,
                    );
                    for raw_json_label in RAW_JSON_BEGIN_LABELS {
                        match_branches.new_string_branch(
                            raw_json_label,
                            Stage::Json,
                            ActionOnMatched::Label(raw_json_label),
                        );
                    }
                } else {
                    add_stop_sequence_branches();
                }
                if options.parse_tool_calls {
                    match_branches.add_tool_call_begin_branches();
                }
                if is_leading {
                    match_branches.new_branch(
                        MatchPattern::LeadingNewline,
                        Stage::Common { is_leading: true },
                        ActionOnMatched::Skip,
                    );
                }
                match_branches.new_string_branch(
                    REASONING_END_LABEL,
                    Stage::Common { is_leading: false },
                    ActionOnMatched::SkipInvalid {
                        kind: InvalidTokenKind::ExtraEndOfThinking,
                    },
                );
                if options.parse_json_output {
                    ActionOnUnmatched::ToSpace
                } else {
                    ActionOnUnmatched::Raw
                }
            }
            Stage::Json => {
                add_stop_sequence_branches();
                match_branches.new_string_branch(
                    JSON_END_LABEL,
                    Stage::MatchedJson,
                    ActionOnMatched::Skip,
                );
                ActionOnUnmatched::Raw
            }
            Stage::MatchedJson => {
                if options.parse_tool_calls {
                    match_branches.add_tool_call_begin_branches();
                }
                ActionOnUnmatched::ToSpace
            }
            Stage::ToolCalls => {
                match_branches.new_string_branch(
                    "invoke name=\"",
                    Stage::ToolName,
                    ActionOnMatched::ToolCallBegin,
                );
                match_branches.new_strings_branch(
                    &DSML_TOOL_CALLS_END_LABELS,
                    Stage::Finished,
                    ActionOnMatched::Skip,
                );
                ActionOnUnmatched::Skip
            }
            Stage::ToolName => {
                match_branches.new_string_branch(
                    "\"",
                    Stage::ToolCallArguments { is_leading: true },
                    ActionOnMatched::ToolNameEnd,
                );
                ActionOnUnmatched::ToolName
            }
            Stage::ToolCallArguments { is_leading } => {
                match_branches.new_string_branch(
                    "parameter name=",
                    Stage::ToolCallParamName,
                    if is_leading {
                        ActionOnMatched::LabelToolCallArguments { label: "{", id: 0 }
                    } else {
                        ActionOnMatched::LabelToolCallArguments { label: ", ", id: 1 }
                    },
                );
                match_branches.new_string_branch(
                    DSML_END_LABEL,
                    Stage::ToolCalls,
                    if is_leading {
                        ActionOnMatched::ToolCallArgumentsEnd { output: Some("{}") }
                    } else {
                        ActionOnMatched::ToolCallArgumentsEnd { output: Some("}") }
                    },
                );
                ActionOnUnmatched::Skip
            }
            Stage::ToolCallParamName => {
                match_branches.new_string_branch(
                    " ",
                    Stage::ToolCallParamType,
                    ActionOnMatched::LabelToolCallArguments { label: ": ", id: 2 },
                );
                ActionOnUnmatched::RawToolCallArguments { string: false }
            }
            Stage::ToolCallParamType => {
                match_branches.new_string_branch(
                    "true\">",
                    Stage::ToolCallParamValue { string: true },
                    ActionOnMatched::LabelToolCallArguments { label: "\"", id: 3 },
                );
                match_branches.new_string_branch(
                    "false\">",
                    Stage::ToolCallParamValue { string: false },
                    ActionOnMatched::Skip,
                );
                ActionOnUnmatched::Skip
            }
            Stage::ToolCallParamValue { string } => {
                match_branches.new_string_branch(
                    DSML_END_LABEL,
                    Stage::ToolCallArguments { is_leading: false },
                    if string {
                        ActionOnMatched::LabelToolCallArguments { label: "\"", id: 4 }
                    } else {
                        ActionOnMatched::Skip
                    },
                );
                ActionOnUnmatched::RawToolCallArguments { string }
            }
            Stage::Reasoning { is_leading } => {
                if options.parse_tool_calls {
                    match_branches.add_tool_call_begin_branches();
                }
                if is_leading {
                    match_branches.new_branch(
                        MatchPattern::LeadingNewline,
                        Stage::Reasoning { is_leading },
                        ActionOnMatched::Skip,
                    );
                }
                match_branches.new_branch(
                    MatchPattern::NewlinesAndString(REASONING_END_LABEL.to_string()),
                    Stage::Common { is_leading: true },
                    ActionOnMatched::Skip,
                );
                ActionOnUnmatched::Reasoning
            }
            Stage::Finished => ActionOnUnmatched::SkipInvalid {
                kind: InvalidTokenKind::ContentAfterFinished,
            },
        };
        Self {
            stage,
            match_branches,
            action_on_unmatched,
            stashed_size: 0,
        }
    }

    fn feed(&mut self, content: &str, options: &ParsingOptions) -> Vec<OutputActionSegment> {
        let mut output_actions = Vec::new();
        for byte in content.bytes() {
            self.stashed_size += 1;
            let mut matched_branch = None;
            for branch in self.match_branches.0.iter_mut() {
                if branch.state.feed(byte) {
                    matched_branch = Some((
                        branch.state.matching_len(),
                        branch.next_stage,
                        branch.action_on_matched,
                    ));
                    break;
                }
            }
            if let Some((matching_len, next_stage, action_on_matched)) = matched_branch {
                if matching_len < self.stashed_size {
                    output_actions.push(OutputActionSegment::new(
                        self.action_on_unmatched,
                        self.stashed_size - matching_len,
                    ));
                }
                if matching_len > 0 {
                    output_actions.push(OutputActionSegment::new(action_on_matched, matching_len));
                }
                *self = State::new(next_stage, options);
            }
        }
        let matching_len = self
            .match_branches
            .0
            .iter()
            .map(|branch| branch.state.matching_len())
            .max()
            .unwrap_or(0);
        if matching_len < self.stashed_size {
            output_actions.push(OutputActionSegment::new(
                self.action_on_unmatched,
                self.stashed_size - matching_len,
            ));
            self.stashed_size = matching_len;
        }
        output_actions
    }

    fn finish(self) -> (Stage, Vec<OutputActionSegment>) {
        let mut output_actions = Vec::new();
        if self.stashed_size > 0 {
            output_actions.push(OutputActionSegment::new(
                self.action_on_unmatched,
                self.stashed_size,
            ));
        }

        (self.stage, output_actions)
    }
}

enum MatchPattern {
    String(String),
    NewlinesAndString(String),
    LeadingNewline,
}

impl MatchPattern {
    fn new_state(self) -> MatchState {
        match self {
            Self::String(s) => {
                let kmp_table = kmp_table(s.as_bytes());
                MatchState::String {
                    s,
                    kmp_table,
                    len: 0,
                }
            }
            Self::NewlinesAndString(s) => {
                let kmp_table = kmp_table(s.as_bytes());
                MatchState::NewlinesAndString {
                    s,
                    kmp_table,
                    len: 0,
                    newline_count: 0,
                }
            }
            Self::LeadingNewline => MatchState::LeadingNewline {
                leading: true,
                matched: false,
            },
        }
    }
}

type KMPTable = Vec<usize>;

fn kmp_table(pattern: &[u8]) -> KMPTable {
    let mut table = vec![0; pattern.len()];
    let mut len = 0;
    let mut i = 1;
    while i < pattern.len() {
        if pattern[i] == pattern[len] {
            len += 1;
            table[i] = len;
            i += 1;
        } else if len != 0 {
            len = table[len - 1];
        } else {
            table[i] = 0;
            i += 1;
        }
    }
    table
}

#[derive(Debug)]
enum MatchState {
    String {
        s: String,
        kmp_table: KMPTable,
        len: usize,
    },
    NewlinesAndString {
        s: String,
        kmp_table: KMPTable,
        len: usize,
        newline_count: usize,
    },
    LeadingNewline {
        leading: bool,
        matched: bool,
    },
}

fn kmp_next(s: &str, kmp_table: &[usize], mut len: usize, byte: u8) -> usize {
    while len > 0 && s.as_bytes().get(len) != Some(&byte) {
        len = kmp_table[len - 1];
    }
    if s.as_bytes().get(len) == Some(&byte) {
        len += 1;
    }
    len
}

impl MatchState {
    fn feed(&mut self, byte: u8) -> bool {
        match self {
            MatchState::String { s, kmp_table, len } => {
                *len = kmp_next(s, kmp_table, *len, byte);
                *len == s.len()
            }
            MatchState::NewlinesAndString {
                s,
                kmp_table,
                len,
                newline_count,
            } => {
                if *len == 0 && byte == b'\n' {
                    *newline_count += 1;
                } else {
                    let next = kmp_next(s, kmp_table, *len, byte);
                    if next <= *len {
                        *newline_count = 0
                    }
                    *len = next;
                }
                *len == s.len()
            }
            MatchState::LeadingNewline { leading, matched } => {
                if *leading {
                    *matched = byte == b'\n';
                }
                *leading = false;
                *matched
            }
        }
    }
    fn matching_len(&self) -> usize {
        match self {
            MatchState::String { len, .. } => *len,
            MatchState::NewlinesAndString {
                len, newline_count, ..
            } => *len + *newline_count,
            MatchState::LeadingNewline { matched, .. } => {
                if *matched {
                    1
                } else {
                    0
                }
            }
        }
    }
}