Class: OxAiWorkers::ToolDefinition::FunctionSchemas

Inherits:
Object
  • Object
show all
Defined in:
lib/oxaiworkers/tool_definition.rb

Overview

Manages schemas for functions

Instance Method Summary collapse

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

Parameters:

  • method_name (Symbol)

    Name of the method to add

  • description (String)

    Description of the function

Yields:

  • Block that defines the parameters for the function

Raises:

  • (ArgumentError)

    If a block is defined and no parameters are specified for the function



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.

Parameters:

  • method_name (Symbol)

    The name of the method.

Returns:

  • (String)

    The full function name, which is the tool name concatenated with the 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_formatString

Converts schemas to Anthropic-compatible format

Returns:

  • (String)

    JSON string of schemas in Anthropic 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_formatString

Converts schemas to Google Gemini-compatible format

Returns:

  • (String)

    JSON string of schemas in Google Gemini 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_formatString

Converts schemas to OpenAI-compatible format

Returns:

  • (String)

    JSON string of schemas in OpenAI 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_schemasHash<Symbol, Hash>

Returns a subset of schemas based on the provided filter.

Parameters:

  • only (Array<Symbol>)

    An optional array of schema names to filter by.

Returns:

  • (Hash<Symbol, Hash>)

    A hash of schemas with their corresponding names as keys.



157
158
159
# File 'lib/oxaiworkers/tool_definition.rb', line 157

def valid_schemas
  @schemas
end