Class: Langchain::Tool::Tavily

Inherits:
Object
  • Object
show all
Extended by:
Langchain::ToolDefinition
Defined in:
lib/langchain/tool/tavily.rb

Overview

Tavily Search is a robust search API tailored specifically for LLM Agents. It seamlessly integrates with diverse data sources to ensure a superior, relevant search experience.

Usage:

tavily = Langchain::Tool::Tavily.new(api_key: ENV["TAVILY_API_KEY"])

Instance Method Summary collapse

Methods included from Langchain::ToolDefinition

define_function, function_schemas, tool_name

Constructor Details

#initialize(api_key:) ⇒ Tavily

Returns a new instance of Tavily.



29
30
31
# File 'lib/langchain/tool/tavily.rb', line 29

def initialize(api_key:)
  @api_key = api_key
end

Instance Method Details

#search(query:, search_depth: "basic", include_images: false, include_answer: false, include_raw_content: false, max_results: 5, include_domains: [], exclude_domains: []) ⇒ String

Search for data based on a query.

Parameters:

  • query (String)

    The search query string.

  • search_depth (String) (defaults to: "basic")

    The depth of the search. It can be basic or advanced. Default is basic for quick results and advanced for indepth high quality results but longer response time. Advanced calls equals 2 requests.

  • include_images (Boolean) (defaults to: false)

    Include a list of query related images in the response. Default is False.

  • include_answer (Boolean) (defaults to: false)

    Include answers in the search results. Default is False.

  • include_raw_content (Boolean) (defaults to: false)

    Include raw content in the search results. Default is False.

  • max_results (Integer) (defaults to: 5)

    The number of maximum search results to return. Default is 5.

  • include_domains (Array<String>) (defaults to: [])

    A list of domains to specifically include in the search results. Default is None, which includes all domains.

  • exclude_domains (Array<String>) (defaults to: [])

    A list of domains to specifically exclude from the search results. Default is None, which doesn’t exclude any domains.

Returns:

  • (String)

    The search results in JSON format.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/langchain/tool/tavily.rb', line 45

def search(
  query:,
  search_depth: "basic",
  include_images: false,
  include_answer: false,
  include_raw_content: false,
  max_results: 5,
  include_domains: [],
  exclude_domains: []
)
  uri = URI("https://api.tavily.com/search")
  request = Net::HTTP::Post.new(uri)
  request.content_type = "application/json"
  request.body = {
    api_key: @api_key,
    query: query,
    search_depth: search_depth,
    include_images: include_images,
    include_answer: include_answer,
    include_raw_content: include_raw_content,
    max_results: max_results,
    include_domains: include_domains,
    exclude_domains: exclude_domains
  }.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.request(request)
  end
  response.body
end