跳到内容

Duckduckgo

DuckDuckGoSearchToolSpec #

基础:BaseToolSpec

DuckDuckGoSearch 工具规范。

源代码位于 llama-index-integrations/tools/llama-index-tools-duckduckgo/llama_index/tools/duckduckgo/base.py
 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
class DuckDuckGoSearchToolSpec(BaseToolSpec):
    """DuckDuckGoSearch tool spec."""

    spec_functions = ["duckduckgo_instant_search", "duckduckgo_full_search"]

    def __init__(self) -> None:
        if not importlib.util.find_spec("duckduckgo_search"):
            raise ImportError(
                "DuckDuckGoSearchToolSpec requires the duckduckgo_search package to be installed."
            )
        super().__init__()

    def duckduckgo_instant_search(self, query: str) -> List[Dict]:
        """
        Make a query to DuckDuckGo api to receive an instant answer.

        Args:
            query (str): The query to be passed to DuckDuckGo.

        """
        from duckduckgo_search import DDGS

        with DDGS() as ddg:
            return list(ddg.answers(query))

    def duckduckgo_full_search(
        self,
        query: str,
        region: Optional[str] = "wt-wt",
        max_results: Optional[int] = 10,
    ) -> List[Dict]:
        """
        Make a query to DuckDuckGo search to receive a full search results.

        Args:
            query (str): The query to be passed to DuckDuckGo.
            region (Optional[str]): The region to be used for the search in [country-language] convention, ex us-en, uk-en, ru-ru, etc...
            max_results (Optional[int]): The maximum number of results to be returned.

        """
        from duckduckgo_search import DDGS

        params = {
            "keywords": query,
            "region": region,
            "max_results": max_results,
        }

        with DDGS() as ddg:
            return list(ddg.text(**params))
duckduckgo_instant_search(query: str) -> List[Dict]

向 DuckDuckGo API 发起查询以获取即时答案。

参数

名称 类型 描述 默认值
query str

要传递给 DuckDuckGo 的查询。

必需
源代码位于 llama-index-integrations/tools/llama-index-tools-duckduckgo/llama_index/tools/duckduckgo/base.py
18
19
20
21
22
23
24
25
26
27
28
29
def duckduckgo_instant_search(self, query: str) -> List[Dict]:
    """
    Make a query to DuckDuckGo api to receive an instant answer.

    Args:
        query (str): The query to be passed to DuckDuckGo.

    """
    from duckduckgo_search import DDGS

    with DDGS() as ddg:
        return list(ddg.answers(query))
duckduckgo_full_search(query: str, region: Optional[str] = 'wt-wt', max_results: Optional[int] = 10) -> List[Dict]

向 DuckDuckGo 搜索发起查询以获取完整搜索结果。

参数

名称 类型 描述 默认值
query str

要传递给 DuckDuckGo 的查询。

必需
region 可选[str]

用于搜索的区域,遵循 [国家-语言] 惯例,例如 us-en, uk-en, ru-ru 等...

'wt-wt'
max_results 可选[int]

要返回的最大结果数。

10
源代码位于 llama-index-integrations/tools/llama-index-tools-duckduckgo/llama_index/tools/duckduckgo/base.py
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
def duckduckgo_full_search(
    self,
    query: str,
    region: Optional[str] = "wt-wt",
    max_results: Optional[int] = 10,
) -> List[Dict]:
    """
    Make a query to DuckDuckGo search to receive a full search results.

    Args:
        query (str): The query to be passed to DuckDuckGo.
        region (Optional[str]): The region to be used for the search in [country-language] convention, ex us-en, uk-en, ru-ru, etc...
        max_results (Optional[int]): The maximum number of results to be returned.

    """
    from duckduckgo_search import DDGS

    params = {
        "keywords": query,
        "region": region,
        "max_results": max_results,
    }

    with DDGS() as ddg:
        return list(ddg.text(**params))