Class: Phronomy::Agent::ContextParts::Validators::FinalBudgetValidator
- Inherits:
-
Object
- Object
- Phronomy::Agent::ContextParts::Validators::FinalBudgetValidator
- Defined in:
- lib/phronomy/agent/context_parts/validators/final_budget_validator.rb
Instance Method Summary collapse
-
#initialize(content_loader:) ⇒ FinalBudgetValidator
constructor
A new instance of FinalBudgetValidator.
- #validate!(token_budget:, segments:, extra_values: []) ⇒ Object
Constructor Details
#initialize(content_loader:) ⇒ FinalBudgetValidator
Returns a new instance of FinalBudgetValidator.
8 9 10 |
# File 'lib/phronomy/agent/context_parts/validators/final_budget_validator.rb', line 8 def initialize(content_loader:) @content_loader = content_loader end |
Instance Method Details
#validate!(token_budget:, segments:, extra_values: []) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/phronomy/agent/context_parts/validators/final_budget_validator.rb', line 12 def validate!(token_budget:, segments:, extra_values: []) return 0 unless token_budget estimated = Array(segments).sum do |segment| Phronomy::LlmContextWindow::TokenEstimator.estimate( @content_loader.call(segment.fetch(:content_ref)) ) end estimated += Array(extra_values).sum do |value| bytes = value.is_a?(String) ? value : Phronomy::CanonicalJSON.dump(value) Phronomy::LlmContextWindow::TokenEstimator.estimate(bytes) end limit = token_budget.effective_input_limit if estimated > limit raise Phronomy::ContextBudgetExceededError, "Canonical LLM input (estimated #{estimated} tokens) exceeds " \ "available input budget (#{limit} tokens)" end estimated end |