Module: Legion::LLM::Hooks::ResponseGuard

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/hooks/response_guard.rb

Constant Summary collapse

GUARD_REGISTRY =
{
  rag: RagGuard
}.freeze

Class Method Summary collapse

Class Method Details

.guard_response(response:, context: nil, guards: [:rag]) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/llm/hooks/response_guard.rb', line 15

def guard_response(response:, context: nil, guards: [:rag], **)
  guard_results = {}

  guards.each do |guard_name|
    guard_mod = GUARD_REGISTRY[guard_name.to_sym]
    next unless guard_mod

    guard_results[guard_name] = dispatch_guard(guard_mod, guard_name,
                                               response: response, context: context)
  end

  passed = guard_results.values.all? { |r| r[:faithful] != false }

  { passed: passed, guards: guard_results }
rescue StandardError => e
  handle_exception(e, level: :warn)
  { passed: true, guards: {} }
end