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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import List, Optional, Tuple
|
| 3 |
+
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from langchain.agents import Tool
|
| 6 |
+
from langchain_community.agent_toolkits import (
|
| 7 |
+
FileManagementToolkit,
|
| 8 |
+
PlayWrightBrowserToolkit,
|
| 9 |
+
)
|
| 10 |
+
from langchain_community.tools.arxiv.tool import ArxivQueryRun
|
| 11 |
+
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
|
| 12 |
+
from langchain_community.utilities import ArxivAPIWrapper, GoogleSerperAPIWrapper
|
| 13 |
+
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
| 14 |
+
from langchain_experimental.tools import PythonREPLTool
|
| 15 |
+
from playwright.async_api import async_playwright
|
| 16 |
+
from sympy import SympifyError, sympify
|
| 17 |
+
|
| 18 |
+
# Load environment variables
|
| 19 |
+
load_dotenv(override=True)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Custom math tool
|
| 23 |
+
def safe_math_calculator(expression: str) -> str:
|
| 24 |
+
"""Safely evaluate mathematical expressions using sympy."""
|
| 25 |
+
try:
|
| 26 |
+
result = sympify(expression).evalf()
|
| 27 |
+
return str(result)
|
| 28 |
+
except (SympifyError, Exception) as e:
|
| 29 |
+
return f"Error: Unable to evaluate expression '{expression}'. {str(e)}"
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def create_search_tool() -> Optional[Tool]:
|
| 33 |
+
"""Create a web search tool"""
|
| 34 |
+
try:
|
| 35 |
+
serper = GoogleSerperAPIWrapper()
|
| 36 |
+
return Tool(
|
| 37 |
+
name="search",
|
| 38 |
+
func=serper.run,
|
| 39 |
+
description="Use this tool when you want to get the results of an online web search",
|
| 40 |
+
)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Warning: Could not initialize search tool: {e}")
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def create_math_tool() -> List[Tool]:
|
| 47 |
+
"""Create mathematical calculation tools."""
|
| 48 |
+
return [
|
| 49 |
+
Tool(
|
| 50 |
+
name="calculator",
|
| 51 |
+
func=safe_math_calculator,
|
| 52 |
+
description="Use this tool when you want to do math, provide the expression as a string",
|
| 53 |
+
)
|
| 54 |
+
]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def create_file_tools(root_dir: str = "sandbox") -> List[Tool]:
|
| 58 |
+
"""Create file management tools"""
|
| 59 |
+
try:
|
| 60 |
+
# Ensure the directory exists
|
| 61 |
+
os.makedirs(root_dir, exist_ok=True)
|
| 62 |
+
toolkit = FileManagementToolkit(root_dir=root_dir)
|
| 63 |
+
return toolkit.get_tools()
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"Warning: Could not initialize file tools: {e}")
|
| 66 |
+
return []
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def create_wikipedia_tool() -> Optional[Tool]:
|
| 70 |
+
"""Create Wikipedia search tool"""
|
| 71 |
+
try:
|
| 72 |
+
wikipedia = WikipediaAPIWrapper()
|
| 73 |
+
return WikipediaQueryRun(api_wrapper=wikipedia)
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"Warning: Could not initialize Wikipedia tool: {e}")
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def create_arxiv_tool() -> Optional[Tool]:
|
| 80 |
+
"""Create ArXiv search tool"""
|
| 81 |
+
try:
|
| 82 |
+
return ArxivQueryRun(api_wrapper=ArxivAPIWrapper())
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"Warning: Could not initialize ArXiv tool: {e}")
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def create_python_repl_tool() -> Optional[Tool]:
|
| 89 |
+
"""Create Python REPL tool"""
|
| 90 |
+
try:
|
| 91 |
+
return PythonREPLTool()
|
| 92 |
+
except Exception as e:
|
| 93 |
+
print(f"Warning: Could not initialize Python REPL tool: {e}")
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
async def create_playwright_tools() -> (
|
| 98 |
+
Tuple[List[Tool], Optional[object], Optional[object]]
|
| 99 |
+
):
|
| 100 |
+
"""Create Playwright browser tools"""
|
| 101 |
+
try:
|
| 102 |
+
playwright = await async_playwright().start()
|
| 103 |
+
browser = await playwright.chromium.launch(headless=False)
|
| 104 |
+
toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=browser)
|
| 105 |
+
return toolkit.get_tools(), browser, playwright
|
| 106 |
+
except Exception as e:
|
| 107 |
+
print(f"Warning: Could not initialize Playwright tools: {e}")
|
| 108 |
+
return [], None, None
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def get_tools(sandbox_dir: str = "sandbox") -> List[Tool]:
|
| 112 |
+
"""
|
| 113 |
+
Get all available tools.
|
| 114 |
+
|
| 115 |
+
Args:
|
| 116 |
+
sandbox_dir: Directory for file operations (default: "sandbox")
|
| 117 |
+
|
| 118 |
+
Returns:
|
| 119 |
+
List of initialized tools
|
| 120 |
+
"""
|
| 121 |
+
tools = []
|
| 122 |
+
|
| 123 |
+
# Add file management tools
|
| 124 |
+
file_tools = create_file_tools(sandbox_dir)
|
| 125 |
+
tools.extend(file_tools)
|
| 126 |
+
|
| 127 |
+
# Add search tool
|
| 128 |
+
search_tool = create_search_tool()
|
| 129 |
+
if search_tool:
|
| 130 |
+
tools.append(search_tool)
|
| 131 |
+
|
| 132 |
+
# Add math tools
|
| 133 |
+
math_tool = create_math_tool()
|
| 134 |
+
tools.extend(math_tool)
|
| 135 |
+
|
| 136 |
+
# Add Wikipedia tool
|
| 137 |
+
wiki_tool = create_wikipedia_tool()
|
| 138 |
+
if wiki_tool:
|
| 139 |
+
tools.append(wiki_tool)
|
| 140 |
+
|
| 141 |
+
# Add Python REPL tool
|
| 142 |
+
python_tool = create_python_repl_tool()
|
| 143 |
+
if python_tool:
|
| 144 |
+
tools.append(python_tool)
|
| 145 |
+
|
| 146 |
+
# Add ArXiv tool
|
| 147 |
+
arxiv_tool = create_arxiv_tool()
|
| 148 |
+
if arxiv_tool:
|
| 149 |
+
tools.append(arxiv_tool)
|
| 150 |
+
|
| 151 |
+
return tools
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
async def get_all_tools_with_browser(
|
| 155 |
+
sandbox_dir: str = "sandbox",
|
| 156 |
+
) -> Tuple[List[Tool], Optional[object], Optional[object]]:
|
| 157 |
+
"""
|
| 158 |
+
Get all tools including browser tools.
|
| 159 |
+
|
| 160 |
+
Args:
|
| 161 |
+
sandbox_dir: Directory for file operations (default: "sandbox")
|
| 162 |
+
|
| 163 |
+
Returns:
|
| 164 |
+
Tuple of (tools_list, browser_instance, playwright_instance)
|
| 165 |
+
"""
|
| 166 |
+
# Get basic tools
|
| 167 |
+
tools = get_tools(sandbox_dir)
|
| 168 |
+
|
| 169 |
+
# Add browser tools
|
| 170 |
+
browser_tools, browser, playwright = await create_playwright_tools()
|
| 171 |
+
tools.extend(browser_tools)
|
| 172 |
+
|
| 173 |
+
return tools, browser, playwright
|