This commit is contained in:
cjw
2026-02-17 02:31:39 +08:00
parent 2f0e25c400
commit 27a1b8a7e9
14 changed files with 877 additions and 1 deletions
+27
View File
@@ -0,0 +1,27 @@
from typing import Dict, Any
from langchain_core.tools import BaseTool
class CalculatorTool(BaseTool):
"""A simple calculator tool for mathematical operations"""
name: str = "calculator"
description: str = "Perform mathematical calculations. Input should be a mathematical expression like '2 + 2' or '10 * (3 + 5)'"
def _run(self, expression: str) -> str:
"""Evaluate a mathematical expression"""
try:
# Security: Only allow safe mathematical operations
allowed_chars = set("0123456789+-*/(). ")
if not all(c in allowed_chars for c in expression):
return "Error: Expression contains invalid characters"
# Evaluate the expression
result = eval(expression)
return f"Result: {result}"
except Exception as e:
return f"Error calculating expression: {str(e)}"
async def _arun(self, expression: str) -> str:
"""Async version of the tool"""
return self._run(expression)
+25
View File
@@ -0,0 +1,25 @@
from typing import Dict, Any, List
from langchain_core.tools import BaseTool
import requests
class WebSearchTool(BaseTool):
"""A tool for searching the web (placeholder implementation)"""
name: str = "web_search"
description: str = "Search the web for information. Input should be a search query."
def _run(self, query: str) -> str:
"""Search the web for information"""
# This is a placeholder implementation
# In a real implementation, you would integrate with a search API
# like Serper, Tavily, or Google Search API
return f"Web search functionality for query: '{query}' is not implemented. This is a placeholder. To implement real web search, you would need to:
1. Sign up for a search API service (e.g., Serper, Tavily)
2. Add your API key to the .env file
3. Implement the actual search logic here"
async def _arun(self, query: str) -> str:
"""Async version of the tool"""
return self._run(query)