Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- tools/__init__.py +0 -0
- tools/browser_tools.py +38 -0
- tools/calculator_tools.py +15 -0
- tools/search_tools.py +37 -0
tools/__init__.py
ADDED
File without changes
|
tools/browser_tools.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import requests
|
5 |
+
from crewai import Agent, Task
|
6 |
+
from langchain.tools import tool
|
7 |
+
from unstructured.partition.html import partition_html
|
8 |
+
|
9 |
+
|
10 |
+
class BrowserTools():
|
11 |
+
|
12 |
+
@tool("Scrape website content")
|
13 |
+
def scrape_and_summarize_website(website):
|
14 |
+
"""Useful to scrape and summarize a website content"""
|
15 |
+
url = f"https://chrome.browserless.io/content?token={os.environ['BROWSERLESS_API_KEY']}"
|
16 |
+
payload = json.dumps({"url": website})
|
17 |
+
headers = {'cache-control': 'no-cache', 'content-type': 'application/json'}
|
18 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
19 |
+
elements = partition_html(text=response.text)
|
20 |
+
content = "\n\n".join([str(el) for el in elements])
|
21 |
+
content = [content[i:i + 8000] for i in range(0, len(content), 8000)]
|
22 |
+
summaries = []
|
23 |
+
for chunk in content:
|
24 |
+
agent = Agent(
|
25 |
+
role='Principal Researcher',
|
26 |
+
goal=
|
27 |
+
'Do amazing researches and summaries based on the content you are working with',
|
28 |
+
backstory=
|
29 |
+
"You're a Principal Researcher at a big company and you need to do a research about a given topic.",
|
30 |
+
allow_delegation=False)
|
31 |
+
task = Task(
|
32 |
+
agent=agent,
|
33 |
+
description=
|
34 |
+
f'Analyze and summarize the content bellow, make sure to include the most relevant information in the summary, return only the summary nothing else.\n\nCONTENT\n----------\n{chunk}'
|
35 |
+
)
|
36 |
+
summary = task.execute()
|
37 |
+
summaries.append(summary)
|
38 |
+
return "\n\n".join(summaries)
|
tools/calculator_tools.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.tools import tool
|
2 |
+
|
3 |
+
class CalculatorTools():
|
4 |
+
|
5 |
+
@tool("Make a calculation")
|
6 |
+
def calculate(operation):
|
7 |
+
"""Useful to perform any mathematical calculations,
|
8 |
+
like sum, minus, multiplication, division, etc.
|
9 |
+
The input to this tool should be a mathematical
|
10 |
+
expression, a couple examples are `200*7` or `5000/2*10`
|
11 |
+
"""
|
12 |
+
try:
|
13 |
+
return eval(operation)
|
14 |
+
except SyntaxError:
|
15 |
+
return "Error: Invalid syntax in mathematical expression"
|
tools/search_tools.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import requests
|
5 |
+
from langchain.tools import tool
|
6 |
+
|
7 |
+
|
8 |
+
class SearchTools():
|
9 |
+
|
10 |
+
@tool("Search the internet")
|
11 |
+
def search_internet(query):
|
12 |
+
"""Useful to search the internet
|
13 |
+
about a a given topic and return relevant results"""
|
14 |
+
top_result_to_return = 4
|
15 |
+
url = "https://google.serper.dev/search"
|
16 |
+
payload = json.dumps({"q": query})
|
17 |
+
headers = {
|
18 |
+
'X-API-KEY': os.environ['SERPER_API_KEY'],
|
19 |
+
'content-type': 'application/json'
|
20 |
+
}
|
21 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
22 |
+
# check if there is an organic key
|
23 |
+
if 'organic' not in response.json():
|
24 |
+
return "Sorry, I couldn't find anything about that, there could be an error with you serper api key."
|
25 |
+
else:
|
26 |
+
results = response.json()['organic']
|
27 |
+
string = []
|
28 |
+
for result in results[:top_result_to_return]:
|
29 |
+
try:
|
30 |
+
string.append('\n'.join([
|
31 |
+
f"Title: {result['title']}", f"Link: {result['link']}",
|
32 |
+
f"Snippet: {result['snippet']}", "\n-----------------"
|
33 |
+
]))
|
34 |
+
except KeyError:
|
35 |
+
next
|
36 |
+
|
37 |
+
return '\n'.join(string)
|