Class: LittleGhost::Providers::OpenAICompatible::ChatNormalizer

Inherits:
Normalizer
  • Object
show all
Defined in:
lib/little_ghost/providers/openai_compatible.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from Normalizer

#stream_done

Constructor Details

#initializeChatNormalizer

Returns a new instance of ChatNormalizer.



632
633
634
635
# File 'lib/little_ghost/providers/openai_compatible.rb', line 632

def initialize(...)
  super
  @reasoning_details = []
end

Instance Method Details

#consume(event) ⇒ Object



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/little_ghost/providers/openai_compatible.rb', line 637

def consume(event)
  if event["error"]
    raise StreamError.from(event["error"], prefix: "Provider stream error")
  end

  events = []
  events << start_event(event["id"], event["model"]) unless @message_id
  choice = event.fetch("choices", []).first
  if choice
    delta = choice["delta"] || {}
    events << text_delta(delta["content"]) if delta["content"]
    reasoning = delta["reasoning_content"] || delta["reasoning"]
    events << reasoning_delta(reasoning) if reasoning
    capture_reasoning_details(delta["reasoning_details"]) if delta.key?("reasoning_details")
    delta.fetch("tool_calls", []).each do |call|
      index = call.fetch("index")
      function = call["function"] || {}
      if call["id"] || function["name"]
        started = tool_start(index, call["id"], function["name"])
        events << started if started
      end
      events << tool_delta(index, function["arguments"]) if function["arguments"]
    end
    if choice["finish_reason"]
      @stop_reason = stop_reason(choice["finish_reason"])
      @terminal = true
    end
  end
  events << usage_event(chat_usage(event["usage"])) if event["usage"]
  events
end

#finishObject



669
670
671
672
# File 'lib/little_ghost/providers/openai_compatible.rb', line 669

def finish
  events = @tool_calls.keys.sort.map { |index| tool_stop(index) }
  events.concat(super)
end