Class: Phronomy::Filter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/filter/base.rb

Overview

Abstract base class for value filters.

A filter may either transform a value (return the new value) or block it (raise Phronomy::FilterBlockError). The same filter instance can be registered at multiple call sites — input, output, and tool result.

Examples:

PII masking filter

class PiiMaskFilter < Phronomy::Filter::Base
  def call(value, **_context)
    value.to_s
         .gsub(/\b\d{2,4}-\d{2,4}-\d{4}\b/, "[PHONE]")
         .gsub(/\b(?:\d{4}[- ]?){3}\d{4}\b/, "[CARD]")
  end
end

Blocking filter

class NoBadWordFilter < Phronomy::Filter::Base
  def call(value, **_context)
    block!("Forbidden content detected") if value.to_s.include?("badword")
    value
  end
end

Direct Known Subclasses

PromptInjectionFilter

Instance Method Summary collapse

Instance Method Details

#call(value, **_context) ⇒ Object

Process +value+ and return the (possibly transformed) result.

The +context+ keyword arguments vary by call site:

  • Tool result: +{ tool_name: String, args: Hash }+
  • Input / output: +(empty)+

Parameters:

  • value (Object)

    the value being filtered

  • context (Hash)

    optional call-site metadata

Returns:

  • (Object)

    the transformed value (or the original if unchanged)

Raises:



41
42
43
# File 'lib/phronomy/filter/base.rb', line 41

def call(value, **_context)
  raise NotImplementedError, "#{self.class}#call is not implemented"
end