Module: RubyLLM::Pollinations::Provider::Tools

Included in:
Pollinations
Defined in:
lib/ruby_llm/pollinations/provider/tools.rb

Constant Summary collapse

EMPTY_PARAMETERS_SCHEMA =
{
  'type' => 'object',
  'properties' => {},
  'required' => [],
  'additionalProperties' => false,
  'strict' => true
}.freeze

Class Method Summary collapse

Class Method Details

.format_tool_calls(tool_calls) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_llm/pollinations/provider/tools.rb', line 43

def format_tool_calls(tool_calls)
  return nil unless tool_calls&.any?

  tool_calls.map do |_, tc|
    {
      id: tc.id,
      type: 'function',
      function: {
        name: tc.name,
        arguments: JSON.generate(tc.arguments)
      }
    }
  end
end

.parameters_schema_for(tool) ⇒ Object



17
18
19
# File 'lib/ruby_llm/pollinations/provider/tools.rb', line 17

def parameters_schema_for(tool)
  tool.params_schema || schema_from_parameters(tool.parameters)
end

.parse_tool_call_arguments(tool_call) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/ruby_llm/pollinations/provider/tools.rb', line 58

def parse_tool_call_arguments(tool_call)
  arguments = tool_call.dig('function', 'arguments')

  if arguments.nil? || arguments.empty?
    {}
  else
    JSON.parse(arguments)
  end
end

.parse_tool_calls(tool_calls, parse_arguments: true) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_llm/pollinations/provider/tools.rb', line 68

def parse_tool_calls(tool_calls, parse_arguments: true)
  return nil unless tool_calls&.any?

  tool_calls.to_h do |tc|
    [
      tc['id'],
      RubyLLM::ToolCall.new(
        id: tc['id'],
        name: tc.dig('function', 'name'),
        arguments: parse_arguments ? parse_tool_call_arguments(tc) : tc.dig('function', 'arguments')
      )
    ]
  end
end

.schema_from_parameters(parameters) ⇒ Object



21
22
23
24
# File 'lib/ruby_llm/pollinations/provider/tools.rb', line 21

def schema_from_parameters(parameters)
  schema_definition = RubyLLM::Tool::SchemaDefinition.from_parameters(parameters)
  schema_definition&.json_schema || EMPTY_PARAMETERS_SCHEMA
end

.tool_for(tool) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_llm/pollinations/provider/tools.rb', line 26

def tool_for(tool)
  parameters_schema = parameters_schema_for(tool)

  definition = {
    type: 'function',
    function: {
      name: tool.name,
      description: tool.description,
      parameters: parameters_schema
    }
  }

  return definition if tool.provider_params.empty?

  RubyLLM::Utils.deep_merge(definition, tool.provider_params)
end