kotaemon/knowledgehub/pipelines/tools/google.py
Tuan Anh Nguyen Dang (Tadashi_Cin) f9fc02a32a [AUR-363, AUR-433, AUR-434] Add Base Tool interface with Wikipedia/Google tools (#30)
* add base Tool

* minor update test_tool

* update test dependency

* update test dependency

* Fix namespace conflict

* update test

---------

Co-authored-by: trducng <trungduc1992@gmail.com>
2023-09-29 10:18:49 +07:00

36 lines
1.0 KiB
Python

from typing import AnyStr, Optional, Type
from pydantic import BaseModel, Field
from .base import BaseTool
class GoogleSearchArgs(BaseModel):
query: str = Field(..., description="a search query")
class GoogleSearchTool(BaseTool):
name = "google_search"
description = (
"A search engine retrieving top search results as snippets from Google. "
"Input should be a search query."
)
args_schema: Optional[Type[BaseModel]] = GoogleSearchArgs
def _run_tool(self, query: AnyStr) -> str:
try:
from googlesearch import search
except ImportError:
raise ImportError(
"install googlesearch using `pip3 install googlesearch-python` to "
"use this tool"
)
output = ""
search_results = search(query, advanced=True)
if search_results:
output = "\n".join(
"{} {}".format(item.title, item.description) for item in search_results
)
return output