Class: LlmOptimizer::HistoryManager
- Inherits:
-
Object
- Object
- LlmOptimizer::HistoryManager
- Defined in:
- lib/llm_optimizer/history_manager.rb
Constant Summary collapse
- SUMMARIZE_COUNT =
10
Instance Method Summary collapse
- #estimate_tokens(messages) ⇒ Object
-
#initialize(llm_caller:, simple_model:, token_budget:) ⇒ HistoryManager
constructor
A new instance of HistoryManager.
- #process(messages) ⇒ Object
Constructor Details
#initialize(llm_caller:, simple_model:, token_budget:) ⇒ HistoryManager
Returns a new instance of HistoryManager.
7 8 9 10 11 |
# File 'lib/llm_optimizer/history_manager.rb', line 7 def initialize(llm_caller:, simple_model:, token_budget:) @llm_caller = llm_caller @simple_model = simple_model @token_budget = token_budget end |
Instance Method Details
#estimate_tokens(messages) ⇒ Object
13 14 15 16 |
# File 'lib/llm_optimizer/history_manager.rb', line 13 def estimate_tokens() total_chars = .sum { |m| (m[:content] || m["content"] || "").length } total_chars / 4 end |
#process(messages) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/llm_optimizer/history_manager.rb', line 18 def process() return if estimate_tokens() <= @token_budget count = [SUMMARIZE_COUNT, .length].min to_summarize = .first(count) remainder = .drop(count) summary = summarize(to_summarize) return if summary.nil? [{ role: "system", content: summary }] + remainder end |