forked from aayu2003/google-hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
100 lines (65 loc) · 2.51 KB
/
tools.py
File metadata and controls
100 lines (65 loc) · 2.51 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# getenv? vs getenviron
# always use pydantic for custom tools??
from scrap import scrapp
from crewai_tools import SerperDevTool
from dotenv import load_dotenv
from langchain.tools import tool as ltl
from pydantic import BaseModel, Field
load_dotenv()
import os
import json, requests
# we can create custom tools too - use pydantic to make custom search and calculation tool
# search_tool=SerperDevTool(
# api_key=os.getenv("SERPER_API_KEY"),
# verbose=True
# )
class calculationInput(BaseModel):
operation: str = Field(..., description="The mathematical operation to be calculated (string containing just numbers and operators)")
class customScrape(BaseModel):
url: str = Field(..., description="The URL/link of the webpage to scrape")
# class SearchTools():
# @ltl("Search the internet")
def search_internet(query):
"""Useful to search the internet about a a given topic and return relevant results"""
url = "https://google.serper.dev/search"
top_result_to_return = 20
payload = json.dumps({
"q": query
})
headers = {
'X-API-KEY': os.getenv("SERPER_API_KEY"),
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
# check if there is an organic key
if 'organic' not in response.json():
return "Sorry, I couldn't find anything about that, there could be an error with you serper api key."
else:
results = response.json()['organic']
string = []
for result in results[:top_result_to_return]:
try:
string.append({
"title": result['title'],
"link": result['link'],
"snippet": result['snippet']
})
except KeyError:
next
return string
# @ltl("Scrape A Webpage",args_schema=customScrape,return_direct=True)
def scrape(url:str):
"""Scrape the given webpage and return the text content"""
ans=scrapp(url)
print(ans)
return ans
@ltl("Make a calculation",args_schema=calculationInput,return_direct=True)
def calculate(operation:str) -> str:
"""Calculate the given mathematical operation and return the result. the expression should be a string containing just numbers and operators
use it to find the number of products for each price segment"""
try :
ans = eval(operation)
return "The result of operation is : "+str(ans)
except SyntaxError:
return "ERROR: INVALID SYNTAX IN MATHEMATICAL EXPRESSION"