Skip to content

mirascope.tools.web._duckduckgo

DuckDuckGoSearchConfig

Bases: _ConfigurableToolConfig

Configuration for DuckDuckGo search

DuckDuckGoSearch

Bases: _BaseDuckDuckGoSearch

Tool for performing web searches using DuckDuckGo.

Takes search queries and returns relevant search results(Title, URL, Snippet).

call

call() -> str

Perform a web search using DuckDuckGo and return formatted results.

Returns:

Name Type Description
str str

Formatted search results if successful, error message if search fails

Source code in mirascope/tools/web/_duckduckgo.py
def call(self) -> str:
    """Perform a web search using DuckDuckGo and return formatted results.

    Returns:
        str: Formatted search results if successful, error message if search fails
    """

    try:
        all_results = []
        for query in self.queries:
            results = DDGS(proxies=None).text(
                query, max_results=self._get_config().max_results_per_query
            )

            all_results.extend(
                {
                    "title": result["title"],
                    "link": result["href"],
                    "snippet": result["body"],
                }
                for result in results
            )
        return "\n\n".join(
            f"Title: {r['title']}\nURL: {r['link']}\nSnippet: {r['snippet']}"
            for r in all_results
        )

    except Exception as e:
        return f"{type(e)}: Failed to search the web for text"

AsyncDuckDuckGoSearch

Bases: _BaseDuckDuckGoSearch

Tool for performing web searches using DuckDuckGo.

Takes search queries and returns relevant search results(Title, URL, Snippet).

call async

call() -> str

Perform an asynchronous web search using DuckDuckGo and return formatted results.

Returns:

Name Type Description
str str

Formatted search results if successful, error message if search fails

Source code in mirascope/tools/web/_duckduckgo.py
async def call(self) -> str:
    """Perform an asynchronous web search using DuckDuckGo and return formatted results.

    Returns:
        str: Formatted search results if successful, error message if search fails
    """

    try:
        all_results = []
        for query in self.queries:
            results = await AsyncDDGS(proxies=None).atext(
                query, max_results=self._get_config().max_results_per_query
            )

            all_results.extend(
                {
                    "title": result["title"],
                    "link": result["href"],
                    "snippet": result["body"],
                }
                for result in results
            )
        return "\n\n".join(
            f"Title: {r['title']}\nURL: {r['link']}\nSnippet: {r['snippet']}"
            for r in all_results
        )

    except Exception as e:
        return f"{type(e)}: Failed to search the web for text"