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:) { ... } ⇒ 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
# File 'lib/oxaiworkers/tool_definition.rb', line 108

def add_function(method_name:, description:, &)
  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
  end

  @schemas[method_name] = {
    type: 'function',
    function: { name:, description:, parameters: }.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_format(only: nil) ⇒ String

Converts schemas to Anthropic-compatible format

Returns:

  • (String)

    JSON string of schemas in Anthropic format



148
149
150
151
152
# File 'lib/oxaiworkers/tool_definition.rb', line 148

def to_anthropic_format(only: nil)
  valid_schemas(only:).values.map do |schema|
    schema[:function].transform_keys('parameters' => 'input_schema')
  end
end

#to_google_gemini_format(only: nil) ⇒ String

Converts schemas to Google Gemini-compatible format

Returns:

  • (String)

    JSON string of schemas in Google Gemini format



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

def to_google_gemini_format(only: nil)
  valid_schemas(only:).values.map { |schema| schema[:function] }
end

#to_openai_format(only: nil) ⇒ String

Converts schemas to OpenAI-compatible format

Returns:

  • (String)

    JSON string of schemas in OpenAI format



129
130
131
# File 'lib/oxaiworkers/tool_definition.rb', line 129

def to_openai_format(only: nil)
  valid_schemas(only:).values
end

#valid_schemas(only: nil) ⇒ Hash<Symbol, Hash>

Returns a subset of schemas based on the provided filter.

Parameters:

  • only (Array<Symbol>) (defaults to: nil)

    An optional array of schema names to filter by.

Returns:

  • (Hash<Symbol, Hash>)

    A hash of schemas with their corresponding names as keys.



137
138
139
140
141
142
143
# File 'lib/oxaiworkers/tool_definition.rb', line 137

def valid_schemas(only: nil)
  if only.nil?
    @schemas
  else
    @schemas.select { |name, _schema| only.include?(name) }
  end
end