summaryrefslogtreecommitdiff
path: root/components/providers/openai/tools.rb
blob: 50eead9fa1fa68b9a5c0acfdfccbfa1a62aeda87 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

require_relative '../../embedding'
require_relative '../../../logic/cartridge/safety'

require 'concurrent'

module NanoBot
  module Components
    module Providers
      class OpenAI < Base
        module Tools
          def self.apply(cartridge, function_cartridge, tools, feedback)
            prepared_tools = NanoBot::Logic::OpenAI::Tools.prepare(function_cartridge, tools)

            # TODO: Confirm before starting futures.
            futures = prepared_tools.map do |tool|
              Concurrent::Promises.future { process!(tool, feedback, function_cartridge, cartridge) }
            end

            results = Concurrent::Promises.zip(*futures).value!

            results.map do |applied_tool|
              {
                who: 'tool',
                message: applied_tool[:output],
                meta: { id: applied_tool[:id], name: applied_tool[:name] }
              }
            end
          end

          def self.process!(tool, feedback, _function_cartridge, cartridge)
            feedback.call(
              { should_be_stored: false,
                interaction: { who: 'AI', message: nil, meta: {
                  tool: { action: 'call', id: tool[:id], name: tool[:name], parameters: tool[:parameters] }
                } } }
            )

            call = {
              parameters: %w[parameters],
              values: [tool[:parameters]],
              safety: { sandboxed: Logic::Cartridge::Safety.sandboxed?(cartridge) }
            }

            if %i[fennel lua clojure].count { |key| !tool[:source][key].nil? } > 1
              raise StandardError, 'conflicting tools'
            end

            if !tool[:source][:fennel].nil?
              call[:source] = tool[:source][:fennel]
              tool[:output] = Components::Embedding.fennel(**call)
            elsif !tool[:source][:clojure].nil?
              call[:source] = tool[:source][:clojure]
              tool[:output] = Components::Embedding.clojure(**call)
            elsif !tool[:source][:lua].nil?
              call[:source] = tool[:source][:lua]
              tool[:output] = Components::Embedding.lua(**call)
            else
              raise 'missing source code'
            end

            feedback.call(
              { should_be_stored: false,
                interaction: { who: 'AI', message: nil, meta: {
                  tool: {
                    action: 'response', id: tool[:id], name: tool[:name],
                    parameters: tool[:parameters], output: tool[:output]
                  }
                } } }
            )

            tool
          end
        end
      end
    end
  end
end