Tourism_Agent / tools /custom_tool.py
dure-waseem's picture
initial cose
735110c
raw
history blame
4.14 kB
# 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)}"