Spaces:
Build error
Build error
Update tools/web_search_tools.py
Browse files- tools/web_search_tools.py +29 -3
tools/web_search_tools.py
CHANGED
|
@@ -6,6 +6,15 @@ from langchain.tools import tool
|
|
| 6 |
class WebSearchTools:
|
| 7 |
@tool("Search the internet")
|
| 8 |
def search_internet(query):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
search_url = f"https://www.google.com/search?q={query}&tbm=nws"
|
| 10 |
headers = {
|
| 11 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
|
@@ -26,6 +35,15 @@ class WebSearchTools:
|
|
| 26 |
|
| 27 |
@tool("Scrape website content")
|
| 28 |
def scrape_and_summarize_website(url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
response = requests.get(url)
|
| 30 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 31 |
# Extract and summarize text
|
|
@@ -45,8 +63,16 @@ class WebSearchTools:
|
|
| 45 |
|
| 46 |
@tool("Download image")
|
| 47 |
def download_image(image_url, save_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
img_data = requests.get(image_url).content
|
| 49 |
with open(save_path, 'wb') as img_file:
|
| 50 |
-
img_file.write(img_data)
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 6 |
class WebSearchTools:
|
| 7 |
@tool("Search the internet")
|
| 8 |
def search_internet(query):
|
| 9 |
+
"""
|
| 10 |
+
Search the internet for the given query and return a list of search results with title, link, and snippet.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
query (str): The search query.
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
list: A list of dictionaries containing the title, link, and snippet of each search result.
|
| 17 |
+
"""
|
| 18 |
search_url = f"https://www.google.com/search?q={query}&tbm=nws"
|
| 19 |
headers = {
|
| 20 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
|
|
|
| 35 |
|
| 36 |
@tool("Scrape website content")
|
| 37 |
def scrape_and_summarize_website(url):
|
| 38 |
+
"""
|
| 39 |
+
Scrape the given website URL and return the extracted text content. Download images and save them locally.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
url (str): The website URL to scrape.
|
| 43 |
+
|
| 44 |
+
Returns:
|
| 45 |
+
str: The extracted text content from the website.
|
| 46 |
+
"""
|
| 47 |
response = requests.get(url)
|
| 48 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 49 |
# Extract and summarize text
|
|
|
|
| 63 |
|
| 64 |
@tool("Download image")
|
| 65 |
def download_image(image_url, save_path):
|
| 66 |
+
"""
|
| 67 |
+
Download an image from the given URL and save it to the specified path.
|
| 68 |
+
|
| 69 |
+
Args:
|
| 70 |
+
image_url (str): The URL of the image to download.
|
| 71 |
+
save_path (str): The path where the image will be saved.
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
None
|
| 75 |
+
"""
|
| 76 |
img_data = requests.get(image_url).content
|
| 77 |
with open(save_path, 'wb') as img_file:
|
| 78 |
+
img_file.write(img_data)
|
|
|
|
|
|