Class: OllamaAgent::LLM::ContextBuilder
- Inherits:
-
Object
- Object
- OllamaAgent::LLM::ContextBuilder
- Defined in:
- lib/ollama_agent/llm/context_builder.rb
Overview
Assembles chat messages under strict per-section token budgets (fail closed).
Constant Summary collapse
- DEFAULT_BUDGET =
{ system: 0.10, history: 0.30, focus: 0.50, buffer: 0.10 }.freeze
Instance Method Summary collapse
-
#build(system:, history:, focus:) ⇒ Array<Hash>
OpenAI-style messages (
role,contentas strings). -
#initialize(max_tokens:, token_counter: OllamaAgent::Context::TokenCounter, budget: {}) ⇒ ContextBuilder
constructor
A new instance of ContextBuilder.
Constructor Details
#initialize(max_tokens:, token_counter: OllamaAgent::Context::TokenCounter, budget: {}) ⇒ ContextBuilder
Returns a new instance of ContextBuilder.
9 10 11 12 13 |
# File 'lib/ollama_agent/llm/context_builder.rb', line 9 def initialize(max_tokens:, token_counter: OllamaAgent::Context::TokenCounter, budget: {}) @token_counter = token_counter @budget = merge_budget(budget) @max_tokens = Integer(max_tokens) end |
Instance Method Details
#build(system:, history:, focus:) ⇒ Array<Hash>
Returns OpenAI-style messages (role, content as strings).
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ollama_agent/llm/context_builder.rb', line 16 def build(system:, history:, focus:) assert_section_budget!(:system, system) assert_section_budget!(:history, history) assert_section_budget!(:focus, focus) = [] << { "role" => "system", "content" => system.to_s } unless system.to_s.empty? user_body = join_history_focus(history.to_s, focus.to_s) << { "role" => "user", "content" => user_body } unless user_body.empty? end |