feat: enhance README with new features and installation instructions; update .gitignore; refactor search tools for site rotation

This commit is contained in:
Matteo Benedetto
2025-04-18 19:50:42 +02:00
parent 91357ce27a
commit 74a9552894
5 changed files with 109 additions and 117 deletions
+49 -11
View File
@@ -2,35 +2,73 @@ import os
from langchain.tools.base import BaseTool
from langchain.callbacks.manager import CallbackManagerForToolRun
import requests
from typing import Optional
from typing import Optional, Dict, List
from langchain_community.tools import DuckDuckGoSearchRun
import random
import time
class MediaInfoSearchTool(BaseTool):
name: str = "duckduckgo_search"
description: str = '''Useful for searching the web using DuckDuckGo for information about \
movies and TV shows, actors and directors. To be used only on imdb.com adding relative keyword imdb to query to filter results.
Input should be a search query, and the tool will return relevant results.'''
movies and TV shows, actors and directors.'''
# Class variable to track previous queries and sites
movie_sites: List[str] = ["imdb.com", "rottentomatoes.com", "metacritic.com", "themoviedb.org", "filmaffinity.com"]
def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
"""Perform a DuckDuckGo search."""
"""Perform a DuckDuckGo search with site rotation queries."""
try:
search_tool = DuckDuckGoSearchRun()
return search_tool.run(query)
result = ""
# Randomly select 3 sites from the movie_sites list
selected_sites = random.sample(self.movie_sites, 2)
for movie_site in selected_sites:
result += f"Searching for '{query}' on {movie_site}...\n"
try:
# Perform the search using DuckDuckGo
result += search_tool.run(f"{query} site:{movie_site}")
except Exception as e:
result += f"Error searching on {movie_site}: {str(e)}\n"
time.sleep(1) # Sleep for 1 second to avoid hitting the API too fast
# Perform the search using DuckDuckGo
result += search_tool.run(f"{query} site:{movie_site}")
result += "\n\n"
print(f"Searching for '{query}' on {movie_site}...\n")
return result
except Exception as e:
return f"Error performing DuckDuckGo search: {str(e)}"
return f"Error searching for '{query}': {str(e)}"
class MoviesAdviceSearchTool(BaseTool):
name: str = "movies_advice_search"
description: str = '''Useful for searching the web using DuckDuckGo for movie recommendations and similar content to a given title or plot.
prefer searching on one (on your preference) known trustworthy sites. add relative keyword (like "reddit" for reddit.com for example) to query to filter results only on that site.
prefer searching on trustworthy sites.
Input should be a search query, and the tool will return relevant results.'''
# Class variable to track recommendation sites
recommendation_sites: List[str] = ["reddit.com/r/moviesuggestions", "tastedive.com", "letterboxd.com", "movielens.org", "flickmetrix.com", "justwatch.com"]
def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
"""Perform a DuckDuckGo search."""
"""Perform a DuckDuckGo search with site rotation queries."""
try:
search_tool = DuckDuckGoSearchRun()
search_results = search_tool.run(query)
return search_results
result = ""
# Randomly select 2 sites from the recommendation_sites list
selected_sites = random.sample(self.recommendation_sites, 2)
for rec_site in selected_sites:
result += f"Searching for '{query}' on {rec_site}...\n"
try:
# Perform the search using DuckDuckGo
result += search_tool.run(f"{query} site:{rec_site}")
except Exception as e:
result += f"Error searching on {rec_site}: {str(e)}\n"
time.sleep(5) # Sleep for 1 second to avoid hitting the API too fast
result += "\n\n"
print(f"Searching for '{query}' on {rec_site}...\n")
return result
except Exception as e:
return f"Error performing DuckDuckGo search: {str(e)}"
return f"Error searching for '{query}': {str(e)}"