Class: Scrubber::LLMGuard
- Inherits:
-
Object
- Object
- Scrubber::LLMGuard
- Defined in:
- lib/scrubber/llm_guard.rb,
sig/scrubber_rb.rbs
Overview
Redacts prompts before they reach a model API.
Constant Summary collapse
- CONTENT_KEYS =
Message hash keys whose values are prompt text.
%w[content text prompt input].freeze
Instance Attribute Summary collapse
-
#scrubber ⇒ Instance
readonly
Returns the value of attribute scrubber.
Instance Method Summary collapse
-
#call(input) ⇒ Object
(also: #scrub)
Redact
input, preserving its shape. -
#findings(input) ⇒ Array[Match]
What would have been redacted.
-
#initialize(scrubber: nil, replacement: :hash, **options) ⇒ LLMGuard
constructor
A new instance of LLMGuard.
-
#to_proc ⇒ Proc
Usable directly as a block:
messages.map(&guard).
Constructor Details
Instance Attribute Details
#scrubber ⇒ Instance (readonly)
Returns the value of attribute scrubber.
28 29 30 |
# File 'lib/scrubber/llm_guard.rb', line 28 def scrubber @scrubber end |
Instance Method Details
#call(input) ⇒ Object Also known as: scrub
Redact input, preserving its shape.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/scrubber/llm_guard.rb', line 35 def call(input) case input when String then @scrubber.scrub(input) when Array then input.map { |item| call(item) } when Hash then scrub_hash(input) when Symbol, Numeric, NilClass, TrueClass, FalseClass then input else input.respond_to?(:to_str) ? @scrubber.scrub(input.to_str) : input end end |
#findings(input) ⇒ Array[Match]
What would have been redacted. Handy for a "we blocked N leaks today" metric, or for failing a test when a prompt builder regresses.
54 55 56 57 58 59 60 61 |
# File 'lib/scrubber/llm_guard.rb', line 54 def findings(input) case input when String then @scrubber.detect(input) when Array then input.flat_map { |item| findings(item) } when Hash then content_values(input).flat_map { |v| findings(v) } else [] end end |
#to_proc ⇒ Proc
Usable directly as a block: messages.map(&guard).
48 49 50 |
# File 'lib/scrubber/llm_guard.rb', line 48 def to_proc method(:call).to_proc end |