summaryrefslogtreecommitdiff
path: root/spec/logic/providers/openai/tools_spec.rb
diff options
context:
space:
mode:
authoricebaker <icebaker@proton.me>2023-11-18 19:07:10 -0300
committericebaker <icebaker@proton.me>2023-11-18 19:07:10 -0300
commit8ae78b954350755a47a13133668dba93bac15f37 (patch)
tree9cdc3bb770d778bd8d00675fdbc1f27a6e27e37c /spec/logic/providers/openai/tools_spec.rb
parentab22d1bbe37093912cb7418b3c945153a15f4255 (diff)
adding support for tools
Diffstat (limited to 'spec/logic/providers/openai/tools_spec.rb')
-rw-r--r--spec/logic/providers/openai/tools_spec.rb73
1 files changed, 73 insertions, 0 deletions
diff --git a/spec/logic/providers/openai/tools_spec.rb b/spec/logic/providers/openai/tools_spec.rb
new file mode 100644
index 0000000..c92c374
--- /dev/null
+++ b/spec/logic/providers/openai/tools_spec.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+require 'yaml'
+
+require_relative '../../../../logic/providers/openai/tools'
+
+RSpec.describe NanoBot::Logic::OpenAI::Tools do
+ context 'tools' do
+ let(:cartridge) { load_symbolized('cartridges/tools.yml') }
+
+ context 'adapt' do
+ it 'adapts to OpenAI expected format' do
+ expect(described_class.adapt(cartridge[:tools][0])).to eq(
+ { type: 'function',
+ function: {
+ name: 'get-current-weather',
+ description: 'Get the current weather in a given location.',
+ parameters: {
+ type: 'object',
+ properties: {
+ location: { type: 'string' },
+ unit: { type: 'string' }
+ }
+ }
+ } }
+ )
+
+ expect(described_class.adapt(cartridge[:tools][1])).to eq(
+ { type: 'function',
+ function: {
+ name: 'what-time-is-it',
+ description: 'Returns the current date and time.',
+ parameters: { properties: {}, type: 'object' }
+ } }
+ )
+
+ expect(described_class.adapt(cartridge[:tools][2])).to eq(
+ { type: 'function',
+ function: {
+ name: 'sh',
+ description: "It has access to computer users' data and can be used to run shell commands, similar to those in a Linux terminal, to extract information. Please be mindful and careful to avoid running dangerous commands on users' computers.",
+ parameters: {
+ type: 'object',
+ properties: {
+ command: {
+ type: 'array',
+ description: 'An array of strings that represents a shell command along with its arguments or options. For instance, `["df", "-h"]` executes the `df -h` command, where each array element specifies either the command itself or an associated argument/option.',
+ items: { type: 'string' }
+ }
+ }
+ }
+ } }
+ )
+ end
+ end
+
+ context 'prepare' do
+ let(:tools) { load_symbolized('providers/openai/tools.yml') }
+
+ it 'prepare tools to be executed' do
+ expect(described_class.prepare(cartridge[:tools], tools)).to eq(
+ [{ id: 'call_XYZ',
+ name: 'get-current-weather',
+ type: 'function',
+ parameters: { 'location' => 'Tokyo, Japan' },
+ source: { fennel: "(let [{:location location :unit unit} parameters]\n (.. \"Here is the weather in \" location \", in \" unit \": 35.8°C.\"))\n" } },
+ { id: 'call_ZYX', name: 'what-time-is-it', type: 'function', parameters: {},
+ source: { fennel: "(os.date)\n" } }]
+ )
+ end
+ end
+ end
+end