Module: RubyLLM::Agents::CallbacksDSL
- Included in:
- Base
- Defined in:
- lib/ruby_llm/agents/core/base/callbacks.rb
Overview
DSL and execution support for before_call/after_call hooks
Provides callbacks that run before and after the LLM call, allowing custom preprocessing (redaction, moderation, validation) and postprocessing (logging, transformation) logic.
Instance Method Summary collapse
-
#after {|context, response| ... } ⇒ void
Simplified alias for after_call (block-only).
-
#after_call(method_name = nil) {|context, response| ... } ⇒ void
Add a callback to run after the LLM call.
-
#before {|context| ... } ⇒ void
Simplified alias for before_call (block-only).
-
#before_call(method_name = nil) {|context| ... } ⇒ void
Add a callback to run before the LLM call.
-
#callbacks ⇒ Hash
Get all registered callbacks.
Instance Method Details
#after {|context, response| ... } ⇒ void
This method returns an undefined value.
Simplified alias for after_call (block-only)
This is the preferred method in the simplified DSL.
107 108 109 |
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 107 def after(&block) after_call(&block) end |
#after_call(method_name = nil) {|context, response| ... } ⇒ void
This method returns an undefined value.
Add a callback to run after the LLM call
Callbacks receive the pipeline context and the response. Return value is ignored.
73 74 75 76 |
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 73 def after_call(method_name = nil, &block) @callbacks ||= {before: [], after: []} @callbacks[:after] << (block || method_name) end |
#before {|context| ... } ⇒ void
This method returns an undefined value.
Simplified alias for before_call (block-only)
This is the preferred method in the simplified DSL.
90 91 92 |
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 90 def before(&block) before_call(&block) end |
#before_call(method_name = nil) {|context| ... } ⇒ void
This method returns an undefined value.
Add a callback to run before the LLM call
Callbacks receive the pipeline context and can:
-
Mutate the context (params, prompts, etc.)
-
Raise an exception to block execution
-
Return value is ignored
51 52 53 54 |
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 51 def before_call(method_name = nil, &block) @callbacks ||= {before: [], after: []} @callbacks[:before] << (block || method_name) end |
#callbacks ⇒ Hash
Get all registered callbacks
114 115 116 |
# File 'lib/ruby_llm/agents/core/base/callbacks.rb', line 114 def callbacks @callbacks ||= {before: [], after: []} end |