Module: Aura::Nodes

Defined in:
lib/aura/transformer.rb

Overview

Pure helper functions for assembling semantic node hashes. Kept in a module (rather than Transformer instance methods) because Parslet::Transform evaluates rule blocks in their own context where instance methods aren’t in scope – constants like ‘Aura::Nodes` always are.

Class Method Summary collapse

Class Method Details

.clean(body) ⇒ Object

Keep only real node hashes, discarding whitespace slices that Parslet may leave in a repeated body.



19
20
21
# File 'lib/aura/transformer.rb', line 19

def clean(body)
  list(body).select { |x| x.is_a?(Hash) }
end

.dataset(name, source, path, options) ⇒ Object



66
67
68
69
# File 'lib/aura/transformer.rb', line 66

def dataset(name, source, path, options)
  { type: :dataset, name: name.to_s, source: source.to_s, path: path.to_s,
    options: settings(options) }
end

.list(value) ⇒ Object



13
14
15
# File 'lib/aura/transformer.rb', line 13

def list(value)
  value.is_a?(Array) ? value : [value]
end

.llm(name, provider, model_id, body) ⇒ Object



38
39
40
41
42
43
# File 'lib/aura/transformer.rb', line 38

def llm(name, provider, model_id, body)
  cfg = clean(body).each_with_object({}) { |h, acc| acc.merge!(h) }
  { type: :model, kind: :llm, name: name.to_s, provider: provider.to_s.to_sym,
    model_id: model_id.to_s, system: cfg[:system],
    temperature: cfg[:temperature], max_tokens: cfg[:max_tokens] }
end

.model(name, body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/aura/transformer.rb', line 23

def model(name, body)
  layers = clean(body)
  if layers.any? { |l| %i[greeting input_text].include?(l[:type]) }
    greeting = layers.find { |l| l[:type] == :greeting }
    { type: :model, kind: :text, name: name.to_s, layers: layers,
      greeting: (greeting && greeting[:text]) || "Hello from Aura!" }
  else
    { type: :model, kind: :torch, name: name.to_s, layers: layers, torch_model: true }
  end
end

.route(path, method, body) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aura/transformer.rb', line 50

def route(path, method, body)
  lines = clean(body)
  out   = lines.find { |l| l.key?(:route_model) }
  auth  = lines.find { |l| l.key?(:auth) }
  { type: :route, path: path.to_s, method: method.to_s,
    model:       out && out[:route_model],
    input_var:   (out && out[:route_input]) || "input",
    format:      out && out[:route_format],
    postprocess: out && out[:route_as],
    auth:        auth && auth[:auth] }
end

.settings(body) ⇒ Object



62
63
64
# File 'lib/aura/transformer.rb', line 62

def settings(body)
  clean(body).each_with_object({}) { |kv, h| h.merge!(kv) }
end

.train(model, dataset, body) ⇒ Object



45
46
47
48
# File 'lib/aura/transformer.rb', line 45

def train(model, dataset, body)
  config = clean(body).each_with_object({}) { |opt, h| h.merge!(opt) }
  { type: :train, model: model.to_s, dataset: dataset.to_s, config: config }
end

.transfer(name, base, body) ⇒ Object



34
35
36
# File 'lib/aura/transformer.rb', line 34

def transfer(name, base, body)
  { type: :model, kind: :transfer, name: name.to_s, base_model: base, layers: clean(body) }
end