diff --git a/api/chat_routes.py b/api/chat_routes.py
index 40a4db1..a31a7f3 100644
--- a/api/chat_routes.py
+++ b/api/chat_routes.py
@@ -26,6 +26,7 @@ Example:
import json
import os
import queue
+import re
import threading
import time as _time
from typing import List, Dict, Any, Optional, Tuple
@@ -1244,6 +1245,62 @@ _FIGURE_ANSWER_KEYWORDS = frozenset([
])
+def _replace_table_image_placeholders(answer: str, images: List[Dict]) -> str:
+ """
+ 将回答中 markdown 表格内的图片占位符替换为实际图片语法。
+
+ LLM 看到的上下文里表格单元格图片以 [图片] 或 [N张图片](MinerU 解析产物)表示,
+ LLM 通常会原样输出,有时还会加上  格式。
+
+ 此函数按顺序将占位符映射到 images 列表中的 URL,
+ 生成  语法,前端 markdown-it 可直接渲染为 标签。
+
+ 处理的占位符格式:
+ - [图片] →  (单张)
+ -  →  (LLM 加了 ! 和假 URL)
+ - [3张图片] →   
+ -  → 同上 (LLM 加了前缀和假 URL)
+
+ Args:
+ answer: LLM 生成的回答文本
+ images: select_images + _filter_images_by_answer 后的最终图片列表
+
+ Returns:
+ 替换后的回答文本
+ """
+ if not images or '图片' not in answer:
+ return answer
+
+ img_iter = iter(images)
+ replaced_count = 0
+
+ def _replacer(match):
+ nonlocal replaced_count
+ n_str = match.group(1) # 数字部分,None 表示单张
+ n = int(n_str) if n_str else 1
+
+ parts = []
+ for _ in range(n):
+ try:
+ img = next(img_iter)
+ url = img.get('url', '')
+ img_id = img.get('id', 'img')
+ parts.append(f'')
+ replaced_count += 1
+ except StopIteration:
+ parts.append('[图片]') # 图片用完了
+ return ' '.join(parts)
+
+ # !? — 可选前导 !(LLM 有时加)
+ # \[(\d+)?张?图片\] — [图片] 或 [3张图片](张 可选)
+ # (?:\((?:图片URL|https?://[^)]+)\))? — 可选的 LLM 从上下文复制的假 URL
+ # 匹配 (图片URL) 或 (https://via.placeholder.com/150) 等
+ result = re.sub(r'!?\[(\d+)?张?图片\](?:\((?:图片URL|https?://[^)]+)\))?', _replacer, answer)
+ if replaced_count > 0:
+ logger.info(f"[图片占位符替换] 替换了 {replaced_count}/{len(images)} 个图片为 markdown 图片语法")
+ return result
+
+
def _filter_images_by_answer(selected_images: List[Dict], answer: str, query: str = "",
primary_sections: List[str] = None) -> List[Dict]:
"""
@@ -2623,6 +2680,10 @@ def rag():
# 9. 过滤敏感信息(违禁词等)
filtered_answer = filter_response(citation_result.get("answer_with_refs", clean_answer))
+ # 9.5 替换表格内 [图片] 占位符为 markdown 图片语法
+ if rich_media.get("images"):
+ filtered_answer = _replace_table_image_placeholders(filtered_answer, rich_media["images"])
+
# 9. 保存消息到会话(仅开发环境)
if session_id and session_repo_ref:
try:
diff --git a/knowledge/manager.py b/knowledge/manager.py
index 150d301..b5c8642 100644
--- a/knowledge/manager.py
+++ b/knowledge/manager.py
@@ -39,6 +39,7 @@ from pathlib import Path
import logging
import chromadb
+from bs4 import BeautifulSoup
# 从 base.py 导入基础类和常量
from .base import (
@@ -550,8 +551,39 @@ class KnowledgeBaseManager(
curr_html = getattr(current, 'table_html', '') or ''
next_html = getattr(next_chunk, 'table_html', '') or ''
if curr_html and next_html:
- # 合并两个表格的 HTML
- current.table_html = curr_html + '\n' + next_html
+ # 正确合并两个表格的 HTML:
+ # 将第二个表格的