KrishGoyani commited on
Commit
fbc318f
·
verified ·
1 Parent(s): 888cdb0

Upload 4 files

Browse files
Files changed (4) hide show
  1. browser_tools.py +53 -0
  2. calculator_tools.py +43 -0
  3. search_tools.py +36 -0
  4. sec_tools.py +108 -0
browser_tools.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ from crewai import Agent, Task
4
+ from langchain.tools import tool
5
+ import os
6
+ from langchain_google_genai import ChatGoogleGenerativeAI
7
+
8
+ class BrowserTools:
9
+ @tool("Scrape website content")
10
+ def scrape_and_summarize_website(website):
11
+ """Useful to scrape and summarize a website content"""
12
+ # Fetch the webpage content
13
+ headers = {
14
+ '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'
15
+ }
16
+ print("=====================used===================")
17
+ response = requests.get(website, headers=headers)
18
+
19
+ # Parse the HTML content
20
+ soup = BeautifulSoup(response.content, 'html.parser')
21
+
22
+ # Extract text content
23
+ for script in soup(["script", "style"]):
24
+ script.decompose()
25
+ content = soup.get_text(separator="\n")
26
+
27
+ # Clean up the text
28
+ lines = (line.strip() for line in content.splitlines())
29
+ chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
30
+ content = '\n'.join(chunk for chunk in chunks if chunk)
31
+ print(content)
32
+ # Split content into chunks
33
+ content_chunks = [content[i:i + 8000] for i in range(0, len(content), 8000)]
34
+
35
+ summaries = []
36
+ for chunk in content_chunks:
37
+ agent = Agent(
38
+ role='Principal Researcher',
39
+ goal='Do amazing research and summaries based on the content you are working with',
40
+ backstory="You're a Principal Researcher at a big company and you need to do research about a given topic.",
41
+ llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash",google_api_key = os.getenv("GOOGLE_API_KEY")),
42
+ allow_delegation=False
43
+ )
44
+ task = Task(
45
+ agent=agent,
46
+ description=f'Analyze and summarize the content below, make sure to include the most relevant information in the summary, return only the summary nothing else.\n\nCONTENT\n----------\n{chunk}',
47
+
48
+
49
+ )
50
+ summary = task.execute()
51
+ summaries.append(summary)
52
+
53
+ return "\n\n".join(summaries)
calculator_tools.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.tools import tool
2
+
3
+
4
+ class CalculatorTools:
5
+
6
+ @tool("Make a calculation")
7
+ def calculate(operation):
8
+ """Useful to perform any mathematical calculations,
9
+ like sum, minus, multiplication, division, etc.
10
+ The input to this tool should be a mathematical
11
+ expression, a couple examples are `200*7` or `5000/2*10`
12
+ """
13
+ try:
14
+ return eval(operation)
15
+ except SyntaxError:
16
+ return "Error: Invalid syntax in mathematical expression"
17
+
18
+ from pydantic import BaseModel, Field
19
+ # from langchain.tools import tool
20
+
21
+ # # Define a Pydantic model for the tool's input parameters
22
+ # class CalculationInput(BaseModel):
23
+ # operation: str = Field(..., description="The mathematical operation to perform")
24
+ # factor: float = Field(..., description="A factor by which to multiply the result of the operation")
25
+
26
+ # # Use the tool decorator with the args_schema parameter pointing to the Pydantic model
27
+ # @tool("perform_calculation", args_schema=CalculationInput, return_direct=True)
28
+ # def perform_calculation(operation: str, factor: float) -> str:
29
+ # """
30
+ # Performs a specified mathematical operation and multiplies the result by a given factor.
31
+
32
+ # Parameters:
33
+ # - operation (str): A string representing a mathematical operation (e.g., "10 + 5").
34
+ # - factor (float): A factor by which to multiply the result of the operation.
35
+
36
+ # Returns:
37
+ # - A string representation of the calculation result.
38
+ # """
39
+ # # Perform the calculation
40
+ # result = eval(operation) * factor
41
+
42
+ # # Return the result as a string
43
+ # return f"The result of '{operation}' multiplied by {factor} is {result}."
search_tools.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import requests
4
+ from langchain.tools import tool
5
+
6
+
7
+ class SearchTools():
8
+
9
+ @tool("Search the internet")
10
+ def search_internet(query):
11
+ """Useful to search the internet
12
+ about a a given topic and return relevant results"""
13
+ top_result_to_return = 4
14
+ url = "https://google.serper.dev/search"
15
+ payload = json.dumps({"q": query})
16
+ headers = {
17
+ 'X-API-KEY': os.environ['SERPER_API_KEY'],
18
+ 'content-type': 'application/json'
19
+ }
20
+ response = requests.request("POST", url, headers=headers, data=payload)
21
+ # check if there is an organic key
22
+ if 'organic' not in response.json():
23
+ return "Sorry, I couldn't find anything about that, there could be an error with you serper api key."
24
+ else:
25
+ results = response.json()['organic']
26
+ string = []
27
+ for result in results[:top_result_to_return]:
28
+ try:
29
+ string.append('\n'.join([
30
+ f"Title: {result['title']}", f"Link: {result['link']}",
31
+ f"Snippet: {result['snippet']}", "\n-----------------"
32
+ ]))
33
+ except KeyError:
34
+ next
35
+
36
+ return '\n'.join(string)
sec_tools.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import requests
4
+
5
+ from langchain.tools import tool
6
+ from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
8
+ from langchain_community.vectorstores import FAISS
9
+
10
+ from sec_api import QueryApi
11
+ from unstructured.partition.html import partition_html
12
+
13
+ class SECTools():
14
+ @tool("Search 10-Q form")
15
+ def search_10q(data):
16
+ """
17
+ Useful to search information from the latest 10-Q form for a
18
+ given stock.
19
+ The input to this tool should be a pipe (|) separated text of
20
+ length two, representing the stock ticker you are interested, what
21
+ question you have from it.
22
+ For example, `AAPL|what was last quarter's revenue`.
23
+ """
24
+ stock, ask = data.split("|")
25
+ queryApi = QueryApi(api_key=os.environ['SEC_API_API_KEY'])
26
+ query = {
27
+ "query": {
28
+ "query_string": {
29
+ "query": f"ticker:{stock} AND formType:\"10-Q\""
30
+ }
31
+ },
32
+ "from": "0",
33
+ "size": "1",
34
+ "sort": [{ "filedAt": { "order": "desc" }}]
35
+ }
36
+
37
+ fillings = queryApi.get_filings(query)['filings']
38
+ link = fillings[0]['linkToFilingDetails']
39
+ answer = SECTools.__embedding_search(link, ask)
40
+ return answer
41
+
42
+ @tool("Search 10-K form")
43
+ def search_10k(data):
44
+ """
45
+ Useful to search information from the latest 10-K form for a
46
+ given stock.
47
+ The input to this tool should be a pipe (|) separated text of
48
+ length two, representing the stock ticker you are interested, what
49
+ question you have from it.
50
+ For example, `AAPL|what was last year's revenue`.
51
+ """
52
+ stock, ask = data.split("|")
53
+ queryApi = QueryApi(api_key=os.environ['SEC_API_API_KEY'])
54
+ query = {
55
+ "query": {
56
+ "query_string": {
57
+ "query": f"ticker:{stock} AND formType:\"10-K\""
58
+ }
59
+ },
60
+ "from": "0",
61
+ "size": "1",
62
+ "sort": [{ "filedAt": { "order": "desc" }}]
63
+ }
64
+
65
+ fillings = queryApi.get_filings(query)['filings']
66
+ link = fillings[0]['linkToFilingDetails']
67
+ answer = SECTools.__embedding_search(link, ask)
68
+ return answer
69
+
70
+ def __embedding_search(url, ask):
71
+ text = SECTools.__download_form_html(url)
72
+ elements = partition_html(text=text)
73
+ content = "\n".join([str(el) for el in elements])
74
+ text_splitter = CharacterTextSplitter(
75
+ separator = "\n",
76
+ chunk_size = 1000,
77
+ chunk_overlap = 150,
78
+ length_function = len,
79
+ is_separator_regex = False,
80
+ )
81
+ docs = text_splitter.create_documents([content])
82
+ retriever = FAISS.from_documents(
83
+ docs, GoogleGenerativeAIEmbeddings(model="models/embedding-001", api_key = os.getenv("GOOGLE_API_KEY"))
84
+ ).as_retriever()
85
+ answers = retriever.get_relevant_documents(ask, top_k=4)
86
+ answers = "\n\n".join([a.page_content for a in answers])
87
+ return answers
88
+
89
+ def __download_form_html(url):
90
+ headers = {
91
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
92
+ 'Accept-Encoding': 'gzip, deflate, br',
93
+ 'Accept-Language': 'en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7',
94
+ 'Cache-Control': 'max-age=0',
95
+ 'Dnt': '1',
96
+ 'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120"',
97
+ 'Sec-Ch-Ua-Mobile': '?0',
98
+ 'Sec-Ch-Ua-Platform': '"macOS"',
99
+ 'Sec-Fetch-Dest': 'document',
100
+ 'Sec-Fetch-Mode': 'navigate',
101
+ 'Sec-Fetch-Site': 'none',
102
+ 'Sec-Fetch-User': '?1',
103
+ 'Upgrade-Insecure-Requests': '1',
104
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
105
+ }
106
+
107
+ response = requests.get(url, headers=headers)
108
+ return response.text