Class: Langchain::Tool::GoogleSearch

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

Overview

Wrapper around SerpApiā€™s Google Search API

Gem requirements:

gem "google_search_results", "~> 2.0.0"

Usage:

search = Langchain::Tool::GoogleSearch.new(api_key: "YOUR_API_KEY")
search.execute(input: "What is the capital of France?")

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Langchain::ToolDefinition

define_function, function_schemas, tool_name

Methods included from DependencyHelper

#depends_on

Constructor Details

#initialize(api_key:) ⇒ Langchain::Tool::GoogleSearch

Initializes the Google Search tool

Parameters:

  • api_key (String)

    Search API key



30
31
32
33
34
# File 'lib/langchain/tool/google_search.rb', line 30

def initialize(api_key:)
  depends_on "google_search_results"

  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



22
23
24
# File 'lib/langchain/tool/google_search.rb', line 22

def api_key
  @api_key
end

Instance Method Details

#execute(input:) ⇒ String

Executes Google Search and returns the result

Parameters:

  • input (String)

    search query

Returns:

  • (String)

    Answer



40
41
42
43
44
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/langchain/tool/google_search.rb', line 40

def execute(input:)
  Langchain.logger.debug("#{self.class} - Executing \"#{input}\"")

  results = execute_search(input: input)

  answer_box = results[:answer_box_list] ? results[:answer_box_list].first : results[:answer_box]
  if answer_box
    return answer_box[:result] ||
        answer_box[:answer] ||
        answer_box[:snippet] ||
        answer_box[:snippet_highlighted_words] ||
        answer_box.reject { |_k, v| v.is_a?(Hash) || v.is_a?(Array) || v.start_with?("http") }
  elsif (events_results = results[:events_results])
    return events_results.take(10)
  elsif (sports_results = results[:sports_results])
    return sports_results
  elsif (top_stories = results[:top_stories])
    return top_stories
  elsif (news_results = results[:news_results])
    return news_results
  elsif (jobs_results = results.dig(:jobs_results, :jobs))
    return jobs_results
  elsif (shopping_results = results[:shopping_results]) && shopping_results.first.key?(:title)
    return shopping_results.take(3)
  elsif (questions_and_answers = results[:questions_and_answers])
    return questions_and_answers
  elsif (popular_destinations = results.dig(:popular_destinations, :destinations))
    return popular_destinations
  elsif (top_sights = results.dig(:top_sights, :sights))
    return top_sights
  elsif (images_results = results[:images_results]) && images_results.first.key?(:thumbnail)
    return images_results.map { |h| h[:thumbnail] }.take(10)
  end

  snippets = []
  if (knowledge_graph = results[:knowledge_graph])
    snippets << knowledge_graph[:description] if knowledge_graph[:description]

    title = knowledge_graph[:title] || ""
    knowledge_graph.each do |k, v|
      if v.is_a?(String) &&
          k != :title &&
          k != :description &&
          !k.to_s.end_with?("_stick") &&
          !k.to_s.end_with?("_link") &&
          !k.to_s.start_with?("http")
        snippets << "#{title} #{k}: #{v}"
      end
    end
  end

  if (first_organic_result = results.dig(:organic_results, 0))
    if (snippet = first_organic_result[:snippet])
      snippets << snippet
    elsif (snippet_highlighted_words = first_organic_result[:snippet_highlighted_words])
      snippets << snippet_highlighted_words
    elsif (rich_snippet = first_organic_result[:rich_snippet])
      snippets << rich_snippet
    elsif (rich_snippet_table = first_organic_result[:rich_snippet_table])
      snippets << rich_snippet_table
    elsif (link = first_organic_result[:link])
      snippets << link
    end
  end

  if (buying_guide = results[:buying_guide])
    snippets << buying_guide
  end

  if (local_results = results.dig(:local_results, :places))
    snippets << local_results
  end

  return "No good search result found" if snippets.empty?
  snippets
end

#execute_search(input:) ⇒ Hash

Executes Google Search and returns hash_results JSON

Parameters:

  • input (String)

    search query

Returns:

  • (Hash)

    hash_results JSON



123
124
125
126
127
# File 'lib/langchain/tool/google_search.rb', line 123

def execute_search(input:)
  ::GoogleSearch
    .new(q: input, serp_api_key: api_key)
    .get_hash
end