Class: OllamaChat::TokenEstimator::Crude
- Inherits:
-
Object
- Object
- OllamaChat::TokenEstimator::Crude
- Defined in:
- lib/ollama_chat/token_estimator/crude.rb
Overview
Provides a "best-effort" estimation of token counts based on the character count or byte size of the input content.
Instance Method Summary collapse
-
#initialize(arg) ⇒ Crude
constructor
Initializes a new crude estimator with the provided source.
-
#perform ⇒ OllamaChat::TokenEstimator::Estimate
Performs the estimation calculation and returns an Estimate object.
Constructor Details
#initialize(arg) ⇒ Crude
Initializes a new crude estimator with the provided source.
8 9 10 11 12 13 14 15 16 |
# File 'lib/ollama_chat/token_estimator/crude.rb', line 8 def initialize(arg) if text = arg.ask_and_send(:to_str) @bytes = text.size elsif bytes = arg.ask_and_send(:to_int) @bytes = bytes else raise ArgumentError, "#{arg.inspect} cannot be used to estimate" end end |
Instance Method Details
#perform ⇒ OllamaChat::TokenEstimator::Estimate
Performs the estimation calculation and returns an Estimate object.
21 22 23 24 |
# File 'lib/ollama_chat/token_estimator/crude.rb', line 21 def perform tokens = (@bytes.to_f / 3.5).ceil OllamaChat::TokenEstimator::Estimate.new(bytes: @bytes, tokens:) end |