This commit is contained in:
MarkLo 2025-11-26 15:28:36 +08:00
parent 3dcdbb5659
commit c93d118308
5 changed files with 11 additions and 89 deletions

View File

@ -277,9 +277,10 @@ class PDFGenerator:
# Remove lines that only contain markdown symbols
text = re.sub(r'^[\*_`~#\-\+]+\s*$', '', text, flags=re.MULTILINE)
# 12. Final Unicode check - remove any characters that might cause PDF encoding issues
# Keep only printable characters and common Chinese characters
text = ''.join(char for char in text if char.isprintable() or char in '\n\r\t' or '\u4e00' <= char <= '\u9fff')
# 12. REMOVED problematic Unicode filter that was corrupting Chinese characters
# The string comparison '\u4e00' <= char <= '\u9fff' was comparing UTF-8 bytes,
# not Unicode code points, causing characters like '經' to be corrupted.
# Unicode normalization at the start (line 237) is sufficient.
return text.strip()

View File

@ -46,24 +46,7 @@ def create_risky_debator(llm):
# 獲取交易員的決策
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 使用量
# 增加限制以確保 800+ 字的報告不被截斷
market_research_report = truncate_text(market_research_report, 2000)
sentiment_report = truncate_text(sentiment_report, 2000)
news_report = truncate_text(news_report, 2500)
fundamentals_report = truncate_text(fundamentals_report, 2000)
trader_decision = truncate_text(trader_decision, 2000)
history = truncate_text(history, 1500)
current_safe_response = truncate_text(current_safe_response, 1000)
current_neutral_response = truncate_text(current_neutral_response, 1000)
# 移除截斷邏輯以保留完整報告內容
# 建立提示 (prompt)
prompt = f"""**重要您必須使用繁體中文Traditional Chinese回覆所有內容。**

View File

@ -47,24 +47,7 @@ def create_safe_debator(llm):
# 獲取交易員的決策
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 使用量
# 增加限制以確保 800+ 字的報告不被截斷
market_research_report = truncate_text(market_research_report, 2000)
sentiment_report = truncate_text(sentiment_report, 2000)
news_report = truncate_text(news_report, 2500)
fundamentals_report = truncate_text(fundamentals_report, 2000)
trader_decision = truncate_text(trader_decision, 2000)
history = truncate_text(history, 1500)
current_risky_response = truncate_text(current_risky_response, 1000)
current_neutral_response = truncate_text(current_neutral_response, 1000)
# 移除截斷邏輯以保留完整報告內容
# 建立提示 (prompt)
prompt = f"""**重要您必須使用繁體中文Traditional Chinese回覆所有內容。**

View File

@ -46,24 +46,7 @@ def create_neutral_debator(llm):
# 獲取交易員的決策
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 使用量
# 增加限制以確保 800+ 字的報告不被截斷
market_research_report = truncate_text(market_research_report, 2000)
sentiment_report = truncate_text(sentiment_report, 2000)
news_report = truncate_text(news_report, 2500)
fundamentals_report = truncate_text(fundamentals_report, 2000)
trader_decision = truncate_text(trader_decision, 2000)
history = truncate_text(history, 1500)
current_risky_response = truncate_text(current_risky_response, 1000)
current_safe_response = truncate_text(current_safe_response, 1000)
# 移除截斷邏輯以保留完整報告內容
# 建立提示 (prompt)
prompt = f"""**重要您必須使用繁體中文Traditional Chinese回覆所有內容。**

View File

@ -39,47 +39,19 @@ def create_trader(llm, memory):
news_report = state["news_report"]
fundamentals_report = state["fundamentals_report"]
# 定義文本截斷函數以避免超過 token 限制
def truncate_text(text, max_chars):
"""智能截斷文本到指定字符數,在句子邊界處截斷"""
if len(text) <= max_chars:
return text
# 在max_chars附近尋找句子結束標記
truncated = text[:max_chars]
# 尋找最後一個句號、換行或逗號
for delimiter in ['', '\n', '', '', ' ']:
last_pos = truncated.rfind(delimiter)
if last_pos > max_chars * 0.8: # 至少保留80%的內容
return text[:last_pos + 1] + "\n\n...(為控制長度已精簡)"
# 如果找不到合適的分隔符,直接在字符處截斷
return truncated + "...(為控制長度已精簡)"
# 截斷各類報告以控制 token 使用量
# 增加限制以確保 800+ 字的報告不被截斷
market_research_report_truncated = truncate_text(market_research_report, 2000)
sentiment_report_truncated = truncate_text(sentiment_report, 2000)
news_report_truncated = truncate_text(news_report, 2500)
fundamentals_report_truncated = truncate_text(fundamentals_report, 2000)
investment_plan_truncated = truncate_text(investment_plan, 2000)
# 移除截斷邏輯以保留完整報告內容
# 整合當前情況(用於記憶檢索)
curr_situation = f"{market_research_report_truncated}\n\n{sentiment_report_truncated}\n\n{news_report_truncated}\n\n{fundamentals_report_truncated}"
curr_situation = f"{market_research_report}\n\n{sentiment_report}\n\n{news_report}\n\n{fundamentals_report}"
# 從記憶體中獲取過去相似情況的經驗
past_memories = memory.get_memories(curr_situation, n_matches=2)
# 將過去的經驗格式化為字串(限制長度)
# 將過去的經驗格式化為字串
past_memory_str = ""
if past_memories:
for i, rec in enumerate(past_memories, 1):
recommendation = rec["recommendation"]
# 限制每條記憶的長度
if len(recommendation) > 200:
recommendation = recommendation[:200] + "...(已截斷)"
past_memory_str += recommendation + "\n\n"
else:
past_memory_str = "找不到過去的記憶。"
@ -96,7 +68,7 @@ def create_trader(llm, memory):
3. **風險管理**設定清晰的進出場與停損參數確保風控到位
可用資訊
- 投資計畫{investment_plan_truncated}
- 投資計畫{investment_plan}
- 過去反思{past_memory_str}
輸出要求