Module: Raif::Concerns::Llms::Google::ToolFormatting

Extended by:
ActiveSupport::Concern
Included in:
Llms::Google
Defined in:
app/models/raif/concerns/llms/google/tool_formatting.rb

Instance Method Summary collapse

Instance Method Details

#build_forced_tool_choice(tool_name) ⇒ Object



48
49
50
# File 'app/models/raif/concerns/llms/google/tool_formatting.rb', line 48

def build_forced_tool_choice(tool_name)
  { mode: "ANY", allowedFunctionNames: [tool_name] }
end

#build_tools_parameter(model_completion) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/raif/concerns/llms/google/tool_formatting.rb', line 6

def build_tools_parameter(model_completion)
  tools = []
  function_declarations = []

  # If we support native tool use and have tools available, add them to the request
  if supports_native_tool_use? && model_completion.available_model_tools.any?
    model_completion.available_model_tools_map.each do |_tool_name, tool|
      if tool.provider_managed?
        # Provider-managed tools are added as separate tool entries
        tools << format_provider_managed_tool(tool)
      else
        function_declarations << {
          name: tool.tool_name,
          description: tool.tool_description,
          parameters: sanitize_schema_for_google(tool.tool_arguments_schema)
        }
      end
    end
  end

  # Add function declarations if any
  if function_declarations.any?
    tools << { functionDeclarations: function_declarations }
  end

  tools
end

#format_provider_managed_tool(tool) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/raif/concerns/llms/google/tool_formatting.rb', line 34

def format_provider_managed_tool(tool)
  validate_provider_managed_tool_support!(tool)

  case tool.name
  when "Raif::ModelTools::ProviderManaged::WebSearch"
    { google_search: {} }
  when "Raif::ModelTools::ProviderManaged::CodeExecution"
    { code_execution: {} }
  else
    raise Raif::Errors::UnsupportedFeatureError,
      "Invalid provider-managed tool: #{tool.name} for #{key}"
  end
end