Class: Helicone::Response
- Inherits:
-
Object
- Object
- Helicone::Response
- Defined in:
- lib/helicone/response.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Delegate hash-like access to raw response.
-
#choices ⇒ Array<Hash>
All choices returned (for n > 1).
-
#completion_tokens ⇒ Integer?
Number of tokens in the completion.
-
#content ⇒ String?
The assistant's text response content.
-
#dig(*keys) ⇒ Object?
Dig into nested data in raw response.
-
#finish_reason ⇒ String?
The finish reason: "stop", "length", "tool_calls", etc.
-
#id ⇒ String?
Unique ID for this completion.
-
#initialize(raw) ⇒ Response
constructor
Initialize a response wrapper.
-
#message ⇒ Hash?
The full message object from the first choice.
-
#model ⇒ String?
Model used for the completion.
-
#prompt_tokens ⇒ Integer?
Number of tokens in the prompt.
-
#role ⇒ String?
The role of the response (usually "assistant").
-
#success? ⇒ Boolean
Whether the response completed successfully.
-
#to_message ⇒ Helicone::Message
Convert response content back to a Message for conversation history.
-
#tool_calls ⇒ Array<Hash>?
Tool calls if any were made.
-
#total_tokens ⇒ Integer?
Total tokens used (prompt + completion).
-
#usage ⇒ Hash?
Usage statistics.
Constructor Details
#initialize(raw) ⇒ Response
Initialize a response wrapper
10 11 12 |
# File 'lib/helicone/response.rb', line 10 def initialize(raw) @raw = raw end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
5 6 7 |
# File 'lib/helicone/response.rb', line 5 def raw @raw end |
Instance Method Details
#[](key) ⇒ Object?
Delegate hash-like access to raw response
116 117 118 |
# File 'lib/helicone/response.rb', line 116 def [](key) raw[key] end |
#choices ⇒ Array<Hash>
All choices returned (for n > 1)
38 39 40 |
# File 'lib/helicone/response.rb', line 38 def choices raw[:choices] || [] end |
#completion_tokens ⇒ Integer?
Number of tokens in the completion
66 67 68 |
# File 'lib/helicone/response.rb', line 66 def completion_tokens usage&.dig(:completion_tokens) end |
#content ⇒ String?
The assistant's text response content
17 18 19 |
# File 'lib/helicone/response.rb', line 17 def content &.dig(:content) end |
#dig(*keys) ⇒ Object?
Dig into nested data in raw response
124 125 126 |
# File 'lib/helicone/response.rb', line 124 def dig(*keys) raw.dig(*keys) end |
#finish_reason ⇒ String?
The finish reason: "stop", "length", "tool_calls", etc.
45 46 47 |
# File 'lib/helicone/response.rb', line 45 def finish_reason raw.dig(:choices, 0, :finish_reason) end |
#id ⇒ String?
Unique ID for this completion
87 88 89 |
# File 'lib/helicone/response.rb', line 87 def id raw[:id] end |
#message ⇒ Hash?
The full message object from the first choice
24 25 26 |
# File 'lib/helicone/response.rb', line 24 def raw.dig(:choices, 0, :message) end |
#model ⇒ String?
Model used for the completion
80 81 82 |
# File 'lib/helicone/response.rb', line 80 def model raw[:model] end |
#prompt_tokens ⇒ Integer?
Number of tokens in the prompt
59 60 61 |
# File 'lib/helicone/response.rb', line 59 def prompt_tokens usage&.dig(:prompt_tokens) end |
#role ⇒ String?
The role of the response (usually "assistant")
31 32 33 |
# File 'lib/helicone/response.rb', line 31 def role &.dig(:role) end |
#success? ⇒ Boolean
Whether the response completed successfully
94 95 96 |
# File 'lib/helicone/response.rb', line 94 def success? (content && !content.empty?) || finish_reason == "stop" end |
#to_message ⇒ Helicone::Message
Convert response content back to a Message for conversation history
108 109 110 |
# File 'lib/helicone/response.rb', line 108 def Message.new(role: role, content: content) end |
#tool_calls ⇒ Array<Hash>?
Tool calls if any were made
101 102 103 |
# File 'lib/helicone/response.rb', line 101 def tool_calls &.dig(:tool_calls) end |
#total_tokens ⇒ Integer?
Total tokens used (prompt + completion)
73 74 75 |
# File 'lib/helicone/response.rb', line 73 def total_tokens usage&.dig(:total_tokens) end |
#usage ⇒ Hash?
Usage statistics
52 53 54 |
# File 'lib/helicone/response.rb', line 52 def usage raw[:usage] end |