from langchain_tavily import TavilySearch from langchain.tools import tool from datetime import date import platform import json import requests import wikipedia from bs4 import BeautifulSoup import os from dotenv import load_dotenv load_dotenv() tool_tavily = TavilySearch( max_results=5, topic="general" ) @tool def time_date(): """ This give today date in format yyyy-mm-dd """ today = date.today() return today @tool def calculator(expression: str) -> str: """Evaluate mathematical expressions""" try: return str(eval(expression)) except Exception as e: return str(e) @tool def python_exec(code: str) -> str: """Execute Python code""" try: local_vars = {} exec(code, {}, local_vars) return str(local_vars) except Exception as e: return str(e) @tool def get_weather(city: str) -> str: """Get current weather for a city""" url = f"https://wttr.in/{city}?format=3" return requests.get(url).text @tool def wikipedia_search(query: str) -> str: """Search Wikipedia""" try: return wikipedia.summary(query, sentences=3) except: return "No result found" @tool def scrape_website(url: str) -> str: """Extract text from a webpage""" res = requests.get(url) soup = BeautifulSoup(res.text, "html.parser") return soup.get_text()[:2000] @tool def read_file(path: str) -> str: """Read local file content""" try: with open(path, "r") as f: return f.read() except Exception as e: return str(e) @tool def format_json(data: str) -> str: """Format JSON string""" try: parsed = json.loads(data) return json.dumps(parsed, indent=2) except Exception as e: return str(e) @tool def generate_sql(query: str) -> str: """Convert natural language to SQL""" return f"-- SQL Query for: {query}" @tool def system_info() -> str: """Get system information""" return platform.platform() @tool def save_user_preference(text: str) -> str: """Save user preference or important info""" # later connect to store.put() return f"Saved memory: {text}" @tool def list_folders(path: str = ".") -> str: """List all folders in the given directory path""" try: items = os.listdir(path) folders = [f for f in items if os.path.isdir(os.path.join(path, f))] if not folders: return "No folders found." return "\n".join(folders) except Exception as e: return f"Error: {str(e)}"