Spaces:
Sleeping
Sleeping
AyrtonSur
commited on
Commit
·
f6affe5
1
Parent(s):
02a3443
feat: adding new tools
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
from duckduckgo_search import DDGS
|
| 3 |
import datetime
|
| 4 |
import requests
|
|
@@ -36,7 +38,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 36 |
|
| 37 |
@tool
|
| 38 |
def search_for_something(query: str) -> str:
|
| 39 |
-
"""A tool that fetches the 5 first websites that appear in a query, with their title, url and snippets.
|
| 40 |
Args:
|
| 41 |
query: String that represent what is being searched
|
| 42 |
"""
|
|
@@ -53,6 +55,30 @@ def search_for_something(query: str) -> str:
|
|
| 53 |
return "\n".join(formatted_results)
|
| 54 |
except Exception as e:
|
| 55 |
return f"Error searching for {query} in the Web: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
final_answer = FinalAnswerTool()
|
|
@@ -76,7 +102,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 76 |
|
| 77 |
agent = CodeAgent(
|
| 78 |
model=model,
|
| 79 |
-
tools=[final_answer, get_current_time_in_timezone, search_for_something], ## add your tools here (don't remove final answer)
|
| 80 |
max_steps=6,
|
| 81 |
verbosity_level=1,
|
| 82 |
grammar=None,
|
|
|
|
| 1 |
+
import json
|
| 2 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
from duckduckgo_search import DDGS
|
| 5 |
import datetime
|
| 6 |
import requests
|
|
|
|
| 38 |
|
| 39 |
@tool
|
| 40 |
def search_for_something(query: str) -> str:
|
| 41 |
+
"""A tool that fetches the 5 first websites that appear in a query, with their title, url and snippets. Can be used to get websites urls to be used in the request_get tool.
|
| 42 |
Args:
|
| 43 |
query: String that represent what is being searched
|
| 44 |
"""
|
|
|
|
| 55 |
return "\n".join(formatted_results)
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error searching for {query} in the Web: {str(e)}"
|
| 58 |
+
|
| 59 |
+
@tool
|
| 60 |
+
def request_get(url: str) -> str:
|
| 61 |
+
"""Tool that is used to make get requests in any API or Website needed and get its response. Can be used after the search_for_something tool to get more specific things in the websites returned.
|
| 62 |
+
Args:
|
| 63 |
+
url: Url that is used to reach the API or Website.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
r = requests.get(url)
|
| 68 |
+
if r.status_code == 200:
|
| 69 |
+
try:
|
| 70 |
+
data = r.json()
|
| 71 |
+
return json.dumps(data, indent=2)
|
| 72 |
+
except ValueError:
|
| 73 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
| 74 |
+
title = soup.title.string if soup.title else "No title available"
|
| 75 |
+
body_text = ' '.join([p.text for p in soup.find_all('p')[:5]])
|
| 76 |
+
return f"Title: {title}\n\nContent:\n{body_text}"
|
| 77 |
+
else:
|
| 78 |
+
return f"Error: Received status code {r.status_code} from {url}"
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Error in request: {str(e)}"
|
| 82 |
|
| 83 |
|
| 84 |
final_answer = FinalAnswerTool()
|
|
|
|
| 102 |
|
| 103 |
agent = CodeAgent(
|
| 104 |
model=model,
|
| 105 |
+
tools=[final_answer, get_current_time_in_timezone, search_for_something, image_generation_tool, request_get], ## add your tools here (don't remove final answer)
|
| 106 |
max_steps=6,
|
| 107 |
verbosity_level=1,
|
| 108 |
grammar=None,
|