Files
sure/app/models/assistant/token_estimator.rb
Juan José Mata 7b2b1dd367 Rebase PR #784 and fix OpenAI model/chat regressions (#1384)
* Wire conversation history through OpenAI responses API

* Fix RuboCop hash brace spacing in assistant tests

* Pipelock ignores

* Batch fixes

---------

Co-authored-by: sokiee <sokysrm@gmail.com>
2026-04-15 18:45:24 +02:00

20 lines
445 B
Ruby

module Assistant::TokenEstimator
CHARS_PER_TOKEN = 4
SAFETY_FACTOR = 1.25
def self.estimate(value)
chars = char_length(value)
((chars / CHARS_PER_TOKEN.to_f) * SAFETY_FACTOR).ceil
end
def self.char_length(value)
case value
when nil then 0
when String then value.length
when Array then value.sum { |v| char_length(v) }
when Hash then value.to_json.length
else value.to_s.length
end
end
end