File size: 1,830 Bytes
81d00fe
401799d
 
81d00fe
 
 
401799d
81d00fe
 
 
401799d
 
 
81d00fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401799d
81d00fe
 
 
 
 
 
401799d
81d00fe
 
 
 
 
 
401799d
81d00fe
 
401799d
81d00fe
 
401799d
81d00fe
 
 
 
 
 
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
import logging

from smolagents import DuckDuckGoSearchTool, Tool, WikipediaSearchTool

logger = logging.getLogger(__name__)


class GeneralSearchTool(Tool):
    name = "search"
    description = """Performs a general web search using both DuckDuckGo and Wikipedia, then returns the combined search results."""
    inputs = {
        "query": {"type": "string", "description": "The search query to perform."}
    }
    output_type = "string"

    def __init__(self, max_results=10, **kwargs):
        super().__init__()
        self.max_results = max_results
        self.ddg_tool = DuckDuckGoSearchTool()
        self.wiki_tool = WikipediaSearchTool()

    def forward(self, query: str) -> str:
        # Get DuckDuckGo results
        try:
            ddg_results = self.ddg_tool.forward(query)
        except Exception as e:
            ddg_results = "No DuckDuckGo results found."
            logger.warning(f"DuckDuckGo search failed: {str(e)}")

        # Get Wikipedia results
        try:
            wiki_results = self.wiki_tool.forward(query)
        except Exception as e:
            wiki_results = "No Wikipedia results found."
            logger.warning(f"Wikipedia search failed: {str(e)}")

        # Combine and format results
        output = []
        if ddg_results and ddg_results != "No DuckDuckGo results found.":
            output.append("## DuckDuckGo Search Results\n\n" + ddg_results)
        if wiki_results and wiki_results != "No Wikipedia results found.":
            output.append("## Wikipedia Results\n\n" + wiki_results)

        if not output:
            raise Exception("No results found! Try a less restrictive/shorter query.")

        return "\n\n---\n\n".join(output)


# Export all tools
tools = [
    # DuckDuckGoSearchTool(),
    GeneralSearchTool(),
    # WikipediaSearchTool(),
]