Skip to content

Commit

Permalink
feat: search on Google for Gemini
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanssen0 committed Dec 17, 2023
1 parent 17061a6 commit 276af60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
34 changes: 30 additions & 4 deletions GoogleAIModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from readability import Document
from markdownify import markdownify
from googlesearch import search as googlesearch

import BotHandler
import UsersHandler
Expand Down Expand Up @@ -444,7 +445,7 @@ def _get_webpage_by_url(args):

header = requests.head(url, timeout=20, allow_redirects=True)
content_type = header.headers.get("content-type")
if content_type != "text/html":
if not content_type.startswith("text/html"):
return {"error": f"Unsupported content type {content_type}"}

res = requests.get(url, timeout=20, allow_redirects=True)
Expand All @@ -454,6 +455,19 @@ def _get_webpage_by_url(args):
return {"error": "Can not read the url"}


def _search_on_google(args):
try:
return {
"results": [
{"url": res.url, "title": res.title, "description": res.description}
for res in googlesearch(args["keyword"], advanced=True)
]
}

except Exception:
return {"error": "Failed to search the keyword, try again"}


TOOLS = [
AITool(
name="get_datetime",
Expand All @@ -462,10 +476,22 @@ def _get_webpage_by_url(args):
handler=lambda _: {"datetime": datetime.today().isoformat()},
),
AITool(
name="get_webpage_by_url",
msg="getting a webpage from {0}",
name="search_on_google",
msg="searching `{0}` on Google",
msg_args=["keyword"],
description="Search the given keyword on Google",
handler=_search_on_google,
parameters=Schema(
type_=SchemaType.OBJECT,
properties={"keyword": Schema(type_=SchemaType.STRING)},
required=["keyword"],
),
),
AITool(
name="summary_webpage_by_url",
msg="getting summary of {0}",
msg_args=["url"],
description="Get a webpage by url",
description="Get summary of a webpage by url",
handler=_get_webpage_by_url,
parameters=Schema(
type_=SchemaType.OBJECT,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ langdetect>=1.0.9
google-generativeai >= 0.3.1
readability-lxml>=0.8.1
markdownify>=0.11.6
googlesearch-python>=1.2.3

0 comments on commit 276af60

Please sign in to comment.