Class: LLM::EventStream::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb

Constant Summary collapse

FIELD_REGEXP =
/[^:]+/
VALUE_REGEXP =
/(?<=: ).+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunk) ⇒ LLM::EventStream::Event

Parameters:

  • chunk (String)


28
29
30
31
32
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 28

def initialize(chunk)
  @field = chunk[FIELD_REGEXP]
  @value = chunk[VALUE_REGEXP]
  @chunk = chunk
end

Instance Attribute Details

#chunkString (readonly)

Returns the full chunk

Returns:

  • (String)


23
24
25
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 23

def chunk
  @chunk
end

#fieldSymbol (readonly)

Returns the field name

Returns:

  • (Symbol)


13
14
15
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 13

def field
  @field
end

#valueString (readonly)

Returns the field value

Returns:

  • (String)


18
19
20
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 18

def value
  @value
end

Instance Method Details

#data?Boolean

Returns true when the event represents a “data” chunk

Returns:

  • (Boolean)


44
45
46
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 44

def data?
  @field == "data"
end

#end?Boolean

Returns true when a chunk represents the end of the stream

Returns:

  • (Boolean)


65
66
67
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 65

def end?
  @value == "[DONE]"
end

#event?Boolean

Returns true when the event represents an “event” chunk

Returns:

  • (Boolean)


51
52
53
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 51

def event?
  @field == "event"
end

#id?Boolean

Returns true when the event represents an “id” chunk

Returns:

  • (Boolean)


37
38
39
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 37

def id?
  @field == "id"
end

#retry?Boolean

Returns true when the event represents a “retry” chunk

Returns:

  • (Boolean)


58
59
60
# File 'lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb', line 58

def retry?
  @field == "retry"
end