class LittleGhost::Providers::OpenAI
OpenAI connects LittleGhost features to OpenAI models for generation and embeddings. Generation uses the Responses API by default and supports streaming, Tools, and structured results.
provider = LittleGhost::Providers::OpenAI.new( api_key: ENV.fetch("OPENAI_API_KEY"), model: ENV.fetch("OPENAI_MODEL") )
Supply api: :chat_completions only when a model or integration requires the Chat Completions wire API.
Constants
- DEFAULT_BASE_URL
-
The
OpenAIAPI endpoint used whenbase_urlis omitted.
Public Class Methods
# File lib/little_ghost/providers/openai.rb, line 28 def initialize(base_url: DEFAULT_BASE_URL, max_embedding_response_bytes: DEFAULT_MAX_EMBEDDING_RESPONSE_BYTES, **arguments) @max_embedding_response_bytes = Integer(max_embedding_response_bytes) raise ArgumentError, "max_embedding_response_bytes must be positive" unless @max_embedding_response_bytes.positive? super(base_url:, **arguments) end
Uses the official OpenAI API base URL by default.
max_embedding_response_bytes bounds the response retained for one embedding batch. Remaining arguments configure the shared generation transport and retry behavior.
Public Instance Methods
Source
# File lib/little_ghost/providers/openai.rb, line 40 def embed(request) attempts = 0 begin request.cancellation_token.raise_if_cancelled! payload = { model:, input: request.inputs, encoding_format: "float" } dimensions = request.settings[:dimensions] payload[:dimensions] = Integer(dimensions) if dimensions body = +"" @transport.stream( path: "embeddings", headers: {"Authorization" => "Bearer #{@api_key}", "Content-Type" => "application/json"}.merge(@headers), body: JSON.generate(payload), cancellation_token: request.cancellation_token, deadline: request.deadline ) do |chunk| if body.bytesize + chunk.bytesize > @max_embedding_response_bytes raise ProtocolError, "OpenAI embedding response exceeded #{@max_embedding_response_bytes} bytes" end body << chunk end normalize_embedding_response(body, request.inputs.length, dimensions && Integer(dimensions)) rescue HTTPError => error raise unless error.retryable? && attempts < @max_retries attempts += 1 delay = capped_retry_delay(request, retry_delay(attempts)) @on_retry.call(attempts, error, delay) wait_before_retry(request, delay) retry end end
Embeds one or more strings with the configured OpenAI model.
The optional :dimensions request setting selects a supported output size for models that accept it. The response preserves input order and raises ProtocolError when OpenAI returns an incomplete or invalid batch.