Class: Vangrail::OptionalReader

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

Overview

Stable adapter around an optional local process, endpoint, or model runtime.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, model_id:, feature_schema:, provider: nil) ⇒ OptionalReader

Returns a new instance of OptionalReader.



118
119
120
121
122
123
# File 'lib/vangrail/score.rb', line 118

def initialize(id:, model_id:, feature_schema:, provider: nil)
  @id = id.to_s.freeze
  @model_id = model_id.to_s.freeze
  @feature_schema = Array(feature_schema).map(&:to_s).freeze
  @provider = provider
end

Instance Attribute Details

#feature_schemaObject (readonly)

Returns the value of attribute feature_schema.



116
117
118
# File 'lib/vangrail/score.rb', line 116

def feature_schema
  @feature_schema
end

#idObject (readonly)

Returns the value of attribute id.



116
117
118
# File 'lib/vangrail/score.rb', line 116

def id
  @id
end

#model_idObject (readonly)

Returns the value of attribute model_id.



116
117
118
# File 'lib/vangrail/score.rb', line 116

def model_id
  @model_id
end

#providerObject (readonly)

Returns the value of attribute provider.



116
117
118
# File 'lib/vangrail/score.rb', line 116

def provider
  @provider
end

Instance Method Details

#score(text, side:, **context) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/vangrail/score.rb', line 125

def score(text, side:, **context)
  return missing(side) unless provider

  raw = if provider.respond_to?(:score)
          provider.score(text, side: side, **context)
        else
          provider.call(text, side: side, **context)
        end
  result = raw.is_a?(ScoreResult) ? raw : result_from(raw, side)
  identity_error(result, side) || result
rescue StandardError => e
  ScoreResult.abstained(
    reader_id: id,
    model_id: model_id,
    feature_schema: feature_schema,
    side: side,
    reason: "#{e.class}: #{e.message}",
  )
end