File size: 4,142 Bytes
735110c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# from duckduckgo_search import DDGS
# import requests
# from crewai.tools import tool

# @tool("Web Search Tool")
# def web_search_tool(query, max_results=2):
#     """
#     Search DuckDuckGo for a query and return the results.
    
#     Args:
#         query (str): Search query
#         max_results (int): Maximum number of results to return (default: 2)
        
#     Returns:
#         list: List of search results
#     """
#     try:
#         with DDGS() as ddgs:
#             results = list(ddgs.text(query, max_results=max_results))
#         return results
#     except Exception as e:
#         print(f"Error searching DuckDuckGo: {e}")
#         return []
#tools/custom_tool.py
from duckduckgo_search import DDGS
import requests
from crewai.tools import tool
from typing import List, Dict, Any

# @tool("web_search_tool")
# def web_search_tool(query: str) -> str:
#     """
#     Search DuckDuckGo for a query and return the results as formatted text.
    
#     Args:
#         query (str): Search query to find information about tourism, places, or activities
        
#     Returns:
#         str: Formatted search results with titles, descriptions, and links
#     """
#     try:
#         max_results = 3  # Fixed number for consistency
        
#         with DDGS() as ddgs:
#             results = list(ddgs.text(query, max_results=max_results))
        
#         if not results:
#             return f"No search results found for query: {query}"
        
#         # Format results as text for the LLM
#         formatted_results = f"Search results for '{query}':\n\n"
        
#         for i, result in enumerate(results, 1):
#             title = result.get('title', 'No title')
#             body = result.get('body', 'No description')
#             href = result.get('href', 'No link')
            
#             formatted_results += f"{i}. **{title}**\n"
#             formatted_results += f"   Description: {body}\n"
#             formatted_results += f"   Link: {href}\n\n"
        
#         return formatted_results
        
#     except Exception as e:
#         error_msg = f"Error searching DuckDuckGo for '{query}': {str(e)}"
#         print(error_msg)
#         return error_msg

# @tool("location_search_tool")
# def location_search_tool(location: str, activity_type: str = "") -> str:
#     """
#     Search for specific activities or places in a given location.
    
#     Args:
#         location (str): The location to search for (city, country, coordinates)
#         activity_type (str): Type of activity or place (e.g., museums, restaurants, parks)
        
#     Returns:
#         str: Formatted search results specific to the location and activity type
#     """
#     try:
#         # Construct a more specific query
#         if activity_type:
#             query = f"{activity_type} in {location} tourist attractions"
#         else:
#             query = f"things to do tourist attractions in {location}"
        
#         return web_search_tool(query)
        
#     except Exception as e:
#         error_msg = f"Error searching for activities in '{location}': {str(e)}"
#         print(error_msg)
#         return error_msg

@tool("simple_web_search")
def web_search_tool(query: str) -> str:
    """
    Search for specific activities or places in a given location using duckduckgo_search.
    Args:
        query (str): Search query to find information about tourism, places, or activities
        
    Returns:
        str: Search results or error message
    """
    try:
        # Simple implementation
        results = []
        with DDGS() as ddgs:
            for result in ddgs.text(query, max_results=3):
                results.append(result)
        
        if not results:
            return f"No results found for: {query}"
        
        # Simple formatting
        output = f"Results for '{query}':\n"
        for i, r in enumerate(results, 1):
            output += f"{i}. {r.get('title', 'No title')}\n{r.get('body', 'No description')[:200]}...\n\n"
        
        return output
        
    except Exception as e:
        return f"Search failed: {str(e)}"