diff --git a/api/services/__pycache__/prompt_service.cpython-312.pyc b/api/services/__pycache__/prompt_service.cpython-312.pyc index 9081add..a997d0c 100644 Binary files a/api/services/__pycache__/prompt_service.cpython-312.pyc and b/api/services/__pycache__/prompt_service.cpython-312.pyc differ diff --git a/api/services/prompt_service.py b/api/services/prompt_service.py index 26b1f9c..d514420 100644 --- a/api/services/prompt_service.py +++ b/api/services/prompt_service.py @@ -17,6 +17,7 @@ from typing import Dict, Any, Optional, List, cast from pathlib import Path import mysql.connector from mysql.connector import pooling +import random from core.config import ConfigManager, ResourceConfig from utils.prompts import BasePromptBuilder, PromptTemplate @@ -341,10 +342,15 @@ class PromptService: full_path = self._get_full_path(path_str) if full_path.exists() and full_path.is_file(): - content = full_path.read_text('utf-8') - refer_content += f"--- {full_path.name} ---\n{content}\n\n" + with open(full_path, 'r', encoding='utf-8') as f: + lines = f.readlines() + if lines: + sample_size = max(1, int(len(lines) * ref_item.sampling_rate)) + sampled_lines = random.sample(lines, sample_size) + sampled_content = ''.join(sampled_lines) + refer_content += f"--- {full_path.name} (sampled {ref_item.sampling_rate * 100}%) ---\n{sampled_content}\n\n" except Exception as e: - logger.error(f"读取参考文件失败 {ref_item.path}: {e}") + logger.error(f"读取或采样参考文件失败 {ref_item.path}: {e}") except Exception as e: logger.error(f"获取参考内容失败: {e}")