Class: PromptBuilder::Content::InputImage

Inherits:
Base
  • Object
show all
Defined in:
lib/prompt_builder/content/input_image.rb

Overview

Represents image input content in a message.

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, detail: nil, **extra) ⇒ InputImage

Create a new InputImage content object.

Parameters:

  • url (String, nil) (defaults to: nil)

    the image URL or data URL

  • detail (String, nil) (defaults to: nil)

    the image detail level

  • extra (Hash)

    provider-specific extra keyword arguments



36
37
38
39
40
# File 'lib/prompt_builder/content/input_image.rb', line 36

def initialize(url: nil, detail: nil, **extra)
  @detail = detail&.to_s
  @extra = normalize_extra_kwargs(extra)
  @url = url&.to_s
end

Instance Attribute Details

#detailString? (readonly)

Returns the detail level for the image.

Returns:

  • (String, nil)

    the detail level for the image



12
13
14
# File 'lib/prompt_builder/content/input_image.rb', line 12

def detail
  @detail
end

#extraHash? (readonly)

Returns provider-specific extra data.

Returns:

  • (Hash, nil)

    provider-specific extra data



15
16
17
# File 'lib/prompt_builder/content/input_image.rb', line 15

def extra
  @extra
end

#urlString? (readonly)

Returns the image URL (may be a fully qualified URL or a base64-encoded data URL such as “data:image/png;base64,…” ).

Returns:

  • (String, nil)

    the image URL (may be a fully qualified URL or a base64-encoded data URL such as “data:image/png;base64,…” )



9
10
11
# File 'lib/prompt_builder/content/input_image.rb', line 9

def url
  @url
end

Class Method Details

.from_h(hash) ⇒ InputImage

Deserialize an InputImage from a Hash.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



22
23
24
25
26
27
28
# File 'lib/prompt_builder/content/input_image.rb', line 22

def from_h(hash)
  new(
    url: hash["url"],
    detail: hash["detail"],
    **hash.except("type", "url", "detail").transform_keys(&:to_sym)
  )
end

Instance Method Details

#to_hHash

Serialize to a Hash with string keys. Nil values are omitted.

Returns:

  • (Hash)


45
46
47
48
49
50
51
# File 'lib/prompt_builder/content/input_image.rb', line 45

def to_h
  h = {"type" => "input_image"}
  h["url"] = @url if @url
  h["detail"] = @detail if @detail
  h = PromptBuilder.jsonify(@extra).merge(h) unless @extra.empty?
  h
end