-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build support for arrays / objects on tools
This allows much more complex calls with tools
- Loading branch information
Showing
14 changed files
with
481 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
omniai (1.9.0) | ||
omniai (1.9.1) | ||
event_stream_parser | ||
http | ||
zeitwerk | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
class Tool | ||
# Represents a schema object. | ||
# | ||
# @example | ||
# array = OmniAI::Tool::Array.new( | ||
# description: 'A list of people.', | ||
# items: OmniAI::Tool::Object.new( | ||
# properties: { | ||
# name: OmniAI::Tool::Property.string(description: 'The name of the person.'), | ||
# age: OmniAI::Tool::Property.integer(description: 'The age of the person.'), | ||
# }, | ||
# required: %i[name] | ||
# ), | ||
# min_items: 1, | ||
# max_items: 5, | ||
# }) | ||
class Array | ||
TYPE = 'array' | ||
|
||
# @!attribute [rw] items | ||
# @return [OmniAI::Tool::Object, OmniAI::Tool::Array, OmniAI::Tool::Property] | ||
attr_accessor :items | ||
|
||
# @!attribute [rw] max_items | ||
# @return [Integer, nil] | ||
attr_accessor :max_items | ||
|
||
# @!attribute [rw] min_items | ||
# @return [Integer, nil] | ||
attr_accessor :min_items | ||
|
||
# @!attribute [rw] description | ||
# @return [String, nil] | ||
attr_accessor :description | ||
|
||
# @param items [OmniAI::Tool::Object, OmniAI::Tool::Array, OmniAI::Tool::Property] required | ||
# @param max_items [Integer] optional | ||
# @param min_items [Integer] optional | ||
# @param description [String] optional | ||
def initialize(items:, max_items: nil, min_items: nil, description: nil) | ||
@items = items | ||
@description = description | ||
@max_items = max_items | ||
@min_items = min_items | ||
end | ||
|
||
# @example | ||
# array.serialize # => { type: 'array', items: { type: 'string' } } | ||
# | ||
# @return [Hash] | ||
def serialize | ||
{ | ||
type: TYPE, | ||
description: @description, | ||
items: @items.serialize, | ||
maxItems: @max_items, | ||
minItems: @min_items, | ||
}.compact | ||
end | ||
|
||
# @example | ||
# array.parse(['1', '2', '3']) # => [1, 2, 3] | ||
# @param args [Array] | ||
# | ||
# @return [Array] | ||
def parse(args) | ||
args.map { |arg| @items.parse(arg) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module OmniAI | ||
class Tool | ||
# Represents a schema object. | ||
# | ||
# @example | ||
# object = OmniAI::Tool::Object.new( | ||
# properties: { | ||
# name: OmniAI::Tool::Property.string(description: 'The name of the person.'), | ||
# age: OmniAI::Tool::Property.integer(description: 'The age of the person.'), | ||
# }, | ||
# required: %i[name] | ||
# }) | ||
class Object | ||
TYPE = 'object' | ||
|
||
# @!attribute [rw] properties | ||
# @return [Hash] | ||
attr_accessor :properties | ||
|
||
# @!attribute [rw] required | ||
# @return [Array<String>] | ||
attr_accessor :required | ||
|
||
# @!attribute [rw] description | ||
# @return [String, nil] | ||
attr_accessor :description | ||
|
||
# @param properties [Hash] | ||
# @param required [Array<String>] | ||
# @return [OmniAI::Tool::Parameters] | ||
def initialize(properties: {}, required: [], description: nil) | ||
@properties = properties | ||
@required = required | ||
@description = description | ||
end | ||
|
||
# @return [Hash] | ||
def serialize | ||
{ | ||
type: TYPE, | ||
description: @description, | ||
properties: @properties.transform_values(&:serialize), | ||
required: @required, | ||
}.compact | ||
end | ||
|
||
# @param args [Hash] | ||
# | ||
# @return [Hash] | ||
def parse(args) | ||
result = {} | ||
@properties.each do |name, property| | ||
value = args[String(name)] | ||
result[name.intern] = property.parse(value) unless value.nil? | ||
end | ||
result | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.