Song commited on
Commit
84034c4
·
1 Parent(s): 04a323f
app.py CHANGED
@@ -172,7 +172,7 @@ elif active_tab == 'AI Report':
172
  if not config_valid:
173
  st.warning('⚠️ AI report functionality is not configured. Set environment variables to enable it.')
174
  else:
175
- # Move AI settings here from sidebar
176
  col_lang, col_depth = st.columns(2)
177
  with col_lang:
178
  report_lang = st.selectbox('Language / 報告語言', ['Chinese', 'English', 'Japanese'], key='lang_select')
@@ -181,6 +181,14 @@ elif active_tab == 'AI Report':
181
  depth_map = {'Short (300 words)': 300, 'Standard (800 words)': 800, 'Detailed (1500 words)': 1500}
182
  selected_length = depth_map[report_depth]
183
 
 
 
 
 
 
 
 
 
184
  if st.button('🤖 Generate Professional Report / 生成專業報告', help='點擊生成基於AI的專業SDG分析報告'):
185
  try:
186
  with st.spinner('AI is analyzing SDG data and generating strategic insights...'):
@@ -188,6 +196,7 @@ elif active_tab == 'AI Report':
188
  'country': selected_country,
189
  'start_year': year_range[0],
190
  'end_year': year_range[1],
 
191
  'latest_score': f"{metrics.get('score', 'N/A'):.1f}" if isinstance(metrics.get('score'), (int, float)) else metrics.get('score', 'N/A'),
192
  'rank': metrics.get('rank', 'N/A'),
193
  'total_countries': metrics.get('country_count', 'N/A'),
 
172
  if not config_valid:
173
  st.warning('⚠️ AI report functionality is not configured. Set environment variables to enable it.')
174
  else:
175
+ # AI Settings Layout
176
  col_lang, col_depth = st.columns(2)
177
  with col_lang:
178
  report_lang = st.selectbox('Language / 報告語言', ['Chinese', 'English', 'Japanese'], key='lang_select')
 
181
  depth_map = {'Short (300 words)': 300, 'Standard (800 words)': 800, 'Detailed (1500 words)': 1500}
182
  selected_length = depth_map[report_depth]
183
 
184
+ # Automatically determine Report Type based on Depth
185
+ if selected_length == 1500:
186
+ selected_type = 'professional' # Detailed -> Professional Report
187
+ elif selected_length == 800:
188
+ selected_type = 'policy' # Standard -> Policy Brief
189
+ else:
190
+ selected_type = 'summary' # Short -> Executive Summary
191
+
192
  if st.button('🤖 Generate Professional Report / 生成專業報告', help='點擊生成基於AI的專業SDG分析報告'):
193
  try:
194
  with st.spinner('AI is analyzing SDG data and generating strategic insights...'):
 
196
  'country': selected_country,
197
  'start_year': year_range[0],
198
  'end_year': year_range[1],
199
+ 'report_type': selected_type, # Auto-selected based on depth
200
  'latest_score': f"{metrics.get('score', 'N/A'):.1f}" if isinstance(metrics.get('score'), (int, float)) else metrics.get('score', 'N/A'),
201
  'rank': metrics.get('rank', 'N/A'),
202
  'total_countries': metrics.get('country_count', 'N/A'),
src/__pycache__/ai_engine.cpython-313.pyc CHANGED
Binary files a/src/__pycache__/ai_engine.cpython-313.pyc and b/src/__pycache__/ai_engine.cpython-313.pyc differ
 
src/ai_engine.py CHANGED
@@ -510,21 +510,20 @@ class EnhancedAIReportEngine:
510
 
511
  system_prompt = f"""
512
  您是聯合國永續發展解決方案網路(SDSN)的首席環境經濟學家與頂級 AI 策略專家。
513
- 您的任務是為 {meta.country} 撰寫一份極具深度、數據驅動且具備戰略前瞻性的 SDG 評估報告。
514
 
515
  報告要求:
516
- 1. **度與度**:不僅要分析總體得分,還要深入探討 17 個目標中的關鍵亮點與瓶頸
517
- 2. **數據驅動**:必須引用提供的趨勢數據、排名以及與全球平均的對比進行批判性分析。
518
- 3. **專業口吻**:使用高級經濟學與政策分析術語,確保報告具有權威性
519
- 4. **結構化**:使用 Markdown 標題、清單表格來增強讀性
520
- 5. **長度**:這是一份詳盡的報告,請確保內容充實,不要過早結束,目標字數約 {meta.length.value} 字。
 
 
521
  6. **語言**:完全使用 {meta.language.value} 撰寫。
522
  7. **結尾標記**:請在報告最後一行加上「【報告結束】」以示完整。
523
 
524
- 您的分析應涵蓋:
525
- - 過去十年的歷史軌跡與轉折點。
526
- - 具體的目標績效評估(SDG 1-17 的亮點與紅字)。
527
- - 政策建議:針對現有數據缺口或落後目標,提出具體的改善路徑。
528
  """
529
 
530
  # 用戶提示詞
@@ -545,18 +544,13 @@ class EnhancedAIReportEngine:
545
  ### 區域比較
546
  {json.dumps(comparison, indent=2, ensure_ascii=False)}
547
 
548
- ## 報告結構要求
549
- 請按照以下結構生成報告
550
- {chr(10).join([f"{i+1}. {section}" for i, section in enumerate(ReportTemplate.get_structure(meta.report_type))])}
551
-
552
- ## 特殊要求
553
- - 報告類型:{meta.report_type.value}
554
  - 語言:{meta.language.value}
555
- - 長度: {meta.length.value} 字
556
- - 包含數據引用和具體建議
557
- - 適合 {style_guide['focus']} 目標受眾
558
 
559
- 請開始生成報告。
560
  """
561
 
562
  return system_prompt, user_prompt
@@ -738,8 +732,13 @@ class EnhancedAIReportEngine:
738
  end_marker = "【報告結束】"
739
  if end_marker not in report_content:
740
  logger.warning(f"Report for {meta.country} may be truncated (marker not found)")
741
- truncation_notice = "\n\n> ⚠️ **(註:因模型輸出長度限制,報告內容可能未完顯示,請選擇較短報告類型或聯繫管理員)**"
742
- report_content += truncation_notice
 
 
 
 
 
743
  else:
744
  # 移除結束標記,保持報告美觀
745
  report_content = report_content.replace(end_marker, "").strip()
 
510
 
511
  system_prompt = f"""
512
  您是聯合國永續發展解決方案網路(SDSN)的首席環境經濟學家與頂級 AI 策略專家。
513
+ 您的任務是為 {meta.country} 撰寫一份數據驅動且具備戰略前瞻性的 SDG 評估報告。
514
 
515
  報告要求:
516
+ 1. **度與專業度**:分析總體得分,並針對各項目標進行深入探討。
517
+ 2. **數據驅動**:必須廣泛引用提供的趨勢數據、排名以及與全球平均的對比進行量化分析。
518
+ 3. **專業口吻**:使用權威性的政策分析術語(如:Decoupling, Circular Economy, Carbon Neutrality 等)
519
+ 4. **結構化**:使用 Markdown 標題、清單表格。**長度必須與要求相符,不敷衍**
520
+ 5. **長度要求**:這是一份約 {meta.length.value} 字/字符的報告
521
+ - 如果是 1500 字「Detailed」報告,請務必提供極具深度的細節分析,涵蓋多個學科視角。
522
+ - 如果是 300 字「Short」報告,請保持極度精煉。
523
  6. **語言**:完全使用 {meta.language.value} 撰寫。
524
  7. **結尾標記**:請在報告最後一行加上「【報告結束】」以示完整。
525
 
526
+ 您的分析應根據報告類型 ({meta.report_type.value}) 提供相應的深度,特別是針對最新的 SDR 2025 數據進行解讀。
 
 
 
527
  """
528
 
529
  # 用戶提示詞
 
544
  ### 區域比較
545
  {json.dumps(comparison, indent=2, ensure_ascii=False)}
546
 
547
+ ## 報告要求
548
+ - 結構:{', '.join(ReportTemplate.get_structure(meta.report_type))}
 
 
 
 
549
  - 語言:{meta.language.value}
550
+ - 目標長度:**嚴格限制在 {meta.length.value} 字以內**。
551
+ - 如果是 300 字版本,請合併部分章節,保持簡潔明瞭,避免冗長描述。
 
552
 
553
+ 請開始生成報告,並以「【報告結束】」結尾
554
  """
555
 
556
  return system_prompt, user_prompt
 
732
  end_marker = "【報告結束】"
733
  if end_marker not in report_content:
734
  logger.warning(f"Report for {meta.country} may be truncated (marker not found)")
735
+ # 調截斷判斷邏輯:
736
+ # 對於日文/中文,1500「字」通常指字符。
737
+ # 只有當內容長度非常接近 max_tokens 限制或明顯異常時才顯示
738
+ # 這裡調高閾值到 3 倍,避免誤報
739
+ if len(report_content) > meta.length.value * 3:
740
+ truncation_notice = "\n\n> ⚠️ **(註:因模型輸出長度限制,報告內容可能未完整顯示,請選擇較短報告類型或聯繫管理員)**"
741
+ report_content += truncation_notice
742
  else:
743
  # 移除結束標記,保持報告美觀
744
  report_content = report_content.replace(end_marker, "").strip()