diff options
author | icebaker <icebaker@proton.me> | 2023-11-18 19:07:10 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-11-18 19:07:10 -0300 |
commit | 8ae78b954350755a47a13133668dba93bac15f37 (patch) | |
tree | 9cdc3bb770d778bd8d00675fdbc1f27a6e27e37c /components/providers/openai/tools.rb | |
parent | ab22d1bbe37093912cb7418b3c945153a15f4255 (diff) |
adding support for tools
Diffstat (limited to 'components/providers/openai/tools.rb')
-rw-r--r-- | components/providers/openai/tools.rb | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/components/providers/openai/tools.rb b/components/providers/openai/tools.rb new file mode 100644 index 0000000..ea34ae6 --- /dev/null +++ b/components/providers/openai/tools.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require_relative '../../embedding' + +require 'concurrent' + +module NanoBot + module Components + module Providers + class OpenAI < Base + module Tools + def self.apply(cartridge, tools, feedback) + prepared_tools = NanoBot::Logic::OpenAI::Tools.prepare(cartridge, tools) + + futures = prepared_tools.map do |tool| + Concurrent::Promises.future { process!(tool, feedback) } + 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) + 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: false } + + 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 |