Class: OxAiWorkers::ToolDefinition::FunctionSchemas
- Inherits:
-
Object
- Object
- OxAiWorkers::ToolDefinition::FunctionSchemas
- Defined in:
- lib/oxaiworkers/tool_definition.rb
Overview
Manages schemas for functions
Instance Method Summary collapse
-
#add_function(method_name:, description:, strict:) { ... } ⇒ Object
Adds a function to the schemas.
-
#function_name(method_name) ⇒ String
Returns the full function name for the given method name.
-
#initialize(tool_name) ⇒ FunctionSchemas
constructor
A new instance of FunctionSchemas.
-
#to_anthropic_format ⇒ String
Converts schemas to Anthropic-compatible format.
-
#to_google_gemini_format ⇒ String
Converts schemas to Google Gemini-compatible format.
-
#to_openai_format ⇒ String
Converts schemas to OpenAI-compatible format.
-
#valid_schemas ⇒ Hash<Symbol, Hash>
Returns a subset of schemas based on the provided filter.
Constructor Details
#initialize(tool_name) ⇒ FunctionSchemas
Returns a new instance of FunctionSchemas.
89 90 91 92 |
# File 'lib/oxaiworkers/tool_definition.rb', line 89 def initialize(tool_name) @schemas = {} @tool_name = tool_name end |
Instance Method Details
#add_function(method_name:, description:, strict:) { ... } ⇒ Object
Adds a function to the schemas
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/oxaiworkers/tool_definition.rb', line 108 def add_function(method_name:, description:, strict:, &) name = function_name(method_name) if block_given? parameters = ParameterBuilder.new(parent_type: 'object').build(&) if parameters[:properties].empty? raise ArgumentError, 'Function parameters must have at least one property defined within it, if a block is provided' end else # Create an empty parameters object with additionalProperties: false when strict is true parameters = { type: 'object', properties: {} } # parameters[:additionalProperties] = false if strict end function_params = { name:, description:, parameters: } function_params[:strict] = strict @schemas[method_name] = { type: 'function', function: function_params.compact } end |
#function_name(method_name) ⇒ String
Returns the full function name for the given method name.
98 99 100 |
# File 'lib/oxaiworkers/tool_definition.rb', line 98 def function_name(method_name) "#{@tool_name}__#{method_name}" end |
#to_anthropic_format ⇒ String
Converts schemas to Anthropic-compatible format
164 165 166 167 168 169 170 |
# File 'lib/oxaiworkers/tool_definition.rb', line 164 def to_anthropic_format @schemas_anthropic ||= Marshal.load(Marshal.dump(valid_schemas.values)) @schemas_anthropic.map do |schema| schema[:function].delete(:strict) schema[:function].transform_keys(parameters: :input_schema) end end |
#to_google_gemini_format ⇒ String
Converts schemas to Google Gemini-compatible format
175 176 177 |
# File 'lib/oxaiworkers/tool_definition.rb', line 175 def to_google_gemini_format valid_schemas.values.map { |schema| schema[:function] } end |
#to_openai_format ⇒ String
Converts schemas to OpenAI-compatible format
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/oxaiworkers/tool_definition.rb', line 136 def to_openai_format @schemas_openai ||= Marshal.load(Marshal.dump(valid_schemas.values)) @schemas_openai.map do |schema| if schema[:function][:strict] == true parameters = schema[:function][:parameters] parameters[:additionalProperties] = false parameters[:properties].each do |key, param| param[:type] = parameters[:required].include?(key.to_s) ? param[:type] : [param[:type], 'null'] end parameters[:required] = parameters[:properties].keys.map(&:to_s) else schema[:function].delete(:strict) end schema end end |
#valid_schemas ⇒ Hash<Symbol, Hash>
Returns a subset of schemas based on the provided filter.
157 158 159 |
# File 'lib/oxaiworkers/tool_definition.rb', line 157 def valid_schemas @schemas end |