Class: PromptBuilder::Content::InputFile

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

Overview

Represents file 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, filename: nil, **extra) ⇒ InputFile

Create a new InputFile content object.

Parameters:

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

    the file URL or data URL

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

    the filename

  • extra (Hash)

    provider-specific extra keyword arguments



35
36
37
38
39
# File 'lib/prompt_builder/content/input_file.rb', line 35

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

Instance Attribute Details

#extraHash? (readonly)

Returns provider-specific extra data.

Returns:

  • (Hash, nil)

    provider-specific extra data



14
15
16
# File 'lib/prompt_builder/content/input_file.rb', line 14

def extra
  @extra
end

#filenameString? (readonly)

Returns the filename.

Returns:

  • (String, nil)

    the filename



11
12
13
# File 'lib/prompt_builder/content/input_file.rb', line 11

def filename
  @filename
end

#urlString? (readonly)

Returns the file URL (may be a data URL).

Returns:

  • (String, nil)

    the file URL (may be a data URL)



8
9
10
# File 'lib/prompt_builder/content/input_file.rb', line 8

def url
  @url
end

Class Method Details

.from_h(hash) ⇒ InputFile

Deserialize an InputFile from a Hash.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



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

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

Instance Method Details

#to_hHash

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

Returns:

  • (Hash)


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

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