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 =
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
-
#call(inputs:) ⇒ Hash
Execute one search request using the normalized Boxcar input contract.
-
#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
Create a boxcar that uses SerpAPI-backed Google search.
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.
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.
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.
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 |