crazyafro/financial_intelligence_agent
0
1import math2import datetime3from langchain.tools import tool4from langchain_openai import ChatOpenAI5from langchain_core.messages import HumanMessage6 7@tool8def calculator(expression: str) -> str:9 """Evaluate a mathematical expression. Supports standard math operations and functions like sqrt, log, pow. Example: '1500 * 0.15' or 'sqrt(144)'."""10 try:11 allowed_names = {k: v for k, v in math.__dict__.items() if not k.startswith("_")}12 result = eval(expression, {"__builtins__": {}}, allowed_names)13 return f"๐งฎ Result: {result}"14 except Exception as e:15 return f"Calculation error: {str(e)}"16 17@tool18def get_date_context(_: str = "") -> str:19 """Get the current date, time, day of the week, and quarter. Useful for providing time-aware financial or research context."""20 now = datetime.datetime.now()21 quarter = (now.month - 1) // 3 + 122 return (23 f"๐
Current Date: {now.strftime('%B %d, %Y')}\n"24 f" Time: {now.strftime('%H:%M:%S')}\n"25 f" Day: {now.strftime('%A')}\n"26 f" Quarter: Q{quarter} {now.year}"27 )28 29@tool30def summarize_text(text: str) -> str:31 """Summarize a long piece of text into a concise paragraph. Useful after gathering research or financial data."""32 try:33 llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)34 response = llm.invoke([HumanMessage(content=f"Summarize the following concisely in 3-5 sentences:\n\n{text}")])35 return f"๐ Summary:\n{response.content}"36 except Exception as e:37 return f"Summarization error: {str(e)}"