Class: OllamaAgent::Providers::OpenAI

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

Overview

OpenAI provider — talks to the OpenAI Chat Completions API (or any compatible endpoint). Also works with Azure OpenAI, Together AI, Groq, and other OpenAI-compatible APIs by setting :base_url.

Examples:

provider = OllamaAgent::Providers::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
response = provider.chat(messages: [...], model: "gpt-4o")

Constant Summary collapse

API_BASE =
"https://api.openai.com/v1"
DEFAULT_MODEL =
"gpt-4o-mini"
PRICING =

Pricing per 1K tokens (USD) — update as needed

{
  "gpt-4o" => { input: 0.0025, output: 0.010 },
  "gpt-4o-mini" => { input: 0.00015, output: 0.0006 },
  "gpt-4-turbo" => { input: 0.010,   output: 0.030 },
  "gpt-3.5-turbo" => { input: 0.0005, output: 0.0015 }
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#to_s

Constructor Details

#initialize(api_key: nil, base_url: nil, organization: nil, timeout: 60) ⇒ OpenAI

Returns a new instance of OpenAI.



29
30
31
32
33
34
35
# File 'lib/ollama_agent/providers/openai.rb', line 29

def initialize(api_key: nil, base_url: nil, organization: nil, timeout: 60, **)
  super(name: "openai", **)
  @api_key      = api_key      || ENV.fetch("OPENAI_API_KEY", nil)
  @base_url     = base_url     || ENV.fetch("OPENAI_BASE_URL", API_BASE)
  @organization = organization || ENV.fetch("OPENAI_ORG_ID", nil)
  @timeout      = timeout
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ollama_agent/providers/openai.rb', line 57

def available?
  !@api_key.nil?
end

#chat(messages:, model: nil, tools: nil, stream_hooks: nil, temperature: 0.2, **_opts) ⇒ Response

Parameters:

  • messages (Array<Hash>)
  • model (String) (defaults to: nil)
  • tools (Array<Hash>) (defaults to: nil)

    nil = no tools

  • stream_hooks (Hash) (defaults to: nil)

    :on_token lambda (streaming)

  • temperature (Float) (defaults to: 0.2)

Returns:

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ollama_agent/providers/openai.rb', line 43

def chat(messages:, model: nil, tools: nil, stream_hooks: nil, temperature: 0.2, **_opts)
  raise ConfigurationError, "OpenAI API key not set (OPENAI_API_KEY)" unless @api_key

  model ||= DEFAULT_MODEL
  body = build_body(messages: messages, model: model, tools: tools,
                    temperature: temperature, stream: !stream_hooks.nil?)

  if stream_hooks
    stream_chat(body, model, stream_hooks)
  else
    blocking_chat(body, model)
  end
end

#estimate_cost(input_tokens:, output_tokens:, model: DEFAULT_MODEL) ⇒ Object



65
66
67
68
69
# File 'lib/ollama_agent/providers/openai.rb', line 65

def estimate_cost(input_tokens:, output_tokens:, model: DEFAULT_MODEL)
  pricing = PRICING[model] || PRICING[DEFAULT_MODEL]
  (input_tokens / 1000.0 * pricing[:input]) +
    (output_tokens / 1000.0 * pricing[:output])
end

#streaming_supported?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ollama_agent/providers/openai.rb', line 61

def streaming_supported?
  true
end