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:) { ... } ⇒ 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(only: nil) ⇒ String
Converts schemas to Anthropic-compatible format.
-
#to_google_gemini_format(only: nil) ⇒ String
Converts schemas to Google Gemini-compatible format.
-
#to_openai_format(only: nil) ⇒ String
Converts schemas to OpenAI-compatible format.
-
#valid_schemas(only: nil) ⇒ 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:) { ... } ⇒ Object
Adds a function to the schemas
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.
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
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
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
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.
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 |