Class: LlmGateway::Adapters::OpenAI::ChatCompletions::InputMapper

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

Direct Known Subclasses

Responses::InputMapper

Class Method Summary collapse

Class Method Details

.map(data) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/llm_gateway/adapters/openai/chat_completions/input_mapper.rb', line 11

def self.map(data)
  {
    messages: map_messages(data[:messages]),
    tools: map_tools(data[:tools]),
    system: map_system(data[:system])
  }
end

.map_messages(messages) ⇒ Object



21
22
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/llm_gateway/adapters/openai/chat_completions/input_mapper.rb', line 21

def self.map_messages(messages)
  return messages unless messages

  message_mapper = BidirectionalMessageMapper.new(LlmGateway::DIRECTION_IN)

  # First map messages like Claude
  mapped_messages = messages.map do |msg|
    msg = msg.merge(role: "user") if msg[:role] == "developer"

    content = if msg[:content].is_a?(Array)
        msg[:content].map do |content|
          message_mapper.map_content(content)
        end
    else
      [ message_mapper.map_content(msg[:content]) ]
    end

    {
      role: msg[:role],
      content: content
    }
  end
  # Then transform to OpenAI format
  mapped_messages.flat_map do |msg|
    # Handle array content with tool calls and tool results
    tool_calls = []
    regular_content = []
    tool_messages = []
    msg[:content].each do |content|
      case content[:type] || content[:role]
      when "tool"
        tool_messages << content
      when "function"
        tool_calls << content
      else
        regular_content << content
      end
    end
    result = []

    # Add the main message with tool calls if any
    if tool_calls.any? || regular_content.any?
      main_msg = msg.dup
      main_msg[:role] = "assistant" if !main_msg[:role]
      main_msg[:tool_calls] = tool_calls if tool_calls.any?
      main_msg[:content] = regular_content.any? ? regular_content : nil
      result << main_msg
    end

    # Add separate tool result messages
    result += tool_messages

    result
  end
end

.map_system(system) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/llm_gateway/adapters/openai/chat_completions/input_mapper.rb', line 92

def self.map_system(system)
  if !system || system.empty?
    []
  else
    system.map do |msg|
      msg[:role] == "system" ? msg.merge(role: "developer") : msg
    end
  end
end

.map_tools(tools) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/llm_gateway/adapters/openai/chat_completions/input_mapper.rb', line 77

def self.map_tools(tools)
  return tools unless tools

  tools.map do |tool|
    {
      type: "function",
      function: {
        name: tool[:name],
        description: tool[:description],
        parameters: tool[:input_schema]
      }
    }
  end
end