Class: ActiveHarness::Providers::Images::OpenAI

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

Constant Summary collapse

ENDPOINT =
"https://api.openai.com/v1/images/generations"

Constants inherited from Base

Base::HTTP, Base::STREAMING_HTTP

Instance Method Summary collapse

Instance Method Details

#call(model:, prompt:, size: "1024x1024", quality: nil, **_) ⇒ Object

Parameters:

  • model (String)

    “dall-e-2”, “dall-e-3”, “gpt-image-1”

  • prompt (String)

    image description

  • size (String) (defaults to: "1024x1024")

    e.g. “1024x1024”

  • quality (String) (defaults to: nil)

    “standard”/“hd” (dall-e-3), “low”/“medium”/“high”/“auto” (gpt-image-1)

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_harness/providers/images/openai.rb', line 13

def call(model:, prompt:, size: "1024x1024", quality: nil, **_)
  headers = {
    "Content-Type"  => "application/json",
    "Authorization" => "Bearer #{api_key}"
  }

  raw  = post_json(URI(ENDPOINT), headers: headers, body: build_payload(model, prompt, size, quality), timeout: 60)
  data = parse!(raw)
  handle_error!(data)

  b64 = data.dig("data", 0, "b64_json")
  raise Errors::ProviderError, "No image data in response" unless b64

  { content: b64, provider: :openai, model: model, usage: nil }
end