File size: 2,590 Bytes
45f00f2
 
 
 
 
 
 
 
 
fbf7ee2
45f00f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf7ee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
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)}"