Class: OllamaAgent::Providers::OpenAI
- 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.
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
Instance Method Summary collapse
- #available? ⇒ Boolean
- #chat(messages:, model: nil, tools: nil, stream_hooks: nil, temperature: 0.2, **_opts) ⇒ Response
- #estimate_cost(input_tokens:, output_tokens:, model: DEFAULT_MODEL) ⇒ Object
-
#initialize(api_key: nil, base_url: nil, organization: nil, timeout: 60) ⇒ OpenAI
constructor
A new instance of OpenAI.
- #streaming_supported? ⇒ Boolean
Methods inherited from Base
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
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
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: , 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
61 62 63 |
# File 'lib/ollama_agent/providers/openai.rb', line 61 def streaming_supported? true end |