Skip to content

Commit

Permalink
Suport hash for tool parameters
Browse files Browse the repository at this point in the history
Using a hash skips a handful of verification steps.
  • Loading branch information
ksylvest committed Aug 17, 2024
1 parent d459b32 commit a8fea87
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
omniai (1.8.0)
omniai (1.8.1)
event_stream_parser
http
zeitwerk
Expand Down
9 changes: 6 additions & 3 deletions lib/omniai/tool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def initialize(function, name:, description: nil, parameters: nil)
# # parameters: {
# # type: 'object',
# # properties: {
# # n: OmniAI::Tool::Property.integer(description: 'The nth Fibonacci number to calculate')
# # n: {
# # description: 'The nth Fibonacci number to calculate')
# # type: 'integer'
# # }
# # },
# # required: ['n']
# # }
Expand All @@ -71,7 +74,7 @@ def serialize(context: nil)
function: {
name: @name,
description: @description,
parameters: @parameters&.prepare,
parameters: @parameters.is_a?(Tool::Parameters) ? @parameters.serialize : @parameters,
}.compact,
}
end
Expand All @@ -83,7 +86,7 @@ def serialize(context: nil)
# @param args [Hash]
# @return [String]
def call(args = {})
@function.call(**(@parameters ? @parameters.parse(args) : args))
@function.call(**(@parameters.is_a?(Tool::Parameters) ? @parameters.parse(args) : args))
end
end
end
4 changes: 2 additions & 2 deletions lib/omniai/tool/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def initialize(type: Type::OBJECT, properties: {}, required: [])
end

# @return [Hash]
def prepare
def serialize
{
type: @type,
properties: @properties.transform_values(&:prepare),
properties: @properties.transform_values(&:serialize),
required: @required,
}.compact
end
Expand Down
4 changes: 2 additions & 2 deletions lib/omniai/tool/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def initialize(type:, description: nil, enum: nil)
end

# @example
# property.prepare
# property.serialize
# # {
# # type: 'string',
# # description: 'The unit (e.g. "fahrenheit" or "celsius").'
# # enum: %w[fahrenheit celsius]
# # }
#
# @return [Hash]
def prepare
def serialize
{
type: @type,
description: @description,
Expand Down
2 changes: 1 addition & 1 deletion lib/omniai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module OmniAI
VERSION = '1.8.0'
VERSION = '1.8.1'
end
4 changes: 2 additions & 2 deletions spec/omniai/tool/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

let(:required) { %i[n] }

describe '#prepare' do
describe '#serialize' do
it 'converts the parameters to a hash' do
expect(parameters.prepare).to eq({
expect(parameters.serialize).to eq({
type: 'object',
properties: {
n: { type: 'integer', description: 'The nth number to calculate.' },
Expand Down
4 changes: 2 additions & 2 deletions spec/omniai/tool/property_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
it { expect(property.type).to eq('number') }
end

describe '#prepare' do
describe '#serialize' do
it 'converts the property to a hash' do
expect(property.prepare).to eq({
expect(property.serialize).to eq({
type: 'string',
description: 'The unit (e.g. "fahrenheit" or "celsius")',
enum: %w[fahrenheit celsius],
Expand Down

0 comments on commit a8fea87

Please sign in to comment.