Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Hash for Parameters in Tool #59

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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