-
Notifications
You must be signed in to change notification settings - Fork 41
/
wikipedia.py
executable file
·58 lines (47 loc) · 2.13 KB
/
wikipedia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from typing import AnyStr
from gentopia.tools.utils.docstore import DocstoreExplorer, Docstore, Document
from .basetool import *
class Wiki(Docstore):
"""Wrapper around wikipedia API."""
def __init__(self) -> None:
"""Check that wikipedia package is installed."""
try:
import wikipedia # noqa: F401
except ImportError:
raise ValueError(
"Could not import wikipedia python package. "
"Please install it with `pip install wikipedia`."
)
def search(self, search: str) -> Union[str, Document]:
"""Try to search for wiki page.
If page exists, return the page summary, and a PageWithLookups object.
If page does not exist, return similar entries.
"""
import wikipedia
try:
page_content = wikipedia.page(search).content
url = wikipedia.page(search).url
result: Union[str, Document] = Document(
page_content=page_content, metadata={"page": url}
)
except wikipedia.PageError:
result = f"Could not find [{search}]. Similar: {wikipedia.search(search)}"
except wikipedia.DisambiguationError:
result = f"Could not find [{search}]. Similar: {wikipedia.search(search)}"
return result
class Wikipedia(BaseTool):
"""Tool that adds the capability to query the Wikipedia API."""
name = "wikipedia"
description = "Search engine from Wikipedia, retrieving relevant wiki page. Useful when you need to " \
"get holistic knowledge about people, places, companies, historical events, " \
"or other subjects. Input should be a search query."
args_schema: Optional[Type[BaseModel]] = create_model("WikipediaArgs", query=(str, ...))
doc_store: Any = None
def _run(self, query: AnyStr) -> AnyStr:
if not self.doc_store:
self.doc_store = DocstoreExplorer(Wiki())
tool = self.doc_store
evidence = tool.search(query)
return evidence
async def _arun(self, *args: Any, **kwargs: Any) -> Any:
raise NotImplementedError