Class: LanguageOperator::Agent::Safety::ContentFilter

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/language_operator/agent/safety/content_filter.rb

Overview

Content Filter for pre-prompt and post-response filtering

Supports pattern matching for blocked keywords and topics. Can be extended to integrate with moderation APIs.

Examples:

filter = ContentFilter.new(blocked_patterns: ['password', 'secret'])
filter.check_content!("Here is my password: 12345", direction: :input)

Defined Under Namespace

Classes: ContentBlockedError

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(blocked_patterns: [], blocked_topics: [], case_sensitive: false) ⇒ ContentFilter



22
23
24
25
26
27
28
29
30
31
# File 'lib/language_operator/agent/safety/content_filter.rb', line 22

def initialize(blocked_patterns: [], blocked_topics: [], case_sensitive: false)
  @blocked_patterns = blocked_patterns || []
  @blocked_topics = blocked_topics || []
  @case_sensitive = case_sensitive

  logger.info('Content filter initialized',
              patterns: @blocked_patterns.length,
              topics: @blocked_topics.length,
              case_sensitive: @case_sensitive)
end

Instance Method Details

#blocked?(content) ⇒ Boolean

Check if content would be blocked (without raising)



78
79
80
81
82
83
# File 'lib/language_operator/agent/safety/content_filter.rb', line 78

def blocked?(content)
  check_content!(content)
  false
rescue ContentBlockedError
  true
end

#check_content!(content, direction: :input) ⇒ Object

Check content for blocked patterns

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/language_operator/agent/safety/content_filter.rb', line 38

def check_content!(content, direction: :input)
  return if @blocked_patterns.empty? && @blocked_topics.empty?

  content_to_check = @case_sensitive ? content : content.downcase

  # Check blocked patterns
  @blocked_patterns.each do |pattern|
    pattern_to_match = @case_sensitive ? pattern : pattern.downcase
    next unless content_to_check.include?(pattern_to_match)

    logger.warn('Blocked pattern detected',
                direction: direction,
                pattern: pattern,
                content_preview: content[0..100])
    raise ContentBlockedError,
          "Content blocked: contains forbidden pattern '#{pattern}' in #{direction}"
  end

  # Check blocked topics (using regex patterns)
  @blocked_topics.each do |topic|
    pattern = @case_sensitive ? Regexp.new(topic) : Regexp.new(topic, Regexp::IGNORECASE)
    next unless content_to_check.match?(pattern)

    logger.warn('Blocked topic detected',
                direction: direction,
                topic: topic,
                content_preview: content[0..100])
    raise ContentBlockedError,
          "Content blocked: matches forbidden topic '#{topic}' in #{direction}"
  end

  logger.debug('Content check passed',
               direction: direction,
               content_length: content.length)
end