Class: Vangrail::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail.rb

Overview

Reads the environment into an engine. A class rather than a method so each decision is separable and testable on its own.

Constant Summary collapse

DEFAULT_RAILS =
%i[input context output].freeze
ALL_RAILS =
%i[input context output grounding secrets patterns links multiturn privacy
markup budget semantic perplexity bayes].freeze
INJECTION_PATTERNS =

Deterministic input patterns, kept small on purpose. Each is a phrase whose presence is itself the violation; anything needing judgement belongs in a policy rail, where a false positive is a model's opinion rather than a hard rule.

{
  'instruction_override' => /\bignore\s+(?:all\s+|any\s+)?(?:previous|prior|above|earlier)\s+instructions?\b/i,
  'prompt_disclosure' => /\b(?:reveal|print|repeat|show|output)\s+(?:me\s+)?(?:your|the|its)?\s*
                          (?:system\s+prompt|initial\s+instructions|developer\s+message)\b/xi,
  'role_reset' => /\byou\s+are\s+now\s+(?:a|an|in)\b.{0,40}\b(?:mode|persona|dan|jailbreak)\b/i,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = ENV) ⇒ Builder

Returns a new instance of Builder.



161
162
163
# File 'lib/vangrail.rb', line 161

def initialize(env = ENV)
  @env = env
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



159
160
161
# File 'lib/vangrail.rb', line 159

def env
  @env
end

Class Method Details

.deterministic(side) ⇒ Object

The offline stack that does not need a folder or an endpoint: patterns, concepts, near-copies, the decoding pass, and the language posture. Config#engine(stdlib: true) prepends the same list so a NeMo folder does not certain-pass a question nothing here can read.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/vangrail.rb', line 181

def self.deterministic(side)
  side = side.to_sym
  return [] if side == :output

  core = [
    (Rails::InjectedInstructions.new if side == :context),
    (if side == :input
       Rails::Pattern.new(patterns: INJECTION_PATTERNS, name: 'injection_patterns',
                          sides: [side])
     end),
    Rails::Jailbreak.new(sides: [side]),
    Rails::Paraphrase.new(sides: [side]),
    Rails::Alignment.new(sides: [side]),
    Rails::Similarity.new(sides: [side]),
    Rails::ManyShot.new(sides: [side]),
  ].compact
  extras = [Rails::Obfuscation.new(rails: core, sides: [side])]
  extras << Rails::Hidden.new(rails: core) if side == :context
  extras << Rails::Language.new(sides: [side])
  core + extras
end

Instance Method Details

#cache?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/vangrail.rb', line 224

def cache?
  !off_value?(env['GUARDRAILS_CACHE'])
end

#config_dirObject



228
229
230
# File 'lib/vangrail.rb', line 228

def config_dir
  present(env['GUARDRAILS_CONFIG'])
end

#enabledObject



203
204
205
206
207
208
209
210
# File 'lib/vangrail.rb', line 203

def enabled
  text = env['GUARDRAILS_RAILS'].to_s.strip
  return DEFAULT_RAILS if text.empty?
  return [] if off_value?(text) || text.casecmp('none').zero?
  return ALL_RAILS.dup if text.casecmp('all').zero?

  text.split(/[,\s]+/).map { |s| s.strip.downcase.to_sym } & ALL_RAILS
end

#engineObject



165
166
167
168
169
170
171
# File 'lib/vangrail.rb', line 165

def engine
  return Engine.new(on_error: on_error, cache: cache?) if off?
  return config_engine if config_dir

  Engine.new(input: input_rails, context: context_rails, output: output_rails,
             on_error: on_error, cache: cache?)
end

#off?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/vangrail.rb', line 216

def off?
  off_value?(env['GUARDRAILS'])
end

#on?(rail) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/vangrail.rb', line 212

def on?(rail)
  enabled.include?(rail)
end

#on_errorObject



220
221
222
# File 'lib/vangrail.rb', line 220

def on_error
  env['GUARDRAILS_ON_ERROR'].to_s.strip.casecmp('block').zero? ? :block : :allow
end

#providerObject

The endpoint the model-backed rails call, or nil when none is reachable.



237
238
239
240
241
# File 'lib/vangrail.rb', line 237

def provider
  return @provider if defined?(@provider)

  @provider = Provider.resolve(env)
end

#server_urlObject



232
233
234
# File 'lib/vangrail.rb', line 232

def server_url
  present(env['GUARDRAILS_SERVER'])
end

#session(prior:, **kwargs) ⇒ Object



173
174
175
# File 'lib/vangrail.rb', line 173

def session(prior:, **kwargs)
  Session.new(engine: engine, prior: prior, **kwargs)
end