Module: Braintrust::Scorer::Callable

Defined in:
lib/braintrust/scorer.rb

Overview

Included into classes that include Scorer. Prepends KeywordFilter and ResultNormalizer so #call receives only declared kwargs and always returns Array<Hash>. Also provides a default #name and #call_parameters.

Defined Under Namespace

Modules: ResultNormalizer

Constant Summary collapse

PREPENDED =

Infrastructure modules prepended onto every scorer class. Used both to set up the ancestor chain and to skip past them in #call_parameters so KeywordFilter sees the real call signature.

[Internal::Callable::KeywordFilter, ResultNormalizer].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Parameters:

  • base (Class)

    the class including Callable



87
88
89
# File 'lib/braintrust/scorer.rb', line 87

def self.included(base)
  PREPENDED.each { |mod| base.prepend(mod) }
end

Instance Method Details

#call_parametersArray<Array>

Provides KeywordFilter with the actual call signature of the subclass. Walks past PREPENDED modules in the ancestor chain so that user-defined #call keyword params are correctly introspected. Block overrides this to point directly at @block.parameters.

Returns:

  • (Array<Array>)

    parameter list



104
105
106
107
108
# File 'lib/braintrust/scorer.rb', line 104

def call_parameters
  meth = method(:call)
  meth = meth.super_method while meth.super_method && PREPENDED.include?(meth.owner)
  meth.parameters
end

#nameString

Default name derived from the class name (e.g. FuzzyMatch -> “fuzzy_match”).

Returns:

  • (String)


93
94
95
96
97
# File 'lib/braintrust/scorer.rb', line 93

def name
  klass = self.class.name&.split("::")&.last
  return Scorer::DEFAULT_NAME unless klass
  klass.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end