Class: ActiveAI::Router

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

Constant Summary collapse

INSTRUCTION =
'For a given Match request, choose where to send it via the "Route" field. If nothing matches, the "Route" field should be None.'
UNMATCHED =
{ 'Match' => 'Create a NASA space program', 'Route' => 'None' }

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



5
6
7
8
# File 'lib/activeai/router.rb', line 5

def initialize
  @routings = []
  @llm = ActiveAI::NeuralNetwork::GPT3.new(ActiveAI.config[:gpt3_token], model: 'code-cushman-001', temperature: 0)
end

Instance Method Details

#add_controller_routing(routing) ⇒ Object



10
11
12
# File 'lib/activeai/router.rb', line 10

def add_controller_routing(routing)
  @routings << routing
end

#add_controller_routing_from_path(path) ⇒ Object



14
15
16
17
# File 'lib/activeai/router.rb', line 14

def add_controller_routing_from_path(path)
  routing = YAML::load(File.read(path))
  add_controller_routing(routing)
end

#auto_load_routing(folder) ⇒ Object



19
20
21
22
23
24
# File 'lib/activeai/router.rb', line 19

def auto_load_routing(folder)
  paths = Dir[folder.join("**", "*.yml")]
  paths.each do |path|
    add_controller_routing_from_path(path)
  end
end

#behaviorObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activeai/router.rb', line 26

def behavior
  raw_examples = [UNMATCHED] + @routings.map do |routing|
    routing['examples'].reject do |example|
      example['Route'] == 'None'
    end.map do |example|
      example.slice('Match', 'Route')
    end
  end.flatten
  examples = ActiveAI.route_examples_to_function_call_examples(raw_examples)

  ActiveAI::Behavior::LLM::WriteFunctionCall.new(@llm, { examples: examples })
end

#find_controller(request) ⇒ Object

ActiveAI::Behavior::LLM::FollowStructuredExamples.new(@llm, config) end



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/activeai/router.rb', line 54

def find_controller(request)
  function = behavior.call(request) # TODO maybe the behavior should return function and params as well. seems right
  
  *controller_path, action_name = function[:path].split(".")
  controller_name = controller_path.join("/").presence

  # TODO verify it's the right controller and the action name exists and it's not a reserved / internal thing

  if controller_name.blank? || action_name.blank? || action_name == 'unmatched'
    return nil
  else
    return (controller_name + "_controller").classify.constantize
    # TODO need protection (somewhere) from using controllers that aren't allowed
    # maybe router has a whitelist? since we're taking user input
    # idk problem for later not now
  end
end