Class: Agentic::LlmResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/llm_response.rb

Overview

Value object representing a response from an LLM

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_response, parsed_content: nil, error: nil, refusal_error: nil) ⇒ LlmResponse

Initializes a new LlmResponse



31
32
33
34
35
36
37
38
# File 'lib/agentic/llm_response.rb', line 31

def initialize(raw_response, parsed_content: nil, error: nil, refusal_error: nil)
  @raw_response = raw_response
  @refusal = raw_response&.dig("choices", 0, "message", "refusal")
  @content = parsed_content || parse_content(raw_response)
  @error = error
  @refusal_error = refusal_error
  @stats = raw_response ? GenerationStats.from_response(raw_response) : nil
end

Instance Attribute Details

#contentHash, ... (readonly)



10
11
12
# File 'lib/agentic/llm_response.rb', line 10

def content
  @content
end

#errorAgentic::Errors::LlmError? (readonly)



19
20
21
# File 'lib/agentic/llm_response.rb', line 19

def error
  @error
end

#raw_responseHash (readonly)



16
17
18
# File 'lib/agentic/llm_response.rb', line 16

def raw_response
  @raw_response
end

#refusalString? (readonly)



13
14
15
# File 'lib/agentic/llm_response.rb', line 13

def refusal
  @refusal
end

#refusal_errorAgentic::Errors::LlmRefusalError? (readonly)



22
23
24
# File 'lib/agentic/llm_response.rb', line 22

def refusal_error
  @refusal_error
end

#statsGenerationStats? (readonly)



25
26
27
# File 'lib/agentic/llm_response.rb', line 25

def stats
  @stats
end

Class Method Details

.error(error, raw_response = nil) ⇒ LlmResponse

Creates an error response



65
66
67
# File 'lib/agentic/llm_response.rb', line 65

def self.error(error, raw_response = nil)
  new(raw_response, error: error)
end

.refusal(raw_response, refusal, refusal_error = nil) ⇒ LlmResponse

Creates a refusal response



53
54
55
56
57
58
59
# File 'lib/agentic/llm_response.rb', line 53

def self.refusal(raw_response, refusal, refusal_error = nil)
  new(
    raw_response,
    parsed_content: nil,
    refusal_error: refusal_error
  )
end

.success(raw_response, content) ⇒ LlmResponse

Creates a successful response



44
45
46
# File 'lib/agentic/llm_response.rb', line 44

def self.success(raw_response, content)
  new(raw_response, parsed_content: content)
end

Instance Method Details

#error?Boolean

Checks if an error occurred



95
96
97
# File 'lib/agentic/llm_response.rb', line 95

def error?
  !@error.nil?
end

#raise_if_error!void

This method returns an undefined value.

Raises the error if one occurred

Raises:



102
103
104
# File 'lib/agentic/llm_response.rb', line 102

def raise_if_error!
  raise @error if error?
end

#refusal_categorySymbol?

Gets the refusal category if available



83
84
85
# File 'lib/agentic/llm_response.rb', line 83

def refusal_category
  @refusal_error&.refusal_category
end

#refused?Boolean

Checks if the request was refused



77
78
79
# File 'lib/agentic/llm_response.rb', line 77

def refused?
  !@refusal.nil? || !@refusal_error.nil?
end

#retryable_refusal?Boolean

Checks if the refusal can be retried with modifications



89
90
91
# File 'lib/agentic/llm_response.rb', line 89

def retryable_refusal?
  @refusal_error&.retryable_with_modifications? || false
end

#successful?Boolean

Checks if the response was successful



71
72
73
# File 'lib/agentic/llm_response.rb', line 71

def successful?
  !refused? && !error?
end

#to_hHash

Converts the response to a hash



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/agentic/llm_response.rb', line 108

def to_h
  base = {stats: @stats&.to_h}

  if error?
    base.merge({
      error: {
        message: @error.message,
        type: @error.class.name
      },
      content: nil,
      refusal: nil,
      refusal_category: nil
    })
  elsif refused?
    refusal_info = {
      refusal: @refusal,
      content: nil,
      error: nil
    }

    # Add refusal category if available
    if @refusal_error
      refusal_info[:refusal_category] = @refusal_error.refusal_category
      refusal_info[:retryable] = @refusal_error.retryable_with_modifications?
    end

    base.merge(refusal_info)
  else
    base.merge({
      content: @content,
      refusal: nil,
      error: nil,
      refusal_category: nil
    })
  end
end