Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- tools/citation_tool.py +7 -13
- tools/fallback_tool.py +3 -9
- tools/math_tool.py +7 -13
- tools/search_tool.py +6 -12
- tools/summarizer_tool.py +6 -12
tools/citation_tool.py
CHANGED
|
@@ -1,16 +1,10 @@
|
|
| 1 |
from smolagents import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
-
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"""Takes input as 'answer ||| url' and formats a markdown citation."""
|
| 12 |
-
try:
|
| 13 |
-
answer, url = input.split("|||")
|
| 14 |
-
return f"{answer.strip()}\n\nSource: [{url.strip()}]({url.strip()})"
|
| 15 |
-
except:
|
| 16 |
-
return "Could not format citation correctly."
|
|
|
|
| 1 |
from smolagents import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
+
def cite(input: str) -> str:
|
| 5 |
+
"""Takes input as 'answer ||| url' and formats a markdown citation."""
|
| 6 |
+
try:
|
| 7 |
+
answer, url = input.split("|||")
|
| 8 |
+
return f"{answer.strip()}\n\nSource: [{url.strip()}]({url.strip()})"
|
| 9 |
+
except:
|
| 10 |
+
return "Could not format citation correctly."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/fallback_tool.py
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
from smolagents import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
-
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
name = "fallback"
|
| 8 |
-
description = "Handle unanswerable or unclear queries."
|
| 9 |
-
|
| 10 |
-
def use(self, _: str) -> str:
|
| 11 |
-
"""Returns a polite fallback message."""
|
| 12 |
-
return "I'm sorry, I couldn't find the answer to your question. Could you rephrase or try something else?"
|
|
|
|
| 1 |
from smolagents import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
+
def fallback(_: str) -> str:
|
| 5 |
+
"""Returns a polite fallback message."""
|
| 6 |
+
return "I'm sorry, I couldn't find the answer to your question. Could you rephrase or try something else?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/math_tool.py
CHANGED
|
@@ -1,16 +1,10 @@
|
|
| 1 |
from smolagents import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
-
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"""Evaluates a math expression using sandboxed eval."""
|
| 12 |
-
try:
|
| 13 |
-
result = str(eval(code, {"__builtins__": {}}))
|
| 14 |
-
return f"Answer: {result}"
|
| 15 |
-
except Exception as e:
|
| 16 |
-
return f"Error: {str(e)}"
|
|
|
|
| 1 |
from smolagents import tool
|
| 2 |
|
| 3 |
@tool
|
| 4 |
+
def python(code: str) -> str:
|
| 5 |
+
"""Evaluates a math expression using sandboxed eval."""
|
| 6 |
+
try:
|
| 7 |
+
result = str(eval(code, {"__builtins__": {}}))
|
| 8 |
+
return f"Answer: {result}"
|
| 9 |
+
except Exception as e:
|
| 10 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/search_tool.py
CHANGED
|
@@ -2,15 +2,9 @@ from smolagents import tool
|
|
| 2 |
from duckduckgo_search import DDGS
|
| 3 |
|
| 4 |
@tool
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def use(self, query: str) -> str:
|
| 12 |
-
"""Returns top search results for a given query."""
|
| 13 |
-
with DDGS() as ddgs:
|
| 14 |
-
results = ddgs.text(query)
|
| 15 |
-
output = [f"{r['title']} - {r['href']}" for r in results[:3]]
|
| 16 |
-
return "\n".join(output) if output else "No relevant results found."
|
|
|
|
| 2 |
from duckduckgo_search import DDGS
|
| 3 |
|
| 4 |
@tool
|
| 5 |
+
def web_search(query: str) -> str:
|
| 6 |
+
"""Returns top search results for a given query."""
|
| 7 |
+
with DDGS() as ddgs:
|
| 8 |
+
results = ddgs.text(query)
|
| 9 |
+
output = [f"{r['title']} - {r['href']}" for r in results[:3]]
|
| 10 |
+
return "\n".join(output) if output else "No relevant results found."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools/summarizer_tool.py
CHANGED
|
@@ -4,15 +4,9 @@ from transformers import pipeline
|
|
| 4 |
summarizer = pipeline("summarization")
|
| 5 |
|
| 6 |
@tool
|
| 7 |
-
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def use(self, input: str) -> str:
|
| 14 |
-
"""Returns a summarized version of the input text."""
|
| 15 |
-
if len(input) < 50:
|
| 16 |
-
return input
|
| 17 |
-
result = summarizer(input, max_length=100, min_length=25, do_sample=False)
|
| 18 |
-
return result[0]['summary_text']
|
|
|
|
| 4 |
summarizer = pipeline("summarization")
|
| 5 |
|
| 6 |
@tool
|
| 7 |
+
def summarize(input: str) -> str:
|
| 8 |
+
"""Returns a summarized version of the input text."""
|
| 9 |
+
if len(input) < 50:
|
| 10 |
+
return input
|
| 11 |
+
result = summarizer(input, max_length=100, min_length=25, do_sample=False)
|
| 12 |
+
return result[0]['summary_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|