跳至内容

维基百科

WikipediaToolSpec #

基础: BaseToolSpec

指定了两个用于查询维基百科信息的工具。

源代码位于 llama-index-integrations/tools/llama-index-tools-wikipedia/llama_index/tools/wikipedia/base.py
 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
class WikipediaToolSpec(BaseToolSpec):
    """
    Specifies two tools for querying information from Wikipedia.
    """

    spec_functions = ["load_data", "search_data"]

    def load_data(
        self, page: str, lang: str = "en", **load_kwargs: Dict[str, Any]
    ) -> str:
        """
        Retrieve a Wikipedia page. Useful for learning about a particular concept that isn't private information.

        Args:
            page (str): Title of the page to read.
            lang (str): Language of Wikipedia to read. (default: English)

        """
        import wikipedia

        wikipedia.set_lang(lang)
        try:
            wikipedia_page = wikipedia.page(page, **load_kwargs, auto_suggest=False)
        except wikipedia.PageError:
            return "Unable to load page. Try searching instead."
        return wikipedia_page.content

    def search_data(self, query: str, lang: str = "en") -> str:
        """
        Search Wikipedia for a page related to the given query.
        Use this tool when `load_data` returns no results.

        Args:
            query (str): the string to search for

        """
        import wikipedia

        pages = wikipedia.search(query)
        if len(pages) == 0:
            return "No search results."
        return self.load_data(pages[0], lang)

load_data #

load_data(page: str, lang: str = 'en', **load_kwargs: Dict[str, Any]) -> str

检索维基百科页面。可用于了解非私人信息的特定概念。

参数

名称 类型 描述 默认值
page str

要读取的页面标题。

必需
lang str

要读取的维基百科语言。(默认:英语)

'en'
源代码位于 llama-index-integrations/tools/llama-index-tools-wikipedia/llama_index/tools/wikipedia/base.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def load_data(
    self, page: str, lang: str = "en", **load_kwargs: Dict[str, Any]
) -> str:
    """
    Retrieve a Wikipedia page. Useful for learning about a particular concept that isn't private information.

    Args:
        page (str): Title of the page to read.
        lang (str): Language of Wikipedia to read. (default: English)

    """
    import wikipedia

    wikipedia.set_lang(lang)
    try:
        wikipedia_page = wikipedia.page(page, **load_kwargs, auto_suggest=False)
    except wikipedia.PageError:
        return "Unable to load page. Try searching instead."
    return wikipedia_page.content

search_data #

search_data(query: str, lang: str = 'en') -> str

在维基百科中搜索与给定查询相关的页面。当 load_data 没有返回结果时使用此工具。

参数

名称 类型 描述 默认值
query str

要搜索的字符串

必需
源代码位于 llama-index-integrations/tools/llama-index-tools-wikipedia/llama_index/tools/wikipedia/base.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def search_data(self, query: str, lang: str = "en") -> str:
    """
    Search Wikipedia for a page related to the given query.
    Use this tool when `load_data` returns no results.

    Args:
        query (str): the string to search for

    """
    import wikipedia

    pages = wikipedia.search(query)
    if len(pages) == 0:
        return "No search results."
    return self.load_data(pages[0], lang)