Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move google search to Websearch.py #1203

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Move google search to Websearch.py
  • Loading branch information
Josh-XT committed Jun 4, 2024
commit c8c9be721c8ac0b06bf73066647a6210e4f79242
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
Loading