Class: Deepsearch::SearchAdapters::TavilyAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/search_adapters/tavily_adapter.rb

Overview

An adapter for the Tavily Search API.

Constant Summary collapse

BASE_URL =
'https://api.tavily.com/search'

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ TavilyAdapter

Returns a new instance of TavilyAdapter.



13
14
15
16
# File 'lib/search_adapters/tavily_adapter.rb', line 13

def initialize(api_key = nil)
  @api_key = api_key || Deepsearch.configuration.tavily_api_key
  validate_api_key!
end

Instance Method Details

#search(query, options = {}) ⇒ Hash

Returns Parsed response from Tavily API.

Parameters:

  • query (String)

    The search query

  • options (Hash) (defaults to: {})

    Additional search options

Options Hash (options):

  • :max_results (Integer)

    Maximum number of results (default: 10)

  • :include_domains (Array<String>)

    Domains to include in search

  • :exclude_domains (Array<String>)

    Domains to exclude from search

  • :include_answer (Boolean)

    Whether to include AI-generated answer (default: true)

  • :include_raw_content (Boolean)

    Whether to include raw content (default: false)

  • :search_depth (String)

    Search depth: ‘basic’ or ‘advanced’ (default: ‘basic’)

Returns:

  • (Hash)

    Parsed response from Tavily API

Raises:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/search_adapters/tavily_adapter.rb', line 28

def search(query, options = {})
  raise ArgumentError, "Query cannot be empty" if query.nil? || query.strip.empty?

  payload = build_payload(query, options)
  response = make_request(payload)
  parse_response(response)
rescue Net::HTTPError, Net::ReadTimeout, Net::OpenTimeout => e
  raise TavilyError, "Network error: #{e.message}"
rescue JSON::ParserError => e
  raise TavilyError, "Invalid JSON response: #{e.message}"
end