Class: Legion::CLI::Chat::Tools::WebSearch

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/legion/cli/chat/tools/web_search.rb

Instance Method Summary collapse

Instance Method Details

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/cli/chat/tools/web_search.rb', line 16

def execute(query:, max_results: 5)
  require 'legion/cli/chat/web_search'
  results = Chat::WebSearch.search(query, max_results: max_results)

  output = results[:results].map do |r|
    "### #{r[:title]}\n#{r[:url]}\n#{r[:snippet]}"
  end.join("\n\n")

  output += "\n\n---\n\n## Top Result Content\n\n#{results[:fetched_content]}" if results[:fetched_content]

  output
rescue Chat::WebSearch::SearchError => e
  Legion::Logging.warn("WebSearch#execute search error for query #{query}: #{e.message}") if defined?(Legion::Logging)
  "Search error: #{e.message}"
rescue StandardError => e
  Legion::Logging.warn("WebSearch#execute failed for query #{query}: #{e.message}") if defined?(Legion::Logging)
  "Error: #{e.message}"
end