Class: Nanobot::Agent::Tools::WebSearch

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/nanobot/agent/tools/web.rb

Overview

Tool for web search using Brave Search API

Constant Summary collapse

SEARCH_OPEN_TIMEOUT =
10
SEARCH_READ_TIMEOUT =
15

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ WebSearch

Returns a new instance of WebSearch.

Parameters:

  • api_key (String, nil) (defaults to: nil) —

    Brave Search API key (falls back to BRAVE_SEARCH_API_KEY env var)



21
22
23
24
# File 'lib/nanobot/agent/tools/web.rb', line 21

def initialize(api_key: nil)
  super()
  @api_key = api_key || ENV.fetch('BRAVE_SEARCH_API_KEY', nil)
end

Instance Method Details

#execute(query:, count: 5) ⇒ String

Perform a web search and return formatted results.

Parameters:

  • query (String) —

    search query

  • count (Integer) (defaults to: 5) —

    number of results to return

Returns:

  • (String) —

    formatted search results or error message



30
31
32
33
34
35
36
37
38
39
# File 'lib/nanobot/agent/tools/web.rb', line 30

def execute(query:, count: 5)
  return 'Error: Brave Search API key not configured' unless @api_key

  begin
    response = search(query, count)
    format_results(response)
  rescue StandardError => e
    "Error performing web search: #{e.message}"
  end
end