Class: Agentic::LlmResponse
- Inherits:
-
Object
- Object
- Agentic::LlmResponse
- Defined in:
- lib/agentic/llm_response.rb
Overview
Value object representing a response from an LLM
Instance Attribute Summary collapse
-
#content ⇒ Hash, ...
readonly
The parsed content from the LLM response.
-
#error ⇒ Agentic::Errors::LlmError?
readonly
The error object, if an error occurred.
-
#raw_response ⇒ Hash
readonly
The raw response from the LLM API.
-
#refusal ⇒ String?
readonly
The refusal message, if the LLM refused the request.
-
#refusal_error ⇒ Agentic::Errors::LlmRefusalError?
readonly
The refusal error object, if the LLM refused the request.
-
#stats ⇒ GenerationStats?
readonly
Statistics about the generation.
Class Method Summary collapse
-
.error(error, raw_response = nil) ⇒ LlmResponse
Creates an error response.
-
.refusal(raw_response, refusal, refusal_error = nil) ⇒ LlmResponse
Creates a refusal response.
-
.success(raw_response, content) ⇒ LlmResponse
Creates a successful response.
Instance Method Summary collapse
-
#error? ⇒ Boolean
Checks if an error occurred.
-
#initialize(raw_response, parsed_content: nil, error: nil, refusal_error: nil) ⇒ LlmResponse
constructor
Initializes a new LlmResponse.
-
#raise_if_error! ⇒ void
Raises the error if one occurred.
-
#refusal_category ⇒ Symbol?
Gets the refusal category if available.
-
#refused? ⇒ Boolean
Checks if the request was refused.
-
#retryable_refusal? ⇒ Boolean
Checks if the refusal can be retried with modifications.
-
#successful? ⇒ Boolean
Checks if the response was successful.
-
#to_h ⇒ Hash
Converts the response to a hash.
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
#content ⇒ Hash, ... (readonly)
Returns The parsed content from the LLM response.
10 11 12 |
# File 'lib/agentic/llm_response.rb', line 10 def content @content end |
#error ⇒ Agentic::Errors::LlmError? (readonly)
Returns The error object, if an error occurred.
19 20 21 |
# File 'lib/agentic/llm_response.rb', line 19 def error @error end |
#raw_response ⇒ Hash (readonly)
Returns The raw response from the LLM API.
16 17 18 |
# File 'lib/agentic/llm_response.rb', line 16 def raw_response @raw_response end |
#refusal ⇒ String? (readonly)
Returns 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_error ⇒ Agentic::Errors::LlmRefusalError? (readonly)
Returns The refusal error object, if the LLM refused the request.
22 23 24 |
# File 'lib/agentic/llm_response.rb', line 22 def refusal_error @refusal_error end |
#stats ⇒ GenerationStats? (readonly)
Returns Statistics about the generation.
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
102 103 104 |
# File 'lib/agentic/llm_response.rb', line 102 def raise_if_error! raise @error if error? end |
#refusal_category ⇒ Symbol?
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_h ⇒ Hash
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., 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 |