AI_Stock_Prediction / tools /company_profile_tool.py
dure-waseem's picture
initial code
a189e3e
from crewai.tools import tool
import finnhub
import os
import json
@tool("get_company_profile")
def get_company_profile(company_symbol: str) -> str:
"""
Retrieves company profile information from Finnhub including country, currency,
exchange, industry, IPO date, market cap, name, phone, shares outstanding,
ticker, and website.
Args:
company_symbol: str -> the ticker symbol of the company (e.g., 'AAPL', 'GOOGL')
"""
try:
# Initialize Finnhub client
finnhub_client = finnhub.Client(api_key=os.getenv("FINNHUB_API_KEY"))
# Get company profile
profile = finnhub_client.company_profile2(symbol=company_symbol)
if not profile:
return f"No company profile found for symbol: {company_symbol}"
# Format the profile data for better readability
formatted_profile = {
"Company Name": profile.get("name", "N/A"),
"Ticker Symbol": profile.get("ticker", "N/A"),
"Country": profile.get("country", "N/A"),
"Currency": profile.get("currency", "N/A"),
"Exchange": profile.get("exchange", "N/A"),
"Industry": profile.get("finnhubIndustry", "N/A"),
"IPO Date": profile.get("ipo", "N/A"),
"Market Capitalization": f"${profile.get('marketCapitalization', 0):,}" if profile.get('marketCapitalization') else "N/A",
"Shares Outstanding": f"{profile.get('shareOutstanding', 0):,}" if profile.get('shareOutstanding') else "N/A",
"Phone": profile.get("phone", "N/A"),
"Website": profile.get("weburl", "N/A"),
"Logo": profile.get("logo", "N/A")
}
# Convert to JSON string for better formatting
return json.dumps(formatted_profile, indent=2)
except Exception as e:
return f"Error retrieving company profile for {company_symbol}: {str(e)}"