diff --git a/api/chat_routes.py b/api/chat_routes.py index a31a7f3..4fc82ec 100644 --- a/api/chat_routes.py +++ b/api/chat_routes.py @@ -1564,6 +1564,18 @@ def select_images(contexts: List[Dict], query: str) -> List[Dict]: MAX_IMAGES = 2 MIN_SCORE = 3.0 + # 动态调整:当表格嵌入大量图片时,提升上限以展示完整内容 + _table_image_count = 0 + for _ctx in contexts: + _meta = _ctx.get('meta', {}) + if _meta.get('chunk_type') == 'table' and _meta.get('images_json'): + try: + _table_image_count += len(json.loads(_meta['images_json'])) + except (json.JSONDecodeError, TypeError): + pass + if _table_image_count > MAX_IMAGES: + MAX_IMAGES = min(_table_image_count, 15) # 上限 15 + # 获取检索结果中涉及的主要章节(只看前 3 个最相关的文本块) primary_sections = set() for ctx in contexts[:3]: @@ -1584,6 +1596,8 @@ def select_images(contexts: List[Dict], query: str) -> List[Dict]: for ctx in contexts: meta = ctx.get('meta', {}) chunk_type = meta.get('chunk_type', 'text') + s = None # P1 评分,用于 P1.5 继承 + doc = ctx.get('doc', '') # 处理图片类型和有关联图片的表格类型 if meta.get('image_path') and chunk_type in ('image', 'chart', 'table'): @@ -1696,6 +1710,11 @@ def select_images(contexts: List[Dict], query: str) -> List[Dict]: # ========== P1.5:处理表格切片的 images_json(跨页表格多图)========== # 当表格切片有 images_json 字段时,添加所有关联图片 if chunk_type == 'table' and meta.get('images_json'): + # 如果 P1 未执行(表格无 image_path),需要独立计算分数 + if s is None: + doc = ctx.get('image_description', '') or meta.get('vlm_desc', '') or ctx.get('doc', '') + meta['score'] = ctx.get('score', 0) + s = score_image_relevance(query, meta, doc) try: images_list = json.loads(meta['images_json']) for img_info in images_list: @@ -1709,9 +1728,7 @@ def select_images(contexts: List[Dict], query: str) -> List[Dict]: # 跳过已添加的图片(避免重复) existing_ids = {img['id'] for img in scored_images} if img_id and img_id not in existing_ids: - # 为关联图片计算分数(继承主表格分数,略低) - ctx_score = ctx.get("score", 0) - assoc_score = ctx_score - 1.0 if ctx_score >= MIN_SCORE else MIN_SCORE - 1.0 + assoc_score = s - 1.0 if s >= MIN_SCORE else MIN_SCORE - 1.0 if assoc_score >= MIN_SCORE: scored_images.append({ 'score': assoc_score,