123 lines
4.4 KiB
Python
123 lines
4.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
import time
|
||
import json
|
||
|
||
|
||
def create_risky_debator(llm):
|
||
"""
|
||
建立一個激進的風險辯論員節點。
|
||
|
||
這個節點在風險評估辯論中扮演激進派的角色。
|
||
其目標是積極倡導高回報、高風險的機會,強調大膽的策略和競爭優勢。
|
||
它會專注於潛在的上升空間,並挑戰保守和中立的觀點。
|
||
|
||
Args:
|
||
llm: 用於生成回應的語言模型。
|
||
|
||
Returns:
|
||
function: 一個代表激進辯論員節點的函式,可在 langgraph 中使用。
|
||
"""
|
||
|
||
def risky_node(state) -> dict:
|
||
"""
|
||
激進辯論員節點的執行函式。
|
||
|
||
Args:
|
||
state (dict): 當前的圖狀態。
|
||
|
||
Returns:
|
||
dict: 更新後的狀態,包含新的風險辯論狀態。
|
||
"""
|
||
# 從狀態中獲取風險辯論的相關資訊
|
||
risk_debate_state = state["risk_debate_state"]
|
||
history = risk_debate_state.get("history", "")
|
||
risky_history = risk_debate_state.get("risky_history", "")
|
||
|
||
# 獲取其他辯論者的最新回應
|
||
current_safe_response = risk_debate_state.get("current_safe_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"]
|
||
|
||
# 定義文本截斷函數以避免超過 token 限制
|
||
def truncate_text(text, max_chars):
|
||
"""截斷文本到指定字符數"""
|
||
if len(text) <= max_chars:
|
||
return text
|
||
return text[:max_chars] + "\n...(內容已截斷)"
|
||
|
||
# 截斷各類輸入以控制 token 使用量
|
||
# 模型限制: 8192 tokens,目標: < 3500 字符
|
||
market_research_report = truncate_text(market_research_report, 500)
|
||
sentiment_report = truncate_text(sentiment_report, 500)
|
||
news_report = truncate_text(news_report, 600)
|
||
fundamentals_report = truncate_text(fundamentals_report, 600)
|
||
trader_decision = truncate_text(trader_decision, 800)
|
||
history = truncate_text(history, 400)
|
||
current_safe_response = truncate_text(current_safe_response, 300)
|
||
current_neutral_response = truncate_text(current_neutral_response, 300)
|
||
|
||
# 建立提示 (prompt)
|
||
prompt = f"""**重要:您必須使用繁體中文(Traditional Chinese)回覆所有內容。**
|
||
|
||
【專業身份】
|
||
您是激進風險分析師,專注於高風險高回報機會。
|
||
|
||
【論證重點】
|
||
1. **上檔潛力**:量化最佳情境回報
|
||
2. **催化劑**:推動股價爆發的事件
|
||
3. **成長加速**:營收/盈利提速跡象
|
||
4. **反駁保守**:指出保守觀點錯失的機會
|
||
|
||
【可用資訊】
|
||
- 交易員計畫:{trader_decision}
|
||
- 各類報告:{market_research_report}, {sentiment_report}, {news_report}, {fundamentals_report}
|
||
- 辯論歷史:{history}
|
||
- 對手觀點:{current_safe_response}, {current_neutral_response}
|
||
|
||
【輸出要求】
|
||
**長度**:300-500字
|
||
**結構**:
|
||
1. 核心激進論點(80字)
|
||
2. 機會與催化劑(150字)
|
||
3. 反駁保守觀點(100字)
|
||
4. 投資建議(70字)
|
||
|
||
**注意**:
|
||
- 強調高回報機會
|
||
- 挑戰保守思維
|
||
- 直接回應對手
|
||
|
||
請提供積極進取的投資論證!"""
|
||
|
||
# 呼叫 LLM 生成回應
|
||
response = llm.invoke(prompt)
|
||
|
||
# 格式化論點
|
||
argument = f"激進分析師:{response.content}"
|
||
|
||
# 更新風險辯論狀態
|
||
new_risk_debate_state = {
|
||
"history": history + "\n" + argument,
|
||
"risky_history": risky_history + "\n" + argument,
|
||
"safe_history": risk_debate_state.get("safe_history", ""),
|
||
"neutral_history": risk_debate_state.get("neutral_history", ""),
|
||
"latest_speaker": "Risky", # 記錄最新的發言者
|
||
"current_risky_response": argument,
|
||
"current_safe_response": risk_debate_state.get("current_safe_response", ""),
|
||
"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 risky_node |