Class: ConsoleAgent::Providers::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/console_agent/providers/openai.rb

Constant Summary collapse

API_URL =
'https://api.openai.com'.freeze

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from ConsoleAgent::Providers::Base

Instance Method Details

#chat(messages, system_prompt: nil) ⇒ Object



6
7
8
# File 'lib/console_agent/providers/openai.rb', line 6

def chat(messages, system_prompt: nil)
  call_api(messages, system_prompt: system_prompt)
end

#chat_with_tools(messages, tools:, system_prompt: nil) ⇒ Object



10
11
12
# File 'lib/console_agent/providers/openai.rb', line 10

def chat_with_tools(messages, tools:, system_prompt: nil)
  call_api(messages, system_prompt: system_prompt, tools: tools)
end

#format_assistant_message(result) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/console_agent/providers/openai.rb', line 14

def format_assistant_message(result)
  msg = { role: 'assistant' }
  msg[:content] = result.text if result.text && !result.text.empty?
  if result.tool_calls && !result.tool_calls.empty?
    msg[:tool_calls] = result.tool_calls.map do |tc|
      {
        'id' => tc[:id],
        'type' => 'function',
        'function' => {
          'name' => tc[:name],
          'arguments' => JSON.generate(tc[:arguments] || {})
        }
      }
    end
  end
  msg
end

#format_tool_result(tool_call_id, result_string) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/console_agent/providers/openai.rb', line 32

def format_tool_result(tool_call_id, result_string)
  {
    role: 'tool',
    tool_call_id: tool_call_id,
    content: result_string.to_s
  }
end