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

Function build_tool

bmtools/tools/bing_search/api.py:119–181  ·  view source on GitHub ↗
(config)

Source from the content-addressed store, hash-verified

117data = SessionData()
118
119def build_tool(config) -> Tool:
120 tool = Tool(
121 "Bing_search",
122 "Bing_search",
123 name_for_model="Bing_search",
124 name_for_human="Bing_search",
125 description_for_model="""Perform Search on Bing Search engine.
126Use search_top3(key: str) to get top 3 search results after input the key to search.
127Use load_page_index(idx: int) to load the detailed page of the search result.""",
128 description_for_human="Bing search API for browsing the internet and search for results.",
129 logo_url="https://your-app-url.com/.well-known/logo.png",
130 contact_email="hello@contact.com",
131 legal_info_url="hello@legal.com"
132 )
133
134 if "debug" in config and config["debug"]:
135 bing_api = config["bing_api"]
136 else:
137 bing_api = BingAPI(config["subscription_key"])
138
139 @tool.get("/search_top3")
140 def search_top3(key_words: str) -> str:
141 """Search key words, return top 3 search results.
142 """
143 top3 = search_all(key_words)[:3]
144 output = ""
145 for idx, item in enumerate(top3):
146 output += "page: " + str(idx+1) + "\n"
147 output += "title: " + item['name'] + "\n"
148 output += "summary: " + item['snippet'] + "\n"
149 return output
150
151 def search_all(key_words: str, data: SessionData = data) -> list:
152 """Search key_words, return a list of class SearchResult.
153 Keyword arguments:
154 key_words -- key words want to search
155 """
156 result = bing_api.search(key_words)
157 data.content = []
158 data.content.append(ContentItem(CONTENT_TYPE.SEARCH_RESULT, result))
159 data.curResultChunk = 0
160 return data.content[-1].data["webPages"]["value"]
161
162 @tool.get("/load_page_index")
163 def load_page_index(idx: str) -> str:
164 """Load page detail of the search result indexed as 'idx', and return the content of the page.
165 """
166 idx = int(idx)
167 href, text = load_page(idx-1)
168 if len(text) > 500:
169 return text[:500]
170 else:
171 return text
172
173 def load_page(idx : int, data: SessionData = data):
174 top = data.content[-1].data["webPages"]["value"]
175 ok, content = bing_api.load_page(top[idx]['url'])
176 if ok:

Callers 1

test_bing.pyFile · 0.70

Calls 2

BingAPIClass · 0.85
ToolClass · 0.50

Tested by

no test coverage detected