Update ai_tools.py
Browse files- ai_tools.py +11 -10
ai_tools.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
from duckduckgo_search import DDGS
|
| 2 |
import re
|
| 3 |
-
from
|
|
|
|
| 4 |
|
| 5 |
class BaseTool:
|
| 6 |
def __init__(self, name: str, description: str):
|
|
@@ -14,33 +14,34 @@ class Calculator(BaseTool):
|
|
| 14 |
def __init__(self):
|
| 15 |
super().__init__(
|
| 16 |
name="Calculator",
|
| 17 |
-
description="Performs basic arithmetic. Input
|
| 18 |
)
|
| 19 |
|
| 20 |
def run(self, expression: str) -> str:
|
| 21 |
try:
|
| 22 |
-
# Safe evaluation for basic operations
|
| 23 |
expression = expression.replace(' ', '')
|
| 24 |
if not re.match(r'^[\d+\-*/.()]+$', expression):
|
| 25 |
-
return "Error: Invalid characters"
|
| 26 |
result = eval(expression)
|
| 27 |
return str(result)
|
| 28 |
except Exception as e:
|
| 29 |
return f"Calculation error: {str(e)}"
|
| 30 |
|
| 31 |
class DocRetriever(BaseTool):
|
| 32 |
-
def __init__(self
|
| 33 |
super().__init__(
|
| 34 |
name="DocRetriever",
|
| 35 |
description="Searches provided text. Input: 'query: <search_term>'"
|
| 36 |
)
|
| 37 |
-
self.document =
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def run(self, query: str) -> str:
|
| 40 |
if not self.document:
|
| 41 |
-
return "
|
| 42 |
|
| 43 |
-
# Case-insensitive search for sentences containing query
|
| 44 |
sentences = [s.strip() for s in self.document.split('.') if s]
|
| 45 |
results = [s for s in sentences if query.lower() in s.lower()]
|
| 46 |
return '. '.join(results[:3]) + '...' if results else "No matches found"
|
|
@@ -49,7 +50,7 @@ class WebSearcher(BaseTool):
|
|
| 49 |
def __init__(self):
|
| 50 |
super().__init__(
|
| 51 |
name="WebSearcher",
|
| 52 |
-
description="Searches the web. Input:
|
| 53 |
)
|
| 54 |
|
| 55 |
def run(self, query: str) -> str:
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
+
from duckduckgo_search import DDGS
|
| 3 |
+
from typing import List, Dict, Any
|
| 4 |
|
| 5 |
class BaseTool:
|
| 6 |
def __init__(self, name: str, description: str):
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
super().__init__(
|
| 16 |
name="Calculator",
|
| 17 |
+
description="Performs basic arithmetic. Input: math expression as string"
|
| 18 |
)
|
| 19 |
|
| 20 |
def run(self, expression: str) -> str:
|
| 21 |
try:
|
|
|
|
| 22 |
expression = expression.replace(' ', '')
|
| 23 |
if not re.match(r'^[\d+\-*/.()]+$', expression):
|
| 24 |
+
return "Error: Invalid characters in expression"
|
| 25 |
result = eval(expression)
|
| 26 |
return str(result)
|
| 27 |
except Exception as e:
|
| 28 |
return f"Calculation error: {str(e)}"
|
| 29 |
|
| 30 |
class DocRetriever(BaseTool):
|
| 31 |
+
def __init__(self):
|
| 32 |
super().__init__(
|
| 33 |
name="DocRetriever",
|
| 34 |
description="Searches provided text. Input: 'query: <search_term>'"
|
| 35 |
)
|
| 36 |
+
self.document = ""
|
| 37 |
+
|
| 38 |
+
def load_document(self, text: str):
|
| 39 |
+
self.document = text
|
| 40 |
|
| 41 |
def run(self, query: str) -> str:
|
| 42 |
if not self.document:
|
| 43 |
+
return "No document loaded"
|
| 44 |
|
|
|
|
| 45 |
sentences = [s.strip() for s in self.document.split('.') if s]
|
| 46 |
results = [s for s in sentences if query.lower() in s.lower()]
|
| 47 |
return '. '.join(results[:3]) + '...' if results else "No matches found"
|
|
|
|
| 50 |
def __init__(self):
|
| 51 |
super().__init__(
|
| 52 |
name="WebSearcher",
|
| 53 |
+
description="Searches the web. Input: search query"
|
| 54 |
)
|
| 55 |
|
| 56 |
def run(self, query: str) -> str:
|