reasoning_parser/parsers/
deepseek_r1.rs

1// DeepSeek-R1 specific reasoning parser.
2// This parser starts with in_reasoning=true, assuming all text is reasoning
3// until an end token is encountered.
4
5use crate::{
6    parsers::BaseReasoningParser,
7    traits::{ParseError, ParserConfig, ParserResult, ReasoningParser},
8};
9
10/// DeepSeek-R1 reasoning parser.
11///
12/// This parser assumes reasoning from the start of text (in_reasoning=true)
13/// and uses <think> and </think> tokens.
14pub struct DeepSeekR1Parser {
15    base: BaseReasoningParser,
16}
17
18impl DeepSeekR1Parser {
19    /// Create a new DeepSeek-R1 parser.
20    pub fn new() -> Self {
21        let config = ParserConfig {
22            think_start_token: "<think>".to_string(),
23            think_end_token: "</think>".to_string(),
24            stream_reasoning: true,
25            max_buffer_size: 65536,
26            initial_in_reasoning: true, // Always starts with reasoning
27        };
28
29        Self {
30            base: BaseReasoningParser::new(config).with_model_type("deepseek_r1".to_string()),
31        }
32    }
33}
34
35impl Default for DeepSeekR1Parser {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl ReasoningParser for DeepSeekR1Parser {
42    fn detect_and_parse_reasoning(&mut self, text: &str) -> Result<ParserResult, ParseError> {
43        self.base.detect_and_parse_reasoning(text)
44    }
45
46    fn parse_reasoning_streaming_incremental(
47        &mut self,
48        text: &str,
49    ) -> Result<ParserResult, ParseError> {
50        self.base.parse_reasoning_streaming_incremental(text)
51    }
52
53    fn reset(&mut self) {
54        self.base.reset()
55    }
56
57    fn model_type(&self) -> &str {
58        self.base.model_type()
59    }
60
61    fn is_in_reasoning(&self) -> bool {
62        self.base.is_in_reasoning()
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_deepseek_r1_initial_state() {
72        let mut parser = DeepSeekR1Parser::new();
73
74        // Should treat text as reasoning even without start token
75        let result = parser
76            .detect_and_parse_reasoning("This is reasoning content")
77            .unwrap();
78        assert_eq!(result.normal_text, "");
79        assert_eq!(result.reasoning_text, "This is reasoning content");
80    }
81
82    #[test]
83    fn test_deepseek_r1_with_end_token() {
84        let mut parser = DeepSeekR1Parser::new();
85
86        // Should extract reasoning until end token
87        let result = parser
88            .detect_and_parse_reasoning("reasoning content</think>normal content")
89            .unwrap();
90        assert_eq!(result.normal_text, "normal content");
91        assert_eq!(result.reasoning_text, "reasoning content");
92    }
93
94    #[test]
95    fn test_deepseek_r1_streaming() {
96        let mut parser = DeepSeekR1Parser::new();
97
98        // First chunk - all reasoning
99        let result1 = parser
100            .parse_reasoning_streaming_incremental("thinking about")
101            .unwrap();
102        assert_eq!(result1.reasoning_text, "thinking about");
103        assert_eq!(result1.normal_text, "");
104
105        // Second chunk - ends reasoning
106        let result2 = parser
107            .parse_reasoning_streaming_incremental(" the problem</think>answer")
108            .unwrap();
109        assert_eq!(result2.reasoning_text, "the problem"); // Text is trimmed
110        assert_eq!(result2.normal_text, "answer");
111    }
112
113    #[test]
114    fn test_model_type() {
115        let parser = DeepSeekR1Parser::new();
116        assert_eq!(parser.model_type(), "deepseek_r1");
117    }
118}