Class: Prescient::Tool::SearXNG
- Defined in:
- lib/prescient/tool/searxng.rb
Overview
SearXNG web-search tool adapter.
Constant Summary collapse
- NETWORK_ERRORS =
Network failures that should be reported as connection errors.
[Net::OpenTimeout, Net::ReadTimeout, Timeout::Error, SocketError, Errno::ECONNREFUSED].freeze
Constants inherited from Base
Base::DEFAULT_MAX_RESPONSE_BYTES, Base::DEFAULT_MAX_RESULTS, Base::DEFAULT_TIMEOUT, Base::MAX_QUERY_LENGTH
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(**options) ⇒ SearXNG
constructor
A new instance of SearXNG.
-
#search(query, limit: nil, timeout: nil) ⇒ Hash
Search SearXNG and return normalized result metadata.
-
#validate_configuration! ⇒ void
protected
Validate SearXNG-specific configuration.
Methods inherited from Base
#max_response_bytes, #request_timeout, #result_limit, #validate_query
Constructor Details
#initialize(**options) ⇒ SearXNG
Returns a new instance of SearXNG.
17 18 19 20 |
# File 'lib/prescient/tool/searxng.rb', line 17 def initialize(**) super @base_url = normalized_base_url(.fetch(:url)) end |
Instance Method Details
#search(query, limit: nil, timeout: nil) ⇒ Hash
Search SearXNG and return normalized result metadata.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/prescient/tool/searxng.rb', line 27 def search(query, limit: nil, timeout: nil) normalized_query = validate_query(query) requested_limit = result_limit(limit) requested_timeout = request_timeout(timeout) response = HTTParty.get( search_url, query: request_parameters(normalized_query), timeout: requested_timeout, ) validate_response!(response) { tool: 'web_search', query: normalized_query, source: 'searxng', results: normalize_results(response.parsed_response, requested_limit), } rescue Prescient::Error raise rescue *NETWORK_ERRORS => e raise Prescient::ToolConnectionError, "SearXNG request failed: #{e.class}" rescue StandardError => e raise Prescient::ToolError, "SearXNG request failed: #{e.}" end |
#validate_configuration! ⇒ void (protected)
This method returns an undefined value.
Validate SearXNG-specific configuration.
56 57 58 59 60 61 62 63 |
# File 'lib/prescient/tool/searxng.rb', line 56 def validate_configuration! normalized_base_url(@options.fetch(:url)) max_response_bytes result_limit(nil) request_timeout(nil) rescue KeyError raise Prescient::ToolConfigurationError, 'SearXNG requires a url' end |