Class: Vangrail::Rails::SelfCheck

Inherits:
Vangrail::Rail show all
Defined in:
lib/vangrail/rails/self_check.rb

Overview

Puts a policy in the system message and the text in the user message, the shape the policy-model guides describe. Any instruct model can serve; a classifier cannot, because it answers with its own label tokens whatever it is asked.

This is the rail that a NeMo self check input or self check output flow resolves to, so a config folder written for the Python toolkit runs here unchanged.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider: nil, policy: nil, model: nil, chat: nil, name: 'self_check', sides: [:input], max_tokens: 256, **chat_options) ⇒ SelfCheck

Returns a new instance of SelfCheck.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vangrail/rails/self_check.rb', line 23

def initialize(provider: nil, policy: nil, model: nil, chat: nil,
               name: 'self_check', sides: [:input], max_tokens: 256, **chat_options)
  super(name: name, sides: sides)
  @model = model || provider&.model(:judge)
  @policy = policy || default_policy(sides)
  raise ArgumentError, 'a self-check rail needs a model' if @model.nil? && chat.nil?

  @chat = chat || begin
    raise ArgumentError, 'a self-check rail needs a provider or a chat client' unless provider

    Chat.new(model: @model, base_url: provider.base_url, api_key: provider.api_key,
             max_tokens: max_tokens, **chat_options)
  end
end

Instance Attribute Details

#chatObject (readonly)

Returns the value of attribute chat.



21
22
23
# File 'lib/vangrail/rails/self_check.rb', line 21

def chat
  @chat
end

#modelObject (readonly)

Returns the value of attribute model.



21
22
23
# File 'lib/vangrail/rails/self_check.rb', line 21

def model
  @model
end

#policyObject (readonly)

Returns the value of attribute policy.



21
22
23
# File 'lib/vangrail/rails/self_check.rb', line 21

def policy
  @policy
end

Instance Method Details

#cache_key(text, context) ⇒ Object



38
39
40
41
42
# File 'lib/vangrail/rails/self_check.rb', line 38

def cache_key(text, context)
  return text if context[:side] == :input

  "#{context[:user_input]} #{text}"
end

#call(text, context) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vangrail/rails/self_check.rb', line 44

def call(text, context)
  rendered = Prompt.render(policy, template_context(text, context))
  answer = chat.ask([
                      { 'role' => 'system', 'content' => rendered },
                      { 'role' => 'user', 'content' => text.to_s },
                    ])
  parsed = Parsers.policy(answer.text)
  unless parsed[:decided]
    return Result.new(status: :passed, rail: name, certain: false, model: model,
                      latency_ms: answer.latency_ms, raw: answer.raw,
                      reason: "unparsed judge response: #{parsed[:reason]}")
  end

  return pass(model: model, latency_ms: answer.latency_ms, raw: answer.raw) unless parsed[:violated]

  block(reason: parsed[:reason], categories: parsed[:categories], model: model,
        latency_ms: answer.latency_ms, raw: answer.raw)
end