Class: Boxcars::GoogleSearch
- 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 =
the description of this boxcar
"useful for when you need to answer questions about current events." \ "You should ask targeted questions"
Instance Attribute Summary
Attributes inherited from Boxcar
#description, #name, #parameters, #return_direct
Instance Method Summary collapse
-
#get_location(question) ⇒ String
Get the location of an answer from Google using the SerpAPI.
-
#initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil) ⇒ GoogleSearch
constructor
implements a boxcar that uses the Google SerpAPI to get answers to questions.
-
#run(question) ⇒ String
Get an answer from Google using the SerpAPI.
Methods inherited from Boxcar
#apply, assi, #call, #conduct, hist, #input_keys, #load, #output_keys, #save, #schema, syst, user, #validate_inputs, #validate_outputs
Constructor Details
#initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil) ⇒ GoogleSearch
implements a boxcar that uses the Google SerpAPI to get answers to questions.
16 17 18 19 20 |
# File 'lib/boxcars/boxcar/google_search.rb', line 16 def initialize(name: "Search", description: SERPDESC, serpapi_api_key: nil) super(name: name, description: description) api_key = Boxcars.configuration.serpapi_api_key(serpapi_api_key: serpapi_api_key) ::GoogleSearch.api_key = api_key end |
Instance Method Details
#get_location(question) ⇒ String
Get the location of an answer from Google using the SerpAPI.
36 37 38 39 40 41 42 |
# File 'lib/boxcars/boxcar/google_search.rb', line 36 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 |
#run(question) ⇒ String
Get an answer from Google using the SerpAPI.
25 26 27 28 29 30 31 |
# File 'lib/boxcars/boxcar/google_search.rb', line 25 def run(question) search = ::GoogleSearch.new(q: question) rv = find_answer(search.get_hash) Boxcars.info "Question: #{question}" Boxcars.info "Answer: #{rv}" rv end |