Class: LlmGateway::Adapters::OpenAI::ChatCompletions::OutputMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_gateway/adapters/openai/chat_completions/output_mapper.rb

Class Method Summary collapse

Class Method Details

.map(data) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/llm_gateway/adapters/openai/chat_completions/output_mapper.rb', line 8

def self.map(data)
  {
    id: data[:id],
    model: data[:model],
    usage: data[:usage],
    choices: map_choices(data[:choices])
  }
end

.map_choices(choices) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llm_gateway/adapters/openai/chat_completions/output_mapper.rb', line 19

def self.map_choices(choices)
  return [] unless choices
  message_mapper = BidirectionalMessageMapper.new(LlmGateway::DIRECTION_OUT)

  choices.map do |choice|
    message = choice[:message] || {}
    content_item = message_mapper.map_content(message[:content])
    tool_calls = message[:tool_calls] ? message[:tool_calls].map { |tool_call| message_mapper.map_content(tool_call) } : []

    # Only include content_item if it has actual text content
    content_array = []
    content_array << content_item if LlmGateway::Utils.present?(content_item[:text])
    content_array += tool_calls

    { role: message[:role], content: content_array }
  end
end