Class: Rubino::LLM::InlineThinkFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/llm/inline_think_filter.rb

Overview

Streaming filter that splits text into :content and :thinking events by recognising inline <think>…</think> sentinels emitted by MiniMax, DeepSeek-R1, Qwen, and similar reasoning models that don’t expose a dedicated reasoning channel.

Holds back up to TAG_MAX_LEN-1 chars across chunks so a tag split between chunks (e.g. “<thi” + “nk>”) still gets matched. Call #flush at end of stream to drain any tail.

Constant Summary collapse

OPEN_RE =
/<think>/i
CLOSE_RE =
%r{</think>}i
TAG_MAX_LEN =
"</think>".length

Instance Method Summary collapse

Constructor Details

#initializeInlineThinkFilter

Returns a new instance of InlineThinkFilter.



18
19
20
21
# File 'lib/rubino/llm/inline_think_filter.rb', line 18

def initialize
  @inside  = false
  @pending = +""
end

Instance Method Details

#feed(chunk) ⇒ Object



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
# File 'lib/rubino/llm/inline_think_filter.rb', line 23

def feed(chunk)
  @pending << chunk
  loop do
    re, sentinel = @inside ? [CLOSE_RE, :thinking] : [OPEN_RE, :content]
    match = @pending.match(re)

    if match
      idx     = match.begin(0)
      tag_len = match[0].length
      emit    = @pending.slice!(0, idx)
      @pending.slice!(0, tag_len)
      yield sentinel, emit unless emit.empty?
      @inside = !@inside
    else
      # Hold back last (TAG_MAX_LEN-1) chars in case the next chunk
      # completes a tag that began at the tail of @pending.
      safe_len = @pending.length - (TAG_MAX_LEN - 1)
      if safe_len.positive?
        emit = @pending.slice!(0, safe_len)
        yield sentinel, emit unless emit.empty?
      end
      break
    end
  end
end

#flush {|sentinel, @pending| ... } ⇒ Object

Yields:

  • (sentinel, @pending)


49
50
51
52
53
54
55
# File 'lib/rubino/llm/inline_think_filter.rb', line 49

def flush
  return if @pending.empty?

  sentinel = @inside ? :thinking : :content
  yield sentinel, @pending
  @pending = +""
end