MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / build_tool

Function build_tool

bmtools/tools/wikipedia/api.py:56–150  ·  view source on GitHub ↗
(config)

Source from the content-addressed store, hash-verified

54currentPage = WikiPage()
55
56def build_tool(config) -> Tool:
57 tool = Tool(
58 "Wikipedia Search",
59 "Plugin for wikipedia",
60 name_for_model="Wikipedia",
61 name_for_human="Wikipedia",
62 description_for_model="A tool to search entity, view content and disambiguate entity on Wikipedia.\nCurrent endpoint for each function is simple and you should only use exact entity name as input for search and disambiguate. And the keyword input to lookup api should also be simple like one or two words.\nSome Tips to use the APIs bertter:\n1. When the search api doesn't find the corresponding page, you should search a related entity in the return list.\n2. You can only search one entity name in each action, so, don't concat multiple entity names in one search input.\n3. The lookup api can only be used after search api since it depends on the result page of search.\n4. When search api result in an entity page that is not related, you should disambiguate the searched entity to find other entities with the same name.\n5. Don't over rely one this simple tool, you may figure out the next action based on your own knowledge.",
63 description_for_human="A tool to search entity, view content and disambiguate entity on Wikipedia.\nCurrent endpoint for each function is simple and you should only use exact entity name as input for search and disambiguate. And the keyword input to lookup api should also be simple like one or two words.",
64 logo_url="https://your-app-url.com/.well-known/logo.png",
65 contact_email="hello@contact.com",
66 legal_info_url="hello@legal.com"
67 )
68
69 @tool.get("/search")
70 def search(entity : str, request : Request):
71 '''The input is an exact entity name. The action will search this entity name on Wikipedia and returns the first five sentences if it exists. If not, it will return some related entities to search next.
72 '''
73
74 entity_ = entity.replace(" ", "+")
75 search_url = f"https://en.wikipedia.org/w/index.php?search={entity_}"
76 response_text = requests.get(search_url).text
77 soup = BeautifulSoup(response_text, features="html.parser")
78 result_divs = soup.find_all("div", {"class": "mw-search-result-heading"})
79 if result_divs: # mismatch
80 result_titles = [clean_str(div.get_text().strip()) for div in result_divs]
81 obs = f"Could not find {entity}. Similar: {result_titles[:5]}."
82 else:
83 local_page = [p.get_text().strip() for p in soup.find_all("p") + soup.find_all("ul")]
84 if any("may refer to:" in p for p in local_page):
85 obs = search("[" + entity + "]", request)
86 else:
87 currentPage.reset_page()
88 page = ""
89 for p in local_page:
90 if len(p.split(" ")) > 2:
91 page += clean_str(p)
92 if not p.endswith("\n"):
93 page += "\n"
94 obs = currentPage.get_page_obs(page)
95 return obs
96
97 @tool.get("/lookup")
98 def lookup(keyword : str, request : Request) -> str:
99 '''The input is a keyword. This action will look up in the current passage and return the next several sentences containing the keyword in current passage.
100 '''
101 # lookup_keyword = request.session["lookup_keyword"]
102 # lookup_list = request.session["lookup_list"]
103 # lookup_cnt = request.session["lookup_cnt"]
104 # sentences = request.session["sentences"]
105 lookup_keyword = currentPage.lookup_keyword
106 if lookup_keyword != keyword: # reset lookup
107 currentPage.construct_lookup_list(keyword)
108 lookup_list = currentPage.lookup_list
109 lookup_cnt = currentPage.lookup_cnt
110 sentences = currentPage.sentences
111 if lookup_cnt >= len(lookup_list):
112 obs = "No more results."
113 else:

Callers

nothing calls this directly

Calls 1

ToolClass · 0.50

Tested by

no test coverage detected