Class: PromptBuilder::Content::OutputText

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

Overview

Represents text output content in an assistant message.

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, annotations: [], logprobs: [], **extra) ⇒ OutputText

Create a new OutputText content object.

Parameters:

  • text (String)

    the text content

  • annotations (Array<Hash>) (defaults to: [])

    annotations on the text

  • logprobs (Array<Hash>) (defaults to: [])

    token log probabilities on the text

  • extra (Hash)

    provider-specific extra keyword arguments



25
26
27
28
29
30
# File 'lib/prompt_builder/content/output_text.rb', line 25

def initialize(text:, annotations: [], logprobs: [], **extra)
  @text = text&.to_s
  @annotations = PromptBuilder.jsonify(annotations)
  @logprobs = PromptBuilder.jsonify(logprobs)
  @extra = extra.transform_keys(&:to_s)
end

Instance Attribute Details

#annotationsArray<Hash> (readonly)

Returns annotations on the text.

Returns:

  • (Array<Hash>)

    annotations on the text



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

def annotations
  @annotations
end

#extraHash? (readonly)

Returns provider-specific extra data.

Returns:

  • (Hash, nil)

    provider-specific extra data



17
18
19
# File 'lib/prompt_builder/content/output_text.rb', line 17

def extra
  @extra
end

#logprobsArray<Hash> (readonly)

Returns token log probabilities for the text.

Returns:

  • (Array<Hash>)

    token log probabilities for the text



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

def logprobs
  @logprobs
end

#textString (readonly)

Returns the text content.

Returns:

  • (String)

    the text content



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

def text
  @text
end

Class Method Details

.from_h(hash) ⇒ OutputText

Deserialize an OutputText from a Hash.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



37
38
39
40
41
42
43
44
# File 'lib/prompt_builder/content/output_text.rb', line 37

def from_h(hash)
  new(
    text: hash["text"],
    annotations: hash["annotations"] || [],
    logprobs: hash["logprobs"] || [],
    **hash.except("type", "text", "annotations", "logprobs").transform_keys(&:to_sym)
  )
end

Instance Method Details

#to_hHash

Serialize to a Hash with string keys. Empty annotations are omitted.

Returns:

  • (Hash)


50
51
52
53
54
55
56
# File 'lib/prompt_builder/content/output_text.rb', line 50

def to_h
  h = {"type" => "output_text", "text" => @text}
  h["annotations"] = @annotations unless @annotations.empty?
  h["logprobs"] = @logprobs unless @logprobs.empty?
  h = PromptBuilder.jsonify(@extra).merge(h) unless @extra.empty?
  h
end