Class: Esp::Providers::OpenAI
- Inherits:
-
Object
- Object
- Esp::Providers::OpenAI
- Defined in:
- lib/esp/providers/openai.rb
Overview
The OpenAI Chat Completions implementation of the provider contract, via the official ‘openai` gem. The neutral transcript maps to chat messages: the system prompt is a leading message (not a separate param), each tool result is its own role:’tool’ message (vs Anthropic’s single grouped user message), and the assistant turn is replayed from the stored native message. Tools become OpenAI function definitions; arguments arrive as a JSON string. Client injected for tests; real one built lazily.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_MODEL =
'gpt-4o'.freeze
- ENV_KEY =
'OPENAI_API_KEY'.freeze
Class Method Summary collapse
-
.configured? ⇒ Boolean
“Configured” = the SDK has a key to authenticate with.
Instance Method Summary collapse
- #complete(system:, tools:, messages:) ⇒ Object
-
#initialize(client: nil, model: DEFAULT_MODEL) ⇒ OpenAI
constructor
A new instance of OpenAI.
Constructor Details
#initialize(client: nil, model: DEFAULT_MODEL) ⇒ OpenAI
Returns a new instance of OpenAI.
21 22 23 24 |
# File 'lib/esp/providers/openai.rb', line 21 def initialize(client: nil, model: DEFAULT_MODEL) @client = client @model = model end |
Class Method Details
.configured? ⇒ Boolean
“Configured” = the SDK has a key to authenticate with.
17 18 19 |
# File 'lib/esp/providers/openai.rb', line 17 def self.configured? !ENV.fetch(ENV_KEY, '').to_s.empty? end |
Instance Method Details
#complete(system:, tools:, messages:) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/esp/providers/openai.rb', line 26 def complete(system:, tools:, messages:) response = client.chat.completions.create( model: @model, messages: (system, ), tools: tools.map { |tool| tool_def(tool) } ) = response.choices.first. tool_calls = Array(.tool_calls).map do |call| { id: call.id, name: call.function.name, input: parse_args(call.function.arguments) } end Completion.new(text: .content.to_s, tool_calls: tool_calls, raw: native_assistant(.content, tool_calls)) end |