mirror of
https://github.com/we-promise/sure.git
synced 2026-05-08 13:14:58 +00:00
* 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>
20 lines
445 B
Ruby
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
|