Class: Vangrail::Rails::Pattern
- Inherits:
-
Vangrail::Rail
- Object
- Vangrail::Rail
- Vangrail::Rails::Pattern
- Defined in:
- lib/vangrail/rails/pattern.rb
Overview
Blocks text matching any of a list of patterns.
A guardrail stack that is only a language model has no floor: when the endpoint is slow, cold, or down, every check degrades at once. A pattern rail decides in microseconds, offline, and identically every time, so the cases you can state exactly stay covered no matter what the network does.
Pattern.new(patterns: { 'instruction_override' => /ignore (all )?previous instructions/i })
Constant Summary
Constants inherited from Vangrail::Rail
Vangrail::Rail::DEFAULT_SIDES, Vangrail::Rail::SIDES
Instance Attribute Summary collapse
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Attributes inherited from Vangrail::Rail
Instance Method Summary collapse
- #cache_key(text, _context) ⇒ Object
- #call(text, _context) ⇒ Object
-
#initialize(patterns:, name: 'pattern', sides: Rail::SIDES, reason: nil) ⇒ Pattern
constructor
A new instance of Pattern.
- #offline? ⇒ Boolean
Methods inherited from Vangrail::Rail
#applies_to?, #placeholder?, #to_s
Constructor Details
#initialize(patterns:, name: 'pattern', sides: Rail::SIDES, reason: nil) ⇒ Pattern
Returns a new instance of Pattern.
18 19 20 21 22 |
# File 'lib/vangrail/rails/pattern.rb', line 18 def initialize(patterns:, name: 'pattern', sides: Rail::SIDES, reason: nil) super(name: name, sides: sides) @patterns = normalize(patterns) @reason = reason end |
Instance Attribute Details
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
16 17 18 |
# File 'lib/vangrail/rails/pattern.rb', line 16 def patterns @patterns end |
Instance Method Details
#cache_key(text, _context) ⇒ Object
28 29 30 |
# File 'lib/vangrail/rails/pattern.rb', line 28 def cache_key(text, _context) text end |
#call(text, _context) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/vangrail/rails/pattern.rb', line 32 def call(text, _context) hit = patterns.find { |_label, pattern| pattern.match?(text.to_s) } return pass unless hit label = hit.first block(categories: [label], reason: @reason || "matched #{label}") end |
#offline? ⇒ Boolean
24 25 26 |
# File 'lib/vangrail/rails/pattern.rb', line 24 def offline? true end |