-
Notifications
You must be signed in to change notification settings - Fork 96
/
functions.py
314 lines (270 loc) · 10.4 KB
/
functions.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import re
import inspect
import requests
import pandas as pd
import yfinance as yf
import concurrent.futures
from typing import List
from bs4 import BeautifulSoup
from utils import inference_logger
from langchain.tools import tool
from langchain_core.utils.function_calling import convert_to_openai_tool
@tool
def code_interpreter(code_markdown: str) -> dict | str:
"""
Execute the provided Python code string on the terminal using exec.
The string should contain valid, executable and pure Python code in markdown syntax.
Code should also import any required Python packages.
Args:
code_markdown (str): The Python code with markdown syntax to be executed.
For example: ```python\n<code-string>\n```
Returns:
dict | str: A dictionary containing variables declared and values returned by function calls,
or an error message if an exception occurred.
Note:
Use this function with caution, as executing arbitrary code can pose security risks.
"""
try:
# Extracting code from Markdown code block
code_lines = code_markdown.split('\n')[1:-1]
code_without_markdown = '\n'.join(code_lines)
# Create a new namespace for code execution
exec_namespace = {}
# Execute the code in the new namespace
exec(code_without_markdown, exec_namespace)
# Collect variables and function call results
result_dict = {}
for name, value in exec_namespace.items():
if callable(value):
try:
result_dict[name] = value()
except TypeError:
# If the function requires arguments, attempt to call it with arguments from the namespace
arg_names = inspect.getfullargspec(value).args
args = {arg_name: exec_namespace.get(arg_name) for arg_name in arg_names}
result_dict[name] = value(**args)
elif not name.startswith('_'): # Exclude variables starting with '_'
result_dict[name] = value
return result_dict
except Exception as e:
error_message = f"An error occurred: {e}"
inference_logger.error(error_message)
return error_message
@tool
def google_search_and_scrape(query: str) -> dict:
"""
Performs a Google search for the given query, retrieves the top search result URLs,
and scrapes the text content and table data from those pages in parallel.
Args:
query (str): The search query.
Returns:
list: A list of dictionaries containing the URL, text content, and table data for each scraped page.
"""
num_results = 2
url = 'https://www.google.com/search'
params = {'q': query, 'num': num_results}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.3'}
inference_logger.info(f"Performing google search with query: {query}\nplease wait...")
response = requests.get(url, params=params, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
urls = [result.find('a')['href'] for result in soup.find_all('div', class_='tF2Cxc')]
inference_logger.info(f"Scraping text from urls, please wait...")
[inference_logger.info(url) for url in urls]
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(lambda url: (url, requests.get(url, headers=headers).text if isinstance(url, str) else None), url) for url in urls[:num_results] if isinstance(url, str)]
results = []
for future in concurrent.futures.as_completed(futures):
url, html = future.result()
soup = BeautifulSoup(html, 'html.parser')
paragraphs = [p.text.strip() for p in soup.find_all('p') if p.text.strip()]
text_content = ' '.join(paragraphs)
text_content = re.sub(r'\s+', ' ', text_content)
table_data = [[cell.get_text(strip=True) for cell in row.find_all('td')] for table in soup.find_all('table') for row in table.find_all('tr')]
if text_content or table_data:
results.append({'url': url, 'content': text_content, 'tables': table_data})
return results
@tool
def get_current_stock_price(symbol: str) -> float:
"""
Get the current stock price for a given symbol.
Args:
symbol (str): The stock symbol.
Returns:
float: The current stock price, or None if an error occurs.
"""
try:
stock = yf.Ticker(symbol)
# Use "regularMarketPrice" for regular market hours, or "currentPrice" for pre/post market
current_price = stock.info.get("regularMarketPrice", stock.info.get("currentPrice"))
return current_price if current_price else None
except Exception as e:
print(f"Error fetching current price for {symbol}: {e}")
return None
@tool
def get_stock_fundamentals(symbol: str) -> dict:
"""
Get fundamental data for a given stock symbol using yfinance API.
Args:
symbol (str): The stock symbol.
Returns:
dict: A dictionary containing fundamental data.
Keys:
- 'symbol': The stock symbol.
- 'company_name': The long name of the company.
- 'sector': The sector to which the company belongs.
- 'industry': The industry to which the company belongs.
- 'market_cap': The market capitalization of the company.
- 'pe_ratio': The forward price-to-earnings ratio.
- 'pb_ratio': The price-to-book ratio.
- 'dividend_yield': The dividend yield.
- 'eps': The trailing earnings per share.
- 'beta': The beta value of the stock.
- '52_week_high': The 52-week high price of the stock.
- '52_week_low': The 52-week low price of the stock.
"""
try:
stock = yf.Ticker(symbol)
info = stock.info
fundamentals = {
'symbol': symbol,
'company_name': info.get('longName', ''),
'sector': info.get('sector', ''),
'industry': info.get('industry', ''),
'market_cap': info.get('marketCap', None),
'pe_ratio': info.get('forwardPE', None),
'pb_ratio': info.get('priceToBook', None),
'dividend_yield': info.get('dividendYield', None),
'eps': info.get('trailingEps', None),
'beta': info.get('beta', None),
'52_week_high': info.get('fiftyTwoWeekHigh', None),
'52_week_low': info.get('fiftyTwoWeekLow', None)
}
return fundamentals
except Exception as e:
print(f"Error getting fundamentals for {symbol}: {e}")
return {}
@tool
def get_financial_statements(symbol: str) -> dict:
"""
Get financial statements for a given stock symbol.
Args:
symbol (str): The stock symbol.
Returns:
dict: Dictionary containing financial statements (income statement, balance sheet, cash flow statement).
"""
try:
stock = yf.Ticker(symbol)
financials = stock.financials
return financials
except Exception as e:
print(f"Error fetching financial statements for {symbol}: {e}")
return {}
@tool
def get_key_financial_ratios(symbol: str) -> dict:
"""
Get key financial ratios for a given stock symbol.
Args:
symbol (str): The stock symbol.
Returns:
dict: Dictionary containing key financial ratios.
"""
try:
stock = yf.Ticker(symbol)
key_ratios = stock.info
return key_ratios
except Exception as e:
print(f"Error fetching key financial ratios for {symbol}: {e}")
return {}
@tool
def get_analyst_recommendations(symbol: str) -> pd.DataFrame:
"""
Get analyst recommendations for a given stock symbol.
Args:
symbol (str): The stock symbol.
Returns:
pd.DataFrame: DataFrame containing analyst recommendations.
"""
try:
stock = yf.Ticker(symbol)
recommendations = stock.recommendations
return recommendations
except Exception as e:
print(f"Error fetching analyst recommendations for {symbol}: {e}")
return pd.DataFrame()
@tool
def get_dividend_data(symbol: str) -> pd.DataFrame:
"""
Get dividend data for a given stock symbol.
Args:
symbol (str): The stock symbol.
Returns:
pd.DataFrame: DataFrame containing dividend data.
"""
try:
stock = yf.Ticker(symbol)
dividends = stock.dividends
return dividends
except Exception as e:
print(f"Error fetching dividend data for {symbol}: {e}")
return pd.DataFrame()
@tool
def get_company_news(symbol: str) -> pd.DataFrame:
"""
Get company news and press releases for a given stock symbol.
Args:
symbol (str): The stock symbol.
Returns:
pd.DataFrame: DataFrame containing company news and press releases.
"""
try:
news = yf.Ticker(symbol).news
return news
except Exception as e:
print(f"Error fetching company news for {symbol}: {e}")
return pd.DataFrame()
@tool
def get_technical_indicators(symbol: str) -> pd.DataFrame:
"""
Get technical indicators for a given stock symbol.
Args:
symbol (str): The stock symbol.
Returns:
pd.DataFrame: DataFrame containing technical indicators.
"""
try:
indicators = yf.Ticker(symbol).history(period="max")
return indicators
except Exception as e:
print(f"Error fetching technical indicators for {symbol}: {e}")
return pd.DataFrame()
@tool
def get_company_profile(symbol: str) -> dict:
"""
Get company profile and overview for a given stock symbol.
Args:
symbol (str): The stock symbol.
Returns:
dict: Dictionary containing company profile and overview.
"""
try:
profile = yf.Ticker(symbol).info
return profile
except Exception as e:
print(f"Error fetching company profile for {symbol}: {e}")
return {}
def get_openai_tools() -> List[dict]:
functions = [
code_interpreter,
google_search_and_scrape,
get_current_stock_price,
get_company_news,
get_company_profile,
get_stock_fundamentals,
get_financial_statements,
get_key_financial_ratios,
get_analyst_recommendations,
get_dividend_data,
get_technical_indicators
]
tools = [convert_to_openai_tool(f) for f in functions]
return tools