Class: Boxcars::GoogleSearch

Inherits:
Boxcar
  • Object
show all
Defined in:
lib/boxcars/boxcar/google_search.rb

Overview

A Boxcar that uses the Google SERP API to get answers to questions. It looks through SERP (search engine results page) results to find the answer.

Constant Summary collapse

SERPDESC =

Default description for this boxcar.

"useful for when you need to answer questions about current events. " \
"You should ask targeted questions"
ANSWER_LOCATIONS =
[
  %i[answer_box answer],
  %i[answer_box snippet],
  [:answer_box, :snippet_highlighted_words, 0],
  %i[sports_results game_spotlight],
  %i[knowledge_graph description],
  [:organic_results, 0, :snippet],
  [:organic_results, 0, :snippet_highlighted_words, 0],
  [:organic_results, 0, :title]
].freeze

Constants inherited from Boxcar

Boxcar::SCHEMA_KEY_ALIASES, Boxcar::TYPE_ALIASES

Instance Attribute Summary

Attributes inherited from Boxcar

#description, #name, #parameters, #return_direct

Instance Method Summary collapse

Methods inherited from Boxcar

#apply, assi, #conduct, #conduct_result, hist, #input_keys, #output_keys, #parameters_json_schema, #run, #run_result, #schema, syst, #tool_call_name, #tool_definition, #tool_spec, user, #validate_inputs, #validate_outputs

Constructor Details

#initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil) ⇒ GoogleSearch

Create a boxcar that uses SerpAPI-backed Google search.

Parameters:

  • name (String) (defaults to: "Search")

    The name of the boxcar. Defaults to classname.

  • description (String) (defaults to: SERPDESC)

    A description of the boxcar. Defaults to SERPDESC.

  • serpapi_api_key (String) (defaults to: nil)

    The API key to use for the SerpAPI. Defaults to Boxcars.configuration.serpapi_api_key.



15
16
17
18
19
20
# File 'lib/boxcars/boxcar/google_search.rb', line 15

def initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil)
  super(name:, description:)
  Boxcars::OptionalDependency.require!("google_search_results", feature: "Boxcars::GoogleSearch")
  api_key = Boxcars.configuration.serpapi_api_key(serpapi_api_key:)
  ::GoogleSearch.api_key = api_key
end

Instance Method Details

#call(inputs:) ⇒ Hash

Execute one search request using the normalized Boxcar input contract.

Parameters:

  • inputs (Hash)

    Expected to contain ‘:question` (or `“question”`).

Returns:

  • (Hash)

    ‘{ answer: … }` where answer is a String or Hash.



25
26
27
28
# File 'lib/boxcars/boxcar/google_search.rb', line 25

def call(inputs:)
  question = inputs[:question]
  { answer: search_answer(question) }
end

#get_location(question) ⇒ String

Get the location of an answer from Google using the SerpAPI.

Parameters:

  • question (String)

    The question to ask Google.

Returns:

  • (String)

    The location found.



33
34
35
36
37
38
39
# File 'lib/boxcars/boxcar/google_search.rb', line 33

def get_location(question)
  Boxcars.debug "Question: #{question}", :yellow
  search = ::GoogleSearch.new(q: question, limit: 3)
  answer = search.get_location
  Boxcars.debug "Answer: #{answer}", :yellow, style: :bold
  answer
end