Class: PromptBuilder::Content::Base

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

Overview

Base class for content objects. Provides polymorphic deserialization via the type field in the hash representation.

Constant Summary collapse

TYPES =

Content type registry for polymorphic dispatch.

{
  "input_file" => "InputFile",
  "input_image" => "InputImage",
  "input_text" => "InputText",
  "input_video" => "InputVideo",
  "output_text" => "OutputText",
  "reasoning_text" => "ReasoningText",
  "refusal" => "RefusalContent",
  "summary_text" => "SummaryText",
  "text" => "Text"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_h(hash) ⇒ Content::Base

Deserialize a content object from a Hash by dispatching on the type field.

Parameters:

  • hash (Hash)

    a Hash with string keys including a “type” field

Returns:

Raises:



27
28
29
30
31
32
33
# File 'lib/prompt_builder/content/base.rb', line 27

def from_h(hash)
  type = hash["type"]
  class_name = TYPES[type]
  raise InvalidItemError, "Unknown content type: #{type.inspect}" unless class_name

  Content.const_get(class_name).from_h(hash)
end

Instance Method Details

#to_hHash

Serialize the content object to a Hash with string keys.

Returns:

  • (Hash)

    the hash representation

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/prompt_builder/content/base.rb', line 39

def to_h
  raise NotImplementedError
end