from patch_langmem import patch_langmem_for_python310 patch_langmem_for_python310() from langgraph.graph import StateGraph, MessagesState, START, END from langmem.short_term import SummarizationNode from langchain_core.messages.utils import count_tokens_approximately from langgraph.prebuilt.chat_agent_executor import AgentState from langgraph.checkpoint.memory import InMemorySaver from typing import Any import os from langchain.chat_models import init_chat_model from langgraph.prebuilt import create_react_agent from langgraph_supervisor import create_supervisor import custom_tools as custom_tools from dotenv import load_dotenv # load_dotenv() # # supervisor_prompt = os.getenv("SUPERVISOR_PROMPT") def build_graph(): a_debug = False model = init_chat_model("llama3-70b-8192", model_provider="groq") summarization_node = SummarizationNode(token_counter=count_tokens_approximately, model=model, max_tokens=2048, max_summary_tokens=1024, output_messages_key="llm_input_messages",) class State(AgentState): context: dict[str, Any] checkpointer = InMemorySaver() agent0= create_react_agent(model=model, tools= [custom_tools.stock_data_tool], prompt=("Give the company name to the stock tool produce the stock fundamentals. Summarize all the information in one paragraph in 200 words."), name="Stock Analyst", pre_model_hook= summarization_node, state_schema=State, checkpointer=checkpointer, debug=a_debug) agent1= create_react_agent(model=model, tools= [custom_tools.web_search], prompt='''You are a geek that can find anything from the internet. Do the research and provide the summery in 250 words and in only one paragraph.''', name="Web Surfer", pre_model_hook= summarization_node, state_schema=State, checkpointer=checkpointer, debug=a_debug) agent2= create_react_agent(model=model, tools= [custom_tools.reddit_search_tool], prompt='''You are a social media analyst. your job is to search social media and find out the sentiments of public on any topic. Do the research and provide the summery in 250 words and in only one paragraph.''', name="Social Media Analyst", pre_model_hook= summarization_node, state_schema=State, checkpointer=checkpointer, debug=a_debug) agent3= create_react_agent(model=model, tools= [custom_tools.tech_news_tool], prompt='''You are a Technology news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''', name="Tech Journalist", pre_model_hook= summarization_node, state_schema=State, checkpointer=checkpointer, debug=a_debug) agent4= create_react_agent(model=model, tools= [custom_tools.politics_news_tool], prompt='''You are a Politics news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''', name="Political Journalist", pre_model_hook= summarization_node, state_schema=State, checkpointer=checkpointer, debug=a_debug) agent5= create_react_agent(model=model, tools= [custom_tools.business_news_tool], prompt='''You are a Business news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''', name="Business Journalist", pre_model_hook= summarization_node, state_schema=State, checkpointer=checkpointer, debug=a_debug) agent6= create_react_agent(model=model, tools= [custom_tools.world_news_tool], prompt='''You are a Geopolitical news analyst. You find the latest news related to a topic and summerize it to findout the overall picture. Do the research and provide the summery in 250 words and in only one paragraph.''', name="Geopolitical Journalist", pre_model_hook= summarization_node, state_schema=State, checkpointer=checkpointer, debug=a_debug) supervisor_prompt = '''You are a stock market research supervisor based in the United Kingdom. You lead a team of expert agents, each specializing in a specific domain. Your job is to orchestrate these agents to compile a comprehensive report based on the user's stock-related question. IMPORTANT: You DO NOT analyze anything yourself — your role is to build a strategy, decide when and whom to call, and summarize their findings. --- Agents: 1. Stock Analyst: Use for fundamental and technical data (stock price, volume, P/E ratio, etc.). 2. Web Surfer: Use to fetch online data about: - the company - the CEO - the product/service - the competitors 3. Social Media Analyst: Analyze sentiment on: - company - CEO - product/service 4. Tech Journalist: Find recent innovations or disruptions in tech sectors. 5. Business Journalist: Find relevant UK/sector business trends or economic insights. 6. Political Journalist: Understand policy/regulation affecting the stock (UK preferred). 7. Geopolitical Journalist: Understand global events (e.g., war, trade, disasters). --- Strategy: Step 1: Start by calling the Stock Analyst to gather basic fundamentals and technicals for the queried company. Step 2: Formulate a strategy for the next section of the report. Use the Web Surfer and Social Media Analyst iteratively: - First get company overview → then CEO → then product(s) → then competitors. - For each competitor found, decide if a deeper investigation (stock, CEO, product sentiment) is needed. Step 3: Once the company and competitor data are ready, call each of the Journalists: - Use each journalist one at a time, you need them to get the latest news in the sector, the company, the war, or their involvement in sports and politics, to find the connection and get a better picture. Step 4: After each agent call, evaluate whether the report section is complete. If not, call the same agent again with refined queries. Step 5: Continue until you have all the data needed for the full report. --- Report Format: # Report on [company or stock name]: ## Contents: ### Executive Summary: One-paragraph summary of insights and recommendation. ### Company Fundamentals: Table with all stock metrics and trends. ### Public Sentiments: Topics the public is discussing. - Public Excitement: Topic(s) the public supports. - Public Rage: Topic(s) receiving negative sentiment. ### CEO and Product Sentiment: Public opinions about the CEO and main products. ### Competitor Analysis: - For each key competitor: - Fundamentals - CEO image - Product overview and sentiment ### Overall Comparison: - Table with percentage-based comparison (0-100%) across companies for fundamentals, sentiment, and innovation. ### Final Recommendation: - Clear "Yes" or "No" answer to the user's question, based on full analysis. --- Optimize calls: - Call only the minimum agents required to fill each section of the report. - For each step, stop and plan before continuing. Do not proceed blindly. Your output should contain intermediate decisions, the agent/tool called, the query issued, and the response. After all tools are used, generate the final report. ''' graph = create_supervisor(agents=[agent0,agent1,agent2,agent3,agent4,agent5,agent6], model=model, prompt= supervisor_prompt) return graph.compile()