Skip to content

Commit

Permalink
Move google search to Websearch.py (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-XT authored Jun 4, 2024
1 parent c6e6fba commit 5147f05
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
51 changes: 50 additions & 1 deletion agixt/Websearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from readers.github import GithubReader
from datetime import datetime
from Memories import extract_keywords
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


logging.basicConfig(
Expand Down Expand Up @@ -433,6 +435,29 @@ async def ddg_search(self, query: str, proxy=None) -> List[str]:
await browser.close()
return results

async def google_search(
self,
query: str,
depth: int = 5,
google_api_key: str = "",
google_search_engine_id: str = "",
) -> List[str]:
if google_api_key == "" or google_search_engine_id == "":
return []
try:
service = build("customsearch", "v1", developerKey=google_api_key)
result = (
service.cse()
.list(q=query, cx=google_search_engine_id, num=depth)
.execute()
)
search_results = result.get("items", [])
search_results_links = [item["link"] for item in search_results]
except Exception as e:
logging.error(f"Google Search Error: {e}")
search_results_links = []
return search_results_links

async def update_search_provider(self):
# SearXNG - List of these at https://searx.space/
# Check if the instances-todays date.json file exists
Expand Down Expand Up @@ -530,7 +555,31 @@ async def websearch_agent(
search_string = " ".join(keywords)
# add month and year to the end of the search string
search_string += f" {datetime.now().strftime('%B %Y')}"
links = await self.ddg_search(query=search_string)
google_api_key = (
self.agent_settings["GOOGLE_API_KEY"]
if "GOOGLE_API_KEY" in self.agent_settings
else ""
)
google_search_engine_id = (
self.agent_settings["GOOGLE_SEARCH_ENGINE_ID"]
if "GOOGLE_SEARCH_ENGINE_ID" in self.agent_settings
else ""
)
links = []
if (
google_api_key != ""
and google_search_engine_id != ""
and google_api_key is not None
and google_search_engine_id is not None
):
links = await self.google_search(
query=search_string,
depth=websearch_depth,
google_api_key=google_api_key,
google_search_engine_id=google_search_engine_id,
)
if links == [] or links is None:
links = await self.ddg_search(query=search_string)
if links == [] or links is None:
links = []
content, links = await self.web_search(query=search_string)
Expand Down
4 changes: 4 additions & 0 deletions agixt/extensions/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def __init__(
GOOGLE_CLIENT_ID: str = "",
GOOGLE_CLIENT_SECRET: str = "",
GOOGLE_REFRESH_TOKEN: str = "",
GOOGLE_API_KEY: str = "",
GOOGLE_SEARCH_ENGINE_ID: str = "",
**kwargs,
):
self.GOOGLE_CLIENT_ID = GOOGLE_CLIENT_ID
self.GOOGLE_CLIENT_SECRET = GOOGLE_CLIENT_SECRET
self.GOOGLE_REFRESH_TOKEN = GOOGLE_REFRESH_TOKEN
self.GOOGLE_API_KEY = GOOGLE_API_KEY
self.GOOGLE_SEARCH_ENGINE_ID = GOOGLE_SEARCH_ENGINE_ID
self.attachments_dir = "./WORKSPACE/email_attachments/"
os.makedirs(self.attachments_dir, exist_ok=True)
self.commands = {
Expand Down

0 comments on commit 5147f05

Please sign in to comment.