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.



705
706
707
708
# File 'lib/little_ghost/providers/openai_compatible.rb', line 705

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

Instance Method Details

#consume(event) ⇒ Object



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/little_ghost/providers/openai_compatible.rb', line 710

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



742
743
744
745
# File 'lib/little_ghost/providers/openai_compatible.rb', line 742

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