|
| 1 | +from isek.tools.toolkit import Toolkit |
| 2 | +import efinance as ef |
| 3 | + |
| 4 | +import requests |
| 5 | +from bs4 import BeautifulSoup |
| 6 | + |
| 7 | + |
| 8 | +def get_stock_info(stock_code: str): |
| 9 | + """get base info of stock base on stock code""" |
| 10 | + |
| 11 | + return ef.stock.get_base_info(stock_code) |
| 12 | + |
| 13 | + |
| 14 | +def get_company_info(url: str): |
| 15 | + """fetch additional company details base this url""" |
| 16 | + headers = { |
| 17 | + "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" |
| 18 | + } |
| 19 | + |
| 20 | + try: |
| 21 | + response = requests.get(url, headers=headers) |
| 22 | + response.encoding = "utf-8" |
| 23 | + soup = BeautifulSoup(response.text, "html.parser") |
| 24 | + return soup.get_text(strip=True) |
| 25 | + except Exception: |
| 26 | + return "" |
| 27 | + |
| 28 | + |
| 29 | +# Create toolkit with debug enabled |
| 30 | +company_base_info_tools = Toolkit( |
| 31 | + name="stock_info_tool", |
| 32 | + tools=[get_stock_info, get_company_info], |
| 33 | + instructions="Fetch company information based on its stock code and the company's 'About' page or official website URL.", |
| 34 | + debug=True, |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +# Optionally, for demonstration, call list_functions and execute_function in debug mode |
| 39 | +if __name__ == "__main__": |
| 40 | + company_base_info_tools.list_functions() |
| 41 | + stock_info = company_base_info_tools.execute_function( |
| 42 | + "get_stock_info", stock_code="00020" |
| 43 | + ) |
| 44 | + company_info = company_base_info_tools.execute_function( |
| 45 | + "get_company_info", url="https://www.sensetime.com/cn/about-index#1" |
| 46 | + ) |
| 47 | + print("Stock Info:", stock_info) |
| 48 | + print("Company Info:", company_info) |
0 commit comments