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

Parameters:

  • raw_response (Hash)

    The raw response from the LLM API

  • parsed_content (Hash, String, nil) (defaults to: nil)

    Optional pre-parsed content

  • error (Agentic::Errors::LlmError, nil) (defaults to: nil)

    Optional error object



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)

Returns The parsed content from the LLM response.

Returns:

  • (Hash, String, nil)

    The parsed content from the LLM response



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

def content
  @content
end

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

Returns The error object, if an error occurred.

Returns:



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

def error
  @error
end

#raw_responseHash (readonly)

Returns The raw response from the LLM API.

Returns:

  • (Hash)

    The raw response from the LLM API



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

def raw_response
  @raw_response
end

#refusalString? (readonly)

Returns The refusal message, if the LLM refused the request.

Returns:

  • (String, nil)

    The refusal message, if the LLM refused the request



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

def refusal
  @refusal
end

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

Returns The refusal error object, if the LLM refused the request.

Returns:



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

def refusal_error
  @refusal_error
end

#statsGenerationStats? (readonly)

Returns Statistics about the generation.

Returns:



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

Parameters:

  • error (Agentic::Errors::LlmError)

    The error that occurred

  • raw_response (Hash, nil) (defaults to: nil)

    The raw response from the LLM API, if available

Returns:



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

Parameters:

  • raw_response (Hash)

    The raw response from the LLM API

  • refusal (String)

    The refusal message

  • refusal_error (Agentic::Errors::LlmRefusalError, nil) (defaults to: nil)

    The refusal error object

Returns:



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

Parameters:

  • raw_response (Hash)

    The raw response from the LLM API

  • content (Hash, String)

    The parsed content

Returns:



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

Returns:

  • (Boolean)

    True 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

Returns:

  • (Symbol, nil)

    The refusal category, or nil if not refused



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

Returns:

  • (Boolean)

    True 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

Returns:

  • (Boolean)

    True if the refusal can be retried with modifications, false otherwise



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

Returns:

  • (Boolean)

    True 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

Returns:

  • (Hash)

    The response as 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