Class: LlmConductor::Response
- Inherits:
-
Object
- Object
- LlmConductor::Response
- Defined in:
- lib/llm_conductor/response.rb
Overview
Response object that encapsulates the result of LLM generation with metadata like token usage and cost information
Instance Attribute Summary collapse
-
#input_tokens ⇒ Object
readonly
Returns the value of attribute input_tokens.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#output_tokens ⇒ Object
readonly
Returns the value of attribute output_tokens.
Instance Method Summary collapse
-
#estimated_cost ⇒ Object
Calculate estimated cost based on model and token usage.
-
#extract_code_block(language = nil) ⇒ Object
Extract text between code blocks.
-
#initialize(output:, model:, input_tokens: nil, output_tokens: nil, metadata: {}) ⇒ Response
constructor
A new instance of Response.
-
#metadata_with_cost ⇒ Object
Get metadata with cost included if available.
-
#parse_json ⇒ Object
Parse JSON from the output.
-
#success? ⇒ Boolean
Check if the response was successful.
- #total_tokens ⇒ Object
Constructor Details
#initialize(output:, model:, input_tokens: nil, output_tokens: nil, metadata: {}) ⇒ Response
9 10 11 12 13 14 15 |
# File 'lib/llm_conductor/response.rb', line 9 def initialize(output:, model:, input_tokens: nil, output_tokens: nil, metadata: {}) @output = output @model = model @input_tokens = input_tokens @output_tokens = output_tokens = || {} end |
Instance Attribute Details
#input_tokens ⇒ Object (readonly)
Returns the value of attribute input_tokens.
7 8 9 |
# File 'lib/llm_conductor/response.rb', line 7 def input_tokens @input_tokens end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/llm_conductor/response.rb', line 7 def end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/llm_conductor/response.rb', line 7 def model @model end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
7 8 9 |
# File 'lib/llm_conductor/response.rb', line 7 def output @output end |
#output_tokens ⇒ Object (readonly)
Returns the value of attribute output_tokens.
7 8 9 |
# File 'lib/llm_conductor/response.rb', line 7 def output_tokens @output_tokens end |
Instance Method Details
#estimated_cost ⇒ Object
Calculate estimated cost based on model and token usage
22 23 24 25 26 27 28 29 |
# File 'lib/llm_conductor/response.rb', line 22 def estimated_cost return nil unless valid_for_cost_calculation? pricing = model_pricing return nil unless pricing calculate_cost(pricing[:input_rate], pricing[:output_rate]) end |
#extract_code_block(language = nil) ⇒ Object
Extract text between code blocks
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/llm_conductor/response.rb', line 52 def extract_code_block(language = nil) return nil unless @output pattern = if language /```#{Regexp.escape(language)}\s*(.*?)```/m else /```(?:\w*)\s*(.*?)```/m end match = @output.match(pattern) match ? match[1].strip : nil end |
#metadata_with_cost ⇒ Object
Get metadata with cost included if available
37 38 39 40 |
# File 'lib/llm_conductor/response.rb', line 37 def cost = estimated_cost cost ? .merge(cost:) : end |
#parse_json ⇒ Object
Parse JSON from the output
43 44 45 46 47 48 49 |
# File 'lib/llm_conductor/response.rb', line 43 def parse_json return nil unless success? && @output JSON.parse(@output.strip) rescue JSON::ParserError => e raise JSON::ParserError, "Failed to parse JSON response: #{e.message}" end |
#success? ⇒ Boolean
Check if the response was successful
32 33 34 |
# File 'lib/llm_conductor/response.rb', line 32 def success? !@output.nil? && !@output.empty? && [:error].nil? end |
#total_tokens ⇒ Object
17 18 19 |
# File 'lib/llm_conductor/response.rb', line 17 def total_tokens (@input_tokens || 0) + (@output_tokens || 0) end |