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

Add google serper API support tools to opengpts. #295

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions backend/app/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
TavilyAnswer,
Wikipedia,
YouSearch,
Serper,
SerperResult,
get_retrieval_tool,
get_retriever,
)
Expand All @@ -50,6 +52,8 @@
DDGSearch,
Arxiv,
YouSearch,
Serper,
SerperResult,
SecFilings,
PressReleases,
PubMed,
Expand Down
44 changes: 44 additions & 0 deletions backend/app/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from langchain_community.tools.tavily_search import (
TavilySearchResults,
)
from langchain_community.tools.google_serper.tool import (
GoogleSerperRun,
GoogleSerperResults,
)
from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper
from langchain_community.utilities.arxiv import ArxivAPIWrapper
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper
Expand All @@ -35,6 +40,8 @@ class DDGInput(BaseModel):
class ArxivInput(BaseModel):
query: str = Field(description="search query to look up")

class GoogleSerperInput(BaseModel):
query: str = Field(description="search query to look up")

class PythonREPLInput(BaseModel):
query: str = Field(description="python command to run")
Expand All @@ -53,6 +60,8 @@ class AvailableTools(str, Enum):
RETRIEVAL = "retrieval"
ARXIV = "arxiv"
YOU_SEARCH = "you_search"
SERPER = "google_serper_run"
SERPER_RESULT = "google_serper_results"
SEC_FILINGS = "sec_filings_kai_ai"
PRESS_RELEASES = "press_releases_kai_ai"
PUBMED = "pubmed"
Expand Down Expand Up @@ -183,6 +192,29 @@ class TavilyAnswer(BaseTool):
const=True,
)

class Serper(BaseTool):
type: AvailableTools = Field(AvailableTools.SERPER, const=True)
name: str = Field("Serper.dev Google Search API", const=True)
description: str = Field(
(
"A low-cost Google Search API."
"Useful for when you need to answer questions about current events."
"Input should be a search query."
),
const=True,
)

class SerperResult(BaseTool):
type: AvailableTools = Field(AvailableTools.SERPER_RESULT, const=True)
name: str = Field("Serper.dev Google Search API (JSON)", const=True)
description: str = Field(
(
"A low-cost Google Search API."
"Useful for when you need to answer questions about current events."
"Input should be a search query. Output is a JSON object of the query results"
),
const=True,
)

class Retrieval(BaseTool):
type: AvailableTools = Field(AvailableTools.RETRIEVAL, const=True)
Expand Down Expand Up @@ -237,6 +269,16 @@ def _get_you_search():
"Searches for documents using You.com",
)

@lru_cache(maxsize=1)
def _get_serper_search():
#os.environ["SERPER_API_KEY"] = "API key from https://serper.dev/api-key"
return GoogleSerperRun(api_wrapper=GoogleSerperAPIWrapper(), args_schema=GoogleSerperInput)

@lru_cache(maxsize=1)
def _get_serper_search_result():
#os.environ["SERPER_API_KEY"] = "API key from https://serper.dev/api-key"
return GoogleSerperResults(api_wrapper=GoogleSerperAPIWrapper(), args_schema=GoogleSerperInput)


@lru_cache(maxsize=1)
def _get_sec_filings():
Expand Down Expand Up @@ -315,6 +357,8 @@ def _get_dalle_tools():
AvailableTools.DDG_SEARCH: _get_duck_duck_go,
AvailableTools.ARXIV: _get_arxiv,
AvailableTools.YOU_SEARCH: _get_you_search,
AvailableTools.SERPER: _get_serper_search,
AvailableTools.SERPER_RESULT: _get_serper_search_result,
AvailableTools.SEC_FILINGS: _get_sec_filings,
AvailableTools.PRESS_RELEASES: _get_press_releases,
AvailableTools.PUBMED: _get_pubmed,
Expand Down