Class: Riffer::Guardrail

Inherits:
Object
  • Object
show all
Extended by:
Helpers::ClassNameConverter
Includes:
Helpers::ClassNameConverter
Defined in:
lib/riffer/guardrail.rb

Overview

Base class for guardrails that process input and output in the agent pipeline.

Subclass this to create custom guardrails:

class MyGuardrail < Riffer::Guardrail
  identifier "my_guardrail"

  def process_input(messages, context:)
    # Return pass(messages), transform(modified_messages), or block(reason)
    pass(messages)
  end

  def process_output(response, messages:, context:)
    # Return pass(response), transform(modified_response), or block(reason)
    pass(response)
  end
end

Direct Known Subclasses

Riffer::Guardrails::MaxLength

Constant Summary

Constants included from Helpers::ClassNameConverter

Helpers::ClassNameConverter::DEFAULT_SEPARATOR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ClassNameConverter

class_name_to_path

Class Method Details

.identifier(value = nil) ⇒ Object

Gets or sets the guardrail identifier.

value - the identifier to set, or nil to get.

: (?String?) -> String



32
33
34
35
# File 'lib/riffer/guardrail.rb', line 32

def identifier(value = nil)
  return @identifier || class_name_to_path(name) if value.nil?
  @identifier = value.to_s
end

Instance Method Details

#identifierObject

Returns the instance’s identifier.

: () -> String



41
42
43
# File 'lib/riffer/guardrail.rb', line 41

def identifier
  self.class.identifier
end

#process_input(messages, context:) ⇒ Object

Processes input messages before they are sent to the LLM.

Override this method in subclasses to implement input processing.

messages - the input messages. context - optional context passed to the agent.

: (Array, context: untyped) -> Riffer::Guardrails::Result



53
54
55
# File 'lib/riffer/guardrail.rb', line 53

def process_input(messages, context:)
  pass(messages)
end

#process_output(response, messages:, context:) ⇒ Object

Processes output response after it is received from the LLM.

Override this method in subclasses to implement output processing.

response - the LLM response. messages - the conversation messages. context - optional context passed to the agent.

: (Riffer::Messages::Assistant, messages: Array, context: untyped) -> Riffer::Guardrails::Result



66
67
68
# File 'lib/riffer/guardrail.rb', line 66

def process_output(response, messages:, context:)
  pass(response)
end