File size: 2,928 Bytes
fd18d93
8fe992b
 
13e61d2
 
8fe992b
55ce585
8fe992b
 
 
 
 
 
13e61d2
 
 
 
 
 
 
 
 
8fe992b
 
13e61d2
 
 
 
8fe992b
 
 
 
 
 
 
 
13e61d2
 
 
 
 
 
 
 
 
8fe992b
55ce585
13e61d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55ce585
e80b45c
 
55ce585
e80b45c
55ce585
 
13e61d2
55ce585
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from typing import Any, Optional
from smolagents.tools import Tool
import duckduckgo_search
import random
import time


class DuckDuckGoSearchTool(Tool):
    name = "web_search"
    description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
    inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
    output_type = "string"

    def __init__(
        self,
        max_results=10,
        backends=None,
        retries=2,
        min_delay=0.2,
        max_delay=0.8,
        **kwargs,
    ):
        super().__init__()
        self.max_results = max_results
        self.backends = backends or ["auto", "lite", "html", "api"]
        self.retries = retries
        self.min_delay = min_delay
        self.max_delay = max_delay
        try:
            from duckduckgo_search import DDGS
        except ImportError as e:
            raise ImportError(
                "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
            ) from e
        self.ddgs = DDGS(**kwargs)

    def _format_results(self, results) -> str:
        postprocessed_results = []
        for result in results:
            title = result.get("title") or "Untitled"
            href = result.get("href") or result.get("url") or ""
            body = result.get("body") or result.get("snippet") or ""
            postprocessed_results.append(f"[{title}]({href})\n{body}".strip())
        return "## Search Results\n\n" + "\n\n".join(postprocessed_results)

    def forward(self, query: str) -> str:
        last_error = None
        for backend in self.backends:
            for attempt in range(self.retries + 1):
                try:
                    results = list(
                        self.ddgs.text(query, max_results=self.max_results, backend=backend)
                    )
                except Exception as e:
                    last_error = e
                    # Jittered backoff to reduce rate-limit issues.
                    sleep_for = min(
                        self.max_delay,
                        self.min_delay * (2 ** attempt) + random.random() * 0.1,
                    )
                    time.sleep(sleep_for)
                    continue
                if results:
                    return self._format_results(results)
                # Empty results can happen; try next backend.
                break
        if last_error is not None:
            return (
                "Web search failed. This can happen if outbound network access is blocked or the "
                f"DuckDuckGo API is rate-limiting requests. Error: {last_error}"
            )
        return (
            "No results found. This may indicate network restrictions or a temporary "
            "DuckDuckGo issue. Try a shorter query or retry later."
        )