Enhance Wikipedia tool flexibility
Added options to extract specific info and summarize content from Wikipedia pages within the gptbot's Wikipedia tool. The 'extract' option enables partial retrieval of page data based on a user-defined string, leveraging the bot's existing chat API for extraction. The 'summarize' option allows users to get concise versions of articles, again utilizing the bot's chat capabilities. These additions provide users with more granular control over the information they receive, potentially reducing response clutter and focusing on user-specified interests.
This commit is contained in:
parent
c1986203e8
commit
31f001057a
1 changed files with 32 additions and 15 deletions
|
@ -18,6 +18,15 @@ class Wikipedia(BaseTool):
|
||||||
"description": "The language to search in.",
|
"description": "The language to search in.",
|
||||||
"default": "en",
|
"default": "en",
|
||||||
},
|
},
|
||||||
|
"extract": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "What information to extract from the page. If not provided, the full page will be returned."
|
||||||
|
},
|
||||||
|
"summarize": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "Whether to summarize the page or not.",
|
||||||
|
"default": False,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"required": ["query"],
|
"required": ["query"],
|
||||||
}
|
}
|
||||||
|
@ -28,7 +37,8 @@ class Wikipedia(BaseTool):
|
||||||
raise Exception('No query provided.')
|
raise Exception('No query provided.')
|
||||||
|
|
||||||
language = self.kwargs.get("language", "en")
|
language = self.kwargs.get("language", "en")
|
||||||
extract = self.kwargs.get("extract", False)
|
extract = self.kwargs.get("extract")
|
||||||
|
summarize = self.kwargs.get("summarize", False)
|
||||||
|
|
||||||
args = {
|
args = {
|
||||||
"action": "query",
|
"action": "query",
|
||||||
|
@ -36,11 +46,6 @@ class Wikipedia(BaseTool):
|
||||||
"titles": query,
|
"titles": query,
|
||||||
}
|
}
|
||||||
|
|
||||||
if extract:
|
|
||||||
args["prop"] = "extracts"
|
|
||||||
args["exintro"] = ""
|
|
||||||
|
|
||||||
else:
|
|
||||||
args["prop"] = "revisions"
|
args["prop"] = "revisions"
|
||||||
args["rvprop"] = "content"
|
args["rvprop"] = "content"
|
||||||
|
|
||||||
|
@ -50,13 +55,25 @@ class Wikipedia(BaseTool):
|
||||||
async with session.get(url) as response:
|
async with session.get(url) as response:
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
|
|
||||||
|
try:
|
||||||
pages = data['query']['pages']
|
pages = data['query']['pages']
|
||||||
page = list(pages.values())[0]
|
page = list(pages.values())[0]
|
||||||
if 'extract' in page:
|
content = page['revisions'][0]['*']
|
||||||
return f"**{page['title']} (Extract)**\n{page['extract']}"
|
except KeyError:
|
||||||
elif 'revisions' in page:
|
|
||||||
return f"**{page['title']}**\n{page['revisions'][0]['*']}"
|
|
||||||
else:
|
|
||||||
raise Exception(f'No results for {query} found in Wikipedia.')
|
raise Exception(f'No results for {query} found in Wikipedia.')
|
||||||
|
|
||||||
|
if extract:
|
||||||
|
chat_messages = [{"role": "system", "content": f"Extract the following from the provided content: {extract}"}]
|
||||||
|
chat_messages.append({"role": "user", "content": content})
|
||||||
|
content, _ = await self.bot.chat_api.generate_chat_response(chat_messages, room=self.room, user=self.user, allow_override=False, use_tools=False)
|
||||||
|
|
||||||
|
if summarize:
|
||||||
|
chat_messages = [{"role": "system", "content": "Summarize the following content:"}]
|
||||||
|
chat_messages.append({"role": "user", "content": content})
|
||||||
|
content, _ = await self.bot.chat_api.generate_chat_response(chat_messages, room=self.room, user=self.user, allow_override=False, use_tools=False)
|
||||||
|
|
||||||
|
return f"**Wikipedia: {page['title']}**\n{content}"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise Exception(f'Could not connect to Wikipedia API: {response.status} {response.reason}')
|
raise Exception(f'Could not connect to Wikipedia API: {response.status} {response.reason}')
|
Loading…
Reference in a new issue