Class: Vangrail::Client::Completion
- Inherits:
-
Object
- Object
- Vangrail::Client::Completion
- Defined in:
- lib/vangrail/client/completion.rb
Overview
A guardrailed chat completion, read out of either server response shape.
The OpenAI-compatible shape puts the answer in choices.message.content
and rail bookkeeping under a top-level guardrails object. The older shape
answers with a bare content message (or a list of them) and puts
bookkeeping at the top level. Both appear in the wild depending on the
server version, so this reads whichever is present.
Constant Summary collapse
- INPUT_RAIL_VAR =
Server-side names for the variables holding the rail that stopped a turn.
'triggered_input_rail'- OUTPUT_RAIL_VAR =
'triggered_output_rail'
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
-
#activated_rails ⇒ Object
Rails that ran, from options.log.activated_rails.
- #allowed? ⇒ Boolean
-
#blocked? ⇒ Boolean
True when a rail is known to have stopped the turn.
- #config_id ⇒ Object
- #content ⇒ Object
- #guardrails ⇒ Object
-
#initialize(raw) ⇒ Completion
constructor
A new instance of Completion.
- #llm_calls ⇒ Object
- #log ⇒ Object
- #model ⇒ Object
- #output_data ⇒ Object
- #stopped_rails ⇒ Object
-
#total_tokens ⇒ Object
Total tokens the rails plus the answer spent, when the server reports it.
-
#triggered_input_rail ⇒ Object
The rail that stopped this turn, or nil.
- #triggered_output_rail ⇒ Object
- #triggered_rail ⇒ Object
Constructor Details
#initialize(raw) ⇒ Completion
Returns a new instance of Completion.
19 20 21 |
# File 'lib/vangrail/client/completion.rb', line 19 def initialize(raw) @raw = raw.is_a?(Hash) ? raw : {} end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
17 18 19 |
# File 'lib/vangrail/client/completion.rb', line 17 def raw @raw end |
Instance Method Details
#activated_rails ⇒ Object
Rails that ran, from options.log.activated_rails. Empty unless logging was requested; an empty list is not evidence that no rail ran.
59 60 61 62 |
# File 'lib/vangrail/client/completion.rb', line 59 def activated_rails entries = log['activated_rails'] entries.is_a?(Array) ? entries : [] end |
#allowed? ⇒ Boolean
77 78 79 |
# File 'lib/vangrail/client/completion.rb', line 77 def allowed? !blocked? end |
#blocked? ⇒ Boolean
True when a rail is known to have stopped the turn. Absent explicit signals this stays false, so a refusal the model wrote itself is never reported as a rail decision.
71 72 73 74 75 |
# File 'lib/vangrail/client/completion.rb', line 71 def blocked? return true unless triggered_rail.nil? !stopped_rails.empty? end |
#config_id ⇒ Object
39 40 41 |
# File 'lib/vangrail/client/completion.rb', line 39 def config_id guardrails['config_id'] end |
#content ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/vangrail/client/completion.rb', line 23 def content choice = choices.first if choice.is_a?(Hash) msg = choice['message'] || choice['delta'] || {} return msg['content'].to_s if msg.is_a?(Hash) && msg.key?('content') end return raw['content'].to_s if raw.key?('content') = raw['messages'] if .is_a?(Array) last = .reverse.find { |m| m.is_a?(Hash) && m['role'].to_s == 'assistant' } return last['content'].to_s if last end '' end |
#guardrails ⇒ Object
85 86 87 88 |
# File 'lib/vangrail/client/completion.rb', line 85 def guardrails g = raw['guardrails'] g.is_a?(Hash) ? g : {} end |
#llm_calls ⇒ Object
100 101 102 103 |
# File 'lib/vangrail/client/completion.rb', line 100 def llm_calls calls = log['llm_calls'] calls.is_a?(Array) ? calls : [] end |
#log ⇒ Object
95 96 97 98 |
# File 'lib/vangrail/client/completion.rb', line 95 def log l = guardrails['log'] || raw['log'] l.is_a?(Hash) ? l : {} end |
#model ⇒ Object
81 82 83 |
# File 'lib/vangrail/client/completion.rb', line 81 def model raw['model'] end |
#output_data ⇒ Object
90 91 92 93 |
# File 'lib/vangrail/client/completion.rb', line 90 def output_data d = guardrails['output_data'] || raw['output_data'] d.is_a?(Hash) ? d : {} end |
#stopped_rails ⇒ Object
64 65 66 |
# File 'lib/vangrail/client/completion.rb', line 64 def stopped_rails activated_rails.select { |r| r.is_a?(Hash) && r['stop'] == true } end |
#total_tokens ⇒ Object
Total tokens the rails plus the answer spent, when the server reports it.
106 107 108 109 110 111 112 |
# File 'lib/vangrail/client/completion.rb', line 106 def total_tokens usage = raw['usage'] return usage['total_tokens'] if usage.is_a?(Hash) && usage['total_tokens'] sums = llm_calls.filter_map { |c| c['total_tokens'] if c.is_a?(Hash) } sums.empty? ? nil : sums.sum end |
#triggered_input_rail ⇒ Object
The rail that stopped this turn, or nil. Reported only when the request asked for the output variables that carry it.
45 46 47 |
# File 'lib/vangrail/client/completion.rb', line 45 def triggered_input_rail output_data[INPUT_RAIL_VAR] end |
#triggered_output_rail ⇒ Object
49 50 51 |
# File 'lib/vangrail/client/completion.rb', line 49 def triggered_output_rail output_data[OUTPUT_RAIL_VAR] end |
#triggered_rail ⇒ Object
53 54 55 |
# File 'lib/vangrail/client/completion.rb', line 53 def triggered_rail triggered_input_rail || triggered_output_rail end |