[docs]classBraveSearchWrapper(BaseModel):"""Wrapper around the Brave search engine."""api_key:str"""The API key to use for the Brave search engine."""search_kwargs:dict=Field(default_factory=dict)"""Additional keyword arguments to pass to the search request."""base_url:str="https://api.search.brave.com/res/v1/web/search""""The base URL for the Brave search engine."""
[docs]defrun(self,query:str)->str:"""Query the Brave search engine and return the results as a JSON string. Args: query: The query to search for. Returns: The results as a JSON string. """web_search_results=self._search_request(query=query)final_results=[{"title":item.get("title"),"link":item.get("url"),"snippet":" ".join(filter(None,[item.get("description"),*item.get("extra_snippets",[])])),}foriteminweb_search_results]returnjson.dumps(final_results)
[docs]defdownload_documents(self,query:str)->List[Document]:"""Query the Brave search engine and return the results as a list of Documents. Args: query: The query to search for. Returns: The results as a list of Documents. """results=self._search_request(query)return[Document(page_content=" ".join(filter(None,[item.get("description"),*item.get("extra_snippets",[])])),metadata={"title":item.get("title"),"link":item.get("url")},)foriteminresults]
def_search_request(self,query:str)->List[dict]:headers={"X-Subscription-Token":self.api_key,"Accept":"application/json",}req=requests.PreparedRequest()params={**self.search_kwargs,**{"q":query,"extra_snippets":True}}req.prepare_url(self.base_url,params)ifreq.urlisNone:raiseValueError("prepared url is None, this should not happen")response=requests.get(req.url,headers=headers)ifnotresponse.ok:raiseException(f"HTTP error {response.status_code}")returnresponse.json().get("web",{}).get("results",[])