Class: Helicone::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/helicone/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Response

Initialize a response wrapper

Parameters:

  • raw (Hash) —

    The raw API response (symbolized keys)



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

Parameters:

  • key (Symbol) —

    Key to access

Returns:

  • (Object, nil)


116
117
118
# File 'lib/helicone/response.rb', line 116

def [](key)
  raw[key]
end

#choices ⇒ Array<Hash>

All choices returned (for n > 1)

Returns:

  • (Array<Hash>)


38
39
40
# File 'lib/helicone/response.rb', line 38

def choices
  raw[:choices] || []
end

#completion_tokens ⇒ Integer?

Number of tokens in the completion

Returns:

  • (Integer, nil)


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

Returns:

  • (String, nil)


17
18
19
# File 'lib/helicone/response.rb', line 17

def content
  message&.dig(:content)
end

#dig(*keys) ⇒ Object?

Dig into nested data in raw response

Parameters:

  • keys (Array<Symbol>) —

    Keys to dig through

Returns:

  • (Object, nil)


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.

Returns:

  • (String, nil)


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

Returns:

  • (String, nil)


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

Returns:

  • (Hash, nil)


24
25
26
# File 'lib/helicone/response.rb', line 24

def message
  raw.dig(:choices, 0, :message)
end

#model ⇒ String?

Model used for the completion

Returns:

  • (String, nil)


80
81
82
# File 'lib/helicone/response.rb', line 80

def model
  raw[:model]
end

#prompt_tokens ⇒ Integer?

Number of tokens in the prompt

Returns:

  • (Integer, nil)


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")

Returns:

  • (String, nil)


31
32
33
# File 'lib/helicone/response.rb', line 31

def role
  message&.dig(:role)
end

#success? ⇒ Boolean

Whether the response completed successfully

Returns:

  • (Boolean)


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

Returns:



108
109
110
# File 'lib/helicone/response.rb', line 108

def to_message
  Message.new(role: role, content: content)
end

#tool_calls ⇒ Array<Hash>?

Tool calls if any were made

Returns:

  • (Array<Hash>, nil)


101
102
103
# File 'lib/helicone/response.rb', line 101

def tool_calls
  message&.dig(:tool_calls)
end

#total_tokens ⇒ Integer?

Total tokens used (prompt + completion)

Returns:

  • (Integer, nil)


73
74
75
# File 'lib/helicone/response.rb', line 73

def total_tokens
  usage&.dig(:total_tokens)
end

#usage ⇒ Hash?

Usage statistics

Returns:

  • (Hash, nil)


52
53
54
# File 'lib/helicone/response.rb', line 52

def usage
  raw[:usage]
end