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 /logic/providers/openai | |
parent | ab22d1bbe37093912cb7418b3c945153a15f4255 (diff) |
adding support for tools
Diffstat (limited to 'logic/providers/openai')
-rw-r--r-- | logic/providers/openai/tools.rb | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/logic/providers/openai/tools.rb b/logic/providers/openai/tools.rb new file mode 100644 index 0000000..080d81e --- /dev/null +++ b/logic/providers/openai/tools.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require 'json' + +require_relative '../../helpers/hash' + +module NanoBot + module Logic + module OpenAI + module Tools + def self.prepare(cartridge, tools) + applies = [] + tools.each do |tool| + # TODO: Does this mutate the hash? + tool = Helpers::Hash.symbolize_keys(tool) + + cartridge.each do |candidate| + next unless ( + candidate[:type].nil? || + (candidate[:type] == 'function' && tool[:type] == candidate[:type]) + ) && tool[:function][:name] == candidate[:name] + + source = {} + + source[:clojure] = candidate[:clojure] if candidate[:clojure] + source[:fennel] = candidate[:fennel] if candidate[:fennel] + source[:lua] = candidate[:lua] if candidate[:lua] + + applies << { + id: tool[:id], + name: tool[:function][:name], + type: candidate[:type] || 'function', + parameters: JSON.parse(tool[:function][:arguments]), + source: + } + end + end + + applies + end + + def self.adapt(cartridge) + raise 'unsupported tool' if cartridge[:type] != 'function' && !cartridge[:type].nil? + + adapted = { + type: cartridge[:type] || 'function', + function: { + name: cartridge[:name], description: cartridge[:description], + parameters: { type: 'object', properties: {} } + } + } + + properties = adapted[:function][:parameters][:properties] + + adapted[:function][:parameters][:required] = cartridge[:required] if cartridge[:required] + + cartridge[:parameters]&.each do |parameter| + key = parameter[:name].to_sym + properties[key] = {} + properties[key][:type] = parameter[:type] || 'string' + properties[key][:description] = parameter[:description] if parameter[:description] + properties[key][:items] = parameter[:items].slice(:type) if parameter[:items] + end + + adapted + end + end + end + end +end |