Skip to content

Commit

Permalink
update web search in order to allow run time verify tokens (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhangpurdue authored Jul 4, 2024
1 parent f2d878f commit 82c62d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
25 changes: 10 additions & 15 deletions modelscope_agent/tools/web_search/search_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,13 @@ def to_dict(cls):
return {member.name: member.value for member in cls}


def get_websearcher_cls():

def get_env(authentication_key: str):
env = os.environ
return env.get(authentication_key, None)

cls_dict = {}
if get_env(AuthenticationKey.bing.value):
from .searcher.bing import BingWebSearcher
cls_dict[AuthenticationKey.bing.name] = BingWebSearcher
if get_env(AuthenticationKey.kuake.value):
from .searcher.kuake import KuakeWebSearcher
cls_dict[AuthenticationKey.kuake.name] = KuakeWebSearcher

return cls_dict
def get_websearcher_cls(searcher: str = None):

if searcher:
if AuthenticationKey.bing.name == searcher:
from .searcher.bing import BingWebSearcher
return BingWebSearcher
elif AuthenticationKey.kuake.name == searcher:
from .searcher.kuake import KuakeWebSearcher
return KuakeWebSearcher
return None
23 changes: 6 additions & 17 deletions modelscope_agent/tools/web_search/web_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,19 @@ class WebSearch(BaseTool):

def __init__(self, cfg={}):
super().__init__(cfg)
available_searchers = get_websearcher_cls()
all_searchers = AuthenticationKey.to_dict()
if not len(available_searchers):
raise ValueError(
f'At least one of web search api token should be set: {all_searchers}'
)

searcher = self.cfg.get('searcher', None)

if not searcher:
available_searchers = list(available_searchers.values())
if len(available_searchers) == 0:
raise ValueError(
f'At least one of web search api token should be set: {all_searchers}'
)
self.searcher = available_searchers[0](**self.cfg)
# set default as searcher
from .searcher.bing import BingWebSearcher
self.searcher = BingWebSearcher(**self.cfg)
else:
if isinstance(searcher,
str) and len(searcher) and all_searchers.get(
searcher, None):
cls = available_searchers.get(searcher, None)
if isinstance(searcher, str) and len(searcher):
cls = get_websearcher_cls(searcher)
if not cls:
raise ValueError(
f'The searcher {searcher}\'s token is not set: {all_searchers.get(searcher, None)}'
f'The searcher {searcher}\'s is not a valid name, please double check.'
)
self.searcher = cls(**cfg)
else:
Expand Down

0 comments on commit 82c62d8

Please sign in to comment.