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 66aae1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
30 changes: 28 additions & 2 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,13 +455,38 @@ 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}
for res in googlesearch(args["keyword"], advanced=True)
]
}

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


TOOLS = [
AITool(
name="get_datetime",
msg="getting the current datetime",
description="Get the current date time in ISO format",
handler=lambda _: {"datetime": datetime.today().isoformat()},
),
AITool(
name="search_on_google",
msg="searching {0} on Google",
msg_args=["keyword"],
description="Search the given keyword on Google. Access the result urls for more information",
handler=_search_on_google,
parameters=Schema(
type_=SchemaType.OBJECT,
properties={"keyword": Schema(type_=SchemaType.STRING)},
required=["keyword"],
),
),
AITool(
name="get_webpage_by_url",
msg="getting a webpage from {0}",
Expand Down Expand Up @@ -495,7 +521,7 @@ def _invoke_tool(function_call: FunctionCall):
tool = next((t for t in TOOLS if t.name == function_call.name), None)
if not tool:
return {"error": "Function not found"}
return tool.handler(function_call.args)
return { "name": function_call.name, "content": tool.handler(function_call.args) }


def _get_tool_msg(function_call: FunctionCall):
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 66aae1d

Please sign in to comment.