import os import dotenv from config import Config dotenv.load_dotenv('./.env') from langchain.tools import Tool from langchain_community.utilities import GoogleSerperAPIWrapper # Set up the search tool search = GoogleSerperAPIWrapper(serper_api_key=os.getenv("SERPER_API_KEY")) tool = Tool( name="Web Search", func=search.run, description="Useful for finding real-time information on the web." ) def perform_web_search(query: str): try: if not query or query.strip() == "" or query.strip().lower() == 'none': return ["", []] results = tool.run(query.strip()) if not results: return ["No web results found", []] # Extract snippets and links from search results if 'organic' in results: snippets = "\n".join([f"{res['title']}: {res['snippet']}" for res in results['organic'][:Config.MAX_SEARCH_RESULTS]]) links = [res['link'] for res in results['organic'][:Config.MAX_SEARCH_RESULTS]] return [snippets, links] return [str(results), []] except Exception as e: return [f"Search error: {str(e)}", []]