Class: PromptBuilder::Content::OutputText
- Defined in:
- lib/prompt_builder/content/output_text.rb
Overview
Represents text output content in an assistant message.
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#annotations ⇒ Array<Hash>
readonly
Annotations on the text.
-
#extra ⇒ Hash?
readonly
Provider-specific extra data.
-
#logprobs ⇒ Array<Hash>
readonly
Token log probabilities for the text.
-
#text ⇒ String
readonly
The text content.
Class Method Summary collapse
-
.from_h(hash) ⇒ OutputText
Deserialize an OutputText from a Hash.
Instance Method Summary collapse
-
#initialize(text:, annotations: [], logprobs: [], **extra) ⇒ OutputText
constructor
Create a new OutputText content object.
-
#to_h ⇒ Hash
Serialize to a Hash with string keys.
Constructor Details
#initialize(text:, annotations: [], logprobs: [], **extra) ⇒ OutputText
Create a new OutputText content object.
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
#annotations ⇒ Array<Hash> (readonly)
Returns annotations on the text.
11 12 13 |
# File 'lib/prompt_builder/content/output_text.rb', line 11 def annotations @annotations end |
#extra ⇒ Hash? (readonly)
Returns provider-specific extra data.
17 18 19 |
# File 'lib/prompt_builder/content/output_text.rb', line 17 def extra @extra end |
#logprobs ⇒ Array<Hash> (readonly)
Returns token log probabilities for the text.
14 15 16 |
# File 'lib/prompt_builder/content/output_text.rb', line 14 def logprobs @logprobs end |
#text ⇒ String (readonly)
Returns 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.
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_h ⇒ Hash
Serialize to a Hash with string keys. Empty annotations are omitted.
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 |