from langchain_core.messages import AIMessage import time import json def create_safe_debator(llm): def safe_node(state) -> dict: risk_debate_state = state["risk_debate_state"] history = risk_debate_state.get("history", "") safe_history = risk_debate_state.get("safe_history", "") current_risky_response = risk_debate_state.get("current_risky_response", "") current_neutral_response = risk_debate_state.get("current_neutral_response", "") market_research_report = state["market_report"] sentiment_report = state["sentiment_report"] news_report = state["news_report"] fundamentals_report = state["fundamentals_report"] trader_decision = state["trader_investment_plan"] prompt = f"""As the Safe/Conservative Risk Analyst for SHORT-TERM trading (1-2 week horizon), your primary objective is to protect assets from short-term volatility and sudden market moves. You prioritize stability and risk mitigation, carefully assessing potential losses from near-term events, earnings surprises, and market volatility over the next 1-2 weeks. When evaluating the trader's position decision (LONG/SHORT/HOLD), critically examine high-risk elements for the short term, pointing out where the decision may expose the firm to undue near-term risk. Here is the trader's position decision: {trader_decision} Your task is to actively counter the arguments of the Risky and Neutral Analysts, highlighting where their views may overlook potential short-term threats or near-term downside risks. Respond directly to their points, drawing from the following data sources to build a convincing case for a cautious short-term approach: Market Research Report: {market_research_report} Social Media Sentiment Report: {sentiment_report} Latest World Affairs Report: {news_report} Company Fundamentals Report: {fundamentals_report} Here is the current conversation history: {history} Here is the last response from the risky analyst: {current_risky_response} Here is the last response from the neutral analyst: {current_neutral_response}. If there are no responses from the other viewpoints, do not halluncinate and just present your point. Engage by questioning their optimism and emphasizing the potential short-term downsides they may have overlooked. Address each of their counterpoints to showcase why a conservative stance is the safest path for the next 1-2 weeks. Focus on debating and critiquing their arguments to demonstrate the strength of a risk-aware strategy for short-term trading. Output conversationally as if you are speaking without any special formatting.""" response = llm.invoke(prompt) argument = f"Safe Analyst: {response.content}" new_risk_debate_state = { "history": history + "\n" + argument, "risky_history": risk_debate_state.get("risky_history", ""), "safe_history": safe_history + "\n" + argument, "neutral_history": risk_debate_state.get("neutral_history", ""), "latest_speaker": "Safe", "current_risky_response": risk_debate_state.get( "current_risky_response", "" ), "current_safe_response": argument, "current_neutral_response": risk_debate_state.get( "current_neutral_response", "" ), "count": risk_debate_state["count"] + 1, } return {"risk_debate_state": new_risk_debate_state} return safe_node