Class: RubyLLM::Toolbox::Tools::WebSearch

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/toolbox/tools/web_search.rb

Overview

SAFE. Searches the web through the configured adapter (Tavily by default) and returns ranked results plus an optional synthesized answer. Requires an API key for the default Tavily adapter (config.tavily_api_key).

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#call, exec_tool!, exec_tool?, #initialize, #name

Constructor Details

This class inherits a constructor from RubyLLM::Toolbox::Base

Instance Method Details

#execute(query:, max_results: 5) ⇒ Object



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

def execute(query:, max_results: 5)
  q = query.to_s.strip
  return error("query must not be empty", code: :bad_query) if q.empty?

  n = max_results.to_i
  n = 5 if n <= 0
  n = 10 if n > 10

  result = adapter.search(q, max_results: n)
  truncate(format_results(q, result))
rescue Search::Error => e
  code = e.message.match?(/missing .*(api key|url)/i) ? :no_api_key : :search_failed
  error(e.message, code: code)
end