summaryrefslogtreecommitdiff
path: root/logic
diff options
context:
space:
mode:
authoricebaker <icebaker@proton.me>2023-12-14 22:49:06 -0300
committericebaker <icebaker@proton.me>2023-12-14 22:49:06 -0300
commite6f0374cc8844d4a053db4e68feee23ffc793d73 (patch)
treea06163503c7a4f754be1a91bbaaee9adf9e7c7b2 /logic
parentfb96658a1ca4b6e3e0505e7a39f660e1a05b3c6e (diff)
adding support for google gemini
Diffstat (limited to 'logic')
-rw-r--r--logic/providers/google/tokens.rb16
-rw-r--r--logic/providers/google/tools.rb60
-rw-r--r--logic/providers/openai/tokens.rb16
-rw-r--r--logic/providers/openai/tools.rb7
4 files changed, 98 insertions, 1 deletions
diff --git a/logic/providers/google/tokens.rb b/logic/providers/google/tokens.rb
new file mode 100644
index 0000000..3d5492f
--- /dev/null
+++ b/logic/providers/google/tokens.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+require 'openai'
+
+module NanoBot
+ module Logic
+ module Google
+ module Tokens
+ def self.apply_policies!(_cartridge, payload)
+ payload[:contents] = payload[:contents].map { |message| message.except(:_meta) }
+ payload
+ end
+ end
+ end
+ end
+end
diff --git a/logic/providers/google/tools.rb b/logic/providers/google/tools.rb
new file mode 100644
index 0000000..e1396d6
--- /dev/null
+++ b/logic/providers/google/tools.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+require 'json'
+require 'babosa'
+
+require_relative '../../helpers/hash'
+
+module NanoBot
+ module Logic
+ module Google
+ module Tools
+ def self.prepare(cartridge, tools)
+ applies = []
+
+ tools = Marshal.load(Marshal.dump(tools))
+
+ tools.each do |tool|
+ tool = Helpers::Hash.symbolize_keys(tool)
+
+ cartridge.each do |candidate|
+ candidate_key = candidate[:name].to_slug.normalize.gsub('-', '_')
+ tool_key = tool[:functionCall][:name].to_slug.normalize.gsub('-', '_')
+
+ next unless candidate_key == tool_key
+
+ source = {}
+
+ source[:clojure] = candidate[:clojure] if candidate[:clojure]
+ source[:fennel] = candidate[:fennel] if candidate[:fennel]
+ source[:lua] = candidate[:lua] if candidate[:lua]
+
+ applies << {
+ label: candidate[:name],
+ name: tool[:functionCall][:name],
+ type: 'function',
+ parameters: tool[:functionCall][:args],
+ source:
+ }
+ end
+ end
+
+ raise 'missing tool' if applies.size != tools.size
+
+ applies
+ end
+
+ def self.adapt(cartridge)
+ output = {
+ name: cartridge[:name],
+ description: cartridge[:description]
+ }
+
+ output[:parameters] = (cartridge[:parameters] || { type: 'object', properties: {} })
+
+ output
+ end
+ end
+ end
+ end
+end
diff --git a/logic/providers/openai/tokens.rb b/logic/providers/openai/tokens.rb
new file mode 100644
index 0000000..60efa60
--- /dev/null
+++ b/logic/providers/openai/tokens.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+require 'openai'
+
+module NanoBot
+ module Logic
+ module OpenAI
+ module Tokens
+ def self.apply_policies!(_cartridge, payload)
+ payload[:messages] = payload[:messages].map { |message| message.except(:_meta) }
+ payload
+ end
+ end
+ end
+ end
+end
diff --git a/logic/providers/openai/tools.rb b/logic/providers/openai/tools.rb
index 1b2882a..f00176c 100644
--- a/logic/providers/openai/tools.rb
+++ b/logic/providers/openai/tools.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'json'
+require 'babosa'
require_relative '../../helpers/hash'
@@ -17,7 +18,10 @@ module NanoBot
tool = Helpers::Hash.symbolize_keys(tool)
cartridge.each do |candidate|
- next unless tool[:function][:name] == candidate[:name]
+ candidate_key = candidate[:name].to_slug.normalize.gsub('-', '_')
+ tool_key = tool[:function][:name].to_slug.normalize.gsub('-', '_')
+
+ next unless candidate_key == tool_key
source = {}
@@ -27,6 +31,7 @@ module NanoBot
applies << {
id: tool[:id],
+ label: candidate[:name],
name: tool[:function][:name],
type: 'function',
parameters: JSON.parse(tool[:function][:arguments]),