Class: PromptBuilder::Response
- Inherits:
-
Object
- Object
- PromptBuilder::Response
- Defined in:
- lib/prompt_builder/response.rb
Overview
Represents a parsed API response from the Open Responses API. All fields are optional and will be nil if not present in the response.
Instance Attribute Summary collapse
-
#background ⇒ Boolean?
readonly
Whether this is a background response.
-
#completed_at ⇒ Integer?
readonly
Completion timestamp.
-
#created_at ⇒ Integer?
readonly
Creation timestamp.
-
#error ⇒ Hash?
readonly
Error information.
-
#extra ⇒ Object
readonly
Returns the value of attribute extra.
-
#frequency_penalty ⇒ Float?
readonly
The frequency penalty.
-
#id ⇒ String?
readonly
The response identifier.
-
#incomplete_details ⇒ Hash?
readonly
Details about why the response is incomplete.
-
#instructions ⇒ String?
readonly
The system instructions.
-
#max_output_tokens ⇒ Integer?
readonly
The maximum output tokens.
-
#max_tool_calls ⇒ Integer?
readonly
The maximum number of tool calls.
-
#metadata ⇒ Hash?
readonly
Arbitrary metadata.
-
#model ⇒ String?
readonly
The model identifier.
-
#object ⇒ String?
readonly
The object type.
-
#output ⇒ Array<Items::Base>
readonly
The output items.
-
#parallel_tool_calls ⇒ Boolean?
readonly
Whether parallel tool calls are enabled.
-
#presence_penalty ⇒ Float?
readonly
The presence penalty.
-
#previous_response_id ⇒ String?
readonly
The previous response identifier.
-
#prompt_cache_key ⇒ String?
readonly
The prompt cache key.
-
#prompt_cache_retention ⇒ String?
readonly
The prompt cache retention policy.
-
#reasoning ⇒ Hash?
readonly
The reasoning configuration.
-
#safety_identifier ⇒ String?
readonly
The safety identifier.
-
#service_tier ⇒ String?
readonly
The service tier.
-
#status ⇒ String?
readonly
The response status.
-
#store ⇒ Boolean?
readonly
Whether to store the response.
-
#temperature ⇒ Float?
readonly
The temperature.
-
#text_config ⇒ Hash?
readonly
The text configuration.
-
#tool_choice ⇒ String, ...
readonly
The tool choice configuration.
-
#tools ⇒ Array<Hash>?
readonly
The tool definitions.
-
#top_logprobs ⇒ Integer?
readonly
The number of top log probabilities to return.
-
#top_p ⇒ Float?
readonly
The top_p sampling parameter.
-
#truncation ⇒ String?
readonly
The truncation strategy.
-
#usage ⇒ Usage?
readonly
Token usage statistics.
Class Method Summary collapse
-
.from_h(hash) ⇒ Response
Deserialize a Response from a Hash.
-
.parse(hash, serializer_class) ⇒ Response
Parse a response hash using the given serializer and return a Response.
Instance Method Summary collapse
-
#completed? ⇒ Boolean
Check if the response completed successfully.
-
#failed? ⇒ Boolean
Check if the response failed.
-
#has_tool_calls? ⇒ Boolean
Check if the response contains any tool calls.
-
#incomplete? ⇒ Boolean
Check if the response is incomplete.
-
#initialize(**attributes) ⇒ Response
constructor
Create a new Response.
-
#text ⇒ String?
Extract the text from the first output message.
-
#to_h ⇒ Hash
Serialize to a Hash with string keys.
-
#tool_calls ⇒ Array<Items::FunctionCall>
Get all function call items from the output.
Constructor Details
#initialize(**attributes) ⇒ Response
Create a new Response.
136 137 138 139 140 141 142 |
# File 'lib/prompt_builder/response.rb', line 136 def initialize(**attributes) FIELDS.each { |f| instance_variable_set(:"@#{f}", coerce_field(f, attributes[f])) } @text_config = PromptBuilder.jsonify(attributes[:text_config]) @output = attributes[:output] || [] @usage = attributes[:usage] @extra = PromptBuilder.jsonify(attributes[:extra]) end |
Instance Attribute Details
#background ⇒ Boolean? (readonly)
Returns whether this is a background response.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#completed_at ⇒ Integer? (readonly)
Returns completion timestamp.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#created_at ⇒ Integer? (readonly)
Returns creation timestamp.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#error ⇒ Hash? (readonly)
Returns error information.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#extra ⇒ Object (readonly)
Returns the value of attribute extra.
126 127 128 |
# File 'lib/prompt_builder/response.rb', line 126 def extra @extra end |
#frequency_penalty ⇒ Float? (readonly)
Returns the frequency penalty.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#id ⇒ String? (readonly)
Returns the response identifier.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#incomplete_details ⇒ Hash? (readonly)
Returns details about why the response is incomplete.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#instructions ⇒ String? (readonly)
Returns the system instructions.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#max_output_tokens ⇒ Integer? (readonly)
Returns the maximum output tokens.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#max_tool_calls ⇒ Integer? (readonly)
Returns the maximum number of tool calls.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#metadata ⇒ Hash? (readonly)
Returns arbitrary metadata.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#model ⇒ String? (readonly)
Returns the model identifier.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#object ⇒ String? (readonly)
Returns the object type.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#output ⇒ Array<Items::Base> (readonly)
Returns the output items.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#parallel_tool_calls ⇒ Boolean? (readonly)
Returns whether parallel tool calls are enabled.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#presence_penalty ⇒ Float? (readonly)
Returns the presence penalty.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#previous_response_id ⇒ String? (readonly)
Returns the previous response identifier.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#prompt_cache_key ⇒ String? (readonly)
Returns the prompt cache key.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#prompt_cache_retention ⇒ String? (readonly)
Returns the prompt cache retention policy.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#reasoning ⇒ Hash? (readonly)
Returns the reasoning configuration.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#safety_identifier ⇒ String? (readonly)
Returns the safety identifier.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#service_tier ⇒ String? (readonly)
Returns the service tier.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#status ⇒ String? (readonly)
Returns the response status.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#store ⇒ Boolean? (readonly)
Returns whether to store the response.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#temperature ⇒ Float? (readonly)
Returns the temperature.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#text_config ⇒ Hash? (readonly)
Returns the text configuration.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#tool_choice ⇒ String, ... (readonly)
Returns the tool choice configuration.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#tools ⇒ Array<Hash>? (readonly)
Returns the tool definitions.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#top_logprobs ⇒ Integer? (readonly)
Returns the number of top log probabilities to return.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#top_p ⇒ Float? (readonly)
Returns the top_p sampling parameter.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#truncation ⇒ String? (readonly)
Returns the truncation strategy.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
#usage ⇒ Usage? (readonly)
Returns token usage statistics.
125 |
# File 'lib/prompt_builder/response.rb', line 125 FIELDS.each { |f| attr_reader f } |
Class Method Details
.from_h(hash) ⇒ Response
Deserialize a Response from a Hash.
161 162 163 164 165 166 167 |
# File 'lib/prompt_builder/response.rb', line 161 def from_h(hash) output = (hash["output"] || []).map { |item| Items::Base.from_h(item) } usage = hash["usage"] ? Usage.from_h(hash["usage"]) : nil attrs = FIELDS.each_with_object({}) { |f, acc| acc[f] = hash[f.to_s] } new(**attrs, text_config: hash["text"], output: output, usage: usage, extra: hash["extra"]) end |
.parse(hash, serializer_class) ⇒ Response
Parse a response hash using the given serializer and return a Response.
153 154 155 |
# File 'lib/prompt_builder/response.rb', line 153 def parse(hash, serializer_class) Serializers.resolve(serializer_class).parse_response(hash) end |
Instance Method Details
#completed? ⇒ Boolean
Check if the response completed successfully.
173 174 175 |
# File 'lib/prompt_builder/response.rb', line 173 def completed? @status == "completed" end |
#failed? ⇒ Boolean
Check if the response failed.
180 181 182 |
# File 'lib/prompt_builder/response.rb', line 180 def failed? @status == "failed" end |
#has_tool_calls? ⇒ Boolean
Check if the response contains any tool calls.
194 195 196 |
# File 'lib/prompt_builder/response.rb', line 194 def has_tool_calls? @output.any? { |item| item.is_a?(Items::FunctionCall) } end |
#incomplete? ⇒ Boolean
Check if the response is incomplete.
187 188 189 |
# File 'lib/prompt_builder/response.rb', line 187 def incomplete? @status == "incomplete" end |
#text ⇒ String?
Extract the text from the first output message. Returns the concatenated text of all OutputText content blocks in the first message.
209 210 211 212 213 214 215 216 217 |
# File 'lib/prompt_builder/response.rb', line 209 def text @output.each do |item| next unless item.is_a?(Items::Message) texts = item.content.select { |c| c.is_a?(Content::OutputText) } return texts.map(&:text).join unless texts.empty? end nil end |
#to_h ⇒ Hash
Serialize to a Hash with string keys. Nil values are omitted.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/prompt_builder/response.rb', line 222 def to_h h = {} FIELDS.each do |f| val = instance_variable_get(:"@#{f}") if BOOLEAN_FIELDS.include?(f) h[f.to_s] = val unless val.nil? elsif val h[f.to_s] = val end end h["text"] = @text_config if @text_config h["output"] = @output.map(&:to_h) unless @output.empty? h["usage"] = @usage.to_h if @usage h["extra"] = @extra if @extra && !@extra.empty? h end |
#tool_calls ⇒ Array<Items::FunctionCall>
Get all function call items from the output.
201 202 203 |
# File 'lib/prompt_builder/response.rb', line 201 def tool_calls @output.select { |item| item.is_a?(Items::FunctionCall) } end |