~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
feat: Add Google Programmable Search Engine implementation
hashedviking338d3107ce4e
diff --git a/web_search_engines/engines/search_engine_google_pse.py b/web_search_engines/engines/search_engine_google_pse.pynew file mode 100644index 0000000000..e442f341a7--- /dev/null+++ b/web_search_engines/engines/search_engine_google_pse.py@@ -0,0 +1,197 @@+from typing import Dict, List, Any, Optional+import os+import requests+from urllib.parse import quote_plus+from langchain_core.language_models import BaseLLM++from web_search_engines.search_engine_base import BaseSearchEngine+++class GooglePSESearchEngine(BaseSearchEngine):+ """Google Programmable Search Engine implementation"""++ def __init__(self,+ max_results: int = 10,+ region: str = "us",+ safe_search: bool = True,+ search_language: str = "English",+ api_key: Optional[str] = None,+ search_engine_id: Optional[str] = None,+ llm: Optional[BaseLLM] = None,+ include_full_content: bool = False,+ max_filtered_results: Optional[int] = None,+ **kwargs):+ """+ Initialize the Google Programmable Search Engine.++ Args:+ max_results: Maximum number of search results+ region: Region code for search results+ safe_search: Whether to enable safe search+ search_language: Language for search results+ api_key: Google API key (can also be set in GOOGLE_PSE_API_KEY env)+ search_engine_id: Google CSE ID (can also be set in GOOGLE_PSE_ENGINE_ID env)+ llm: Language model for relevance filtering+ include_full_content: Whether to include full webpage content in results+ max_filtered_results: Maximum number of results to keep after filtering+ **kwargs: Additional parameters (ignored but accepted for compatibility)+ """+ # Initialize the BaseSearchEngine with the LLM and max_filtered_results+ super().__init__(llm=llm, max_filtered_results=max_filtered_results)++ self.max_results = max_results+ self.include_full_content = include_full_content++ # Language code mapping+ language_code_mapping = {+ "english": "en",+ "spanish": "es",+ "french": "fr",+ "german": "de",+ "italian": "it",+ "japanese": "ja",+ "korean": "ko",+ "portuguese": "pt",+ "russian": "ru",+ "chinese": "zh-CN"+ }++ # Get language code+ search_language = search_language.lower()+ self.language = language_code_mapping.get(search_language, "en")++ # Safe search setting+ self.safe = "active" if safe_search else "off"++ # Region/Country setting+ self.region = region++ # API key and Search Engine ID+ self.api_key = api_key or os.getenv("GOOGLE_PSE_API_KEY")+ self.search_engine_id = search_engine_id or os.getenv("GOOGLE_PSE_ENGINE_ID")++ if not self.api_key:+ raise ValueError("Google API key is required. Set it in the GOOGLE_PSE_API_KEY environment variable.")+ if not self.search_engine_id:+ raise ValueError("Google Search Engine ID is required. Set it in the GOOGLE_PSE_ENGINE_ID environment variable.")++ # Validate connection and credentials+ self._validate_connection()++ def _validate_connection(self):+ """Test the connection to ensure API key and Search Engine ID are valid"""+ try:+ # Make a minimal test query+ response = self._make_request("test")++ # Check if we got a valid response+ if response.get("error"):+ error_msg = response["error"].get("message", "Unknown error")+ raise ValueError(f"Google PSE API error: {error_msg}")++ # If we get here, the connection is valid+ return True++ except Exception as e:+ # Log the error and re-raise+ print(f"Error validating Google PSE connection: {str(e)}")+ raise++ def _make_request(self, query: str, start_index: int = 1) -> Dict:+ """Make a request to the Google PSE API"""+ # Base URL for the API+ url = "https://www.googleapis.com/customsearch/v1"++ # Parameters for the request+ params = {+ "key": self.api_key,+ "cx": self.search_engine_id,+ "q": query,+ "num": min(10, self.max_results), # Max 10 per request+ "start": start_index,+ "safe": self.safe,+ "lr": f"lang_{self.language}",+ "gl": self.region+ }++ # Make the request+ response = requests.get(url, params=params)++ # Check for HTTP errors+ response.raise_for_status()++ # Return the JSON response+ return response.json()++ def _get_previews(self, query: str) -> List[Dict[str, Any]]:+ """Get search result previews/snippets"""+ results = []++ # Google PSE API returns a maximum of 10 results per request+ # We may need to make multiple requests to get the desired number+ start_index = 1+ total_results = 0++ while total_results < self.max_results:+ try:+ response = self._make_request(query, start_index)++ # Break if no items+ if "items" not in response:+ break++ items = response.get("items", [])++ # Process each result+ for item in items:+ title = item.get("title", "")+ snippet = item.get("snippet", "")+ url = item.get("link", "")++ # Skip results without URL+ if not url:+ continue++ results.append({+ "title": title,+ "snippet": snippet,+ "url": url,+ "source": "Google Programmable Search"+ })++ total_results += 1+ if total_results >= self.max_results:+ break++ # Check if there are more results+ if not items or total_results >= self.max_results:+ break++ # Update start index for next request+ start_index += len(items)++ except Exception as e:+ print(f"Error getting search results: {str(e)}")+ break++ return results++ def _get_full_content(self, relevant_items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:+ """Get full content for search results"""+ # Use the BaseSearchEngine implementation+ return super()._get_full_content(relevant_items)++ def run(self, query: str) -> List[Dict[str, Any]]:+ """Run the search engine to get results for a query"""+ # Get search result previews/snippets+ search_results = self._get_previews(query)++ # Filter for relevance if we have an LLM and max_filtered_results+ if self.llm and self.max_filtered_results:+ search_results = self._filter_for_relevance(query, search_results)++ # Get full content if needed+ if self.include_full_content:+ search_results = self._get_full_content(search_results)++ return search_results\ No newline at end of file