-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
66 lines (52 loc) · 1.95 KB
/
tools.py
File metadata and controls
66 lines (52 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import requests
import json
headers = {
"X-API-KEY": os.getenv("SERPER_API_KEY", "your_api_key_here"),
"Content-Type": "application/json",
}
def search_google(query: str) -> str:
"""
Search Google for a given query.
param query: The search query.
"""
url = "https://google.serper.dev/search"
payload = json.dumps({"q": query})
response = requests.request("POST", url, headers=headers, data=payload)
out = response.json()
# Construct a markdown string with the search results
markdown = f"## Search Results for '{query}'\n\n"
for result in out.get("organic", []):
title = result.get("title")
link = result.get("link")
snippet = result.get("snippet")
markdown += f"- **[{title}]({link})**: {snippet}\n"
markdown += "\n### Related Searches\n\n"
for related in out.get("relatedSearches", []):
related_query = related.get("query")
markdown += f"- **{related_query}**\n"
return markdown
def scrape_website(url: str) -> str:
"""
Scrape a website for its content.
param url: The URL of the website to scrape.
"""
srv_url = "https://scrape.serper.dev"
payload = json.dumps({"url": url})
response = requests.request("POST", srv_url, headers=headers, data=payload)
out = response.json()
# Construct a markdown string with the scraped content
markdown = f"## Scraped Content from '{url}'\n\n"
markdown += (
f"### Title: {out.get('metadata', {}).get('title', 'No title available')}\n\n"
)
markdown += f"### Description: {out.get('metadata', {}).get('Description', 'No description available')}\n\n"
markdown += f"### Content:\n{out.get('text', 'No content available')}\n\n"
return markdown
tools = [search_google, scrape_website]
if __name__ == "__main__":
# Example usage
query = "Python programming"
print(search_google(query))
url = "https://www.apple.com"
print(scrape_website(url))