修改了refer模块

This commit is contained in:
jinye_huang 2025-07-11 16:45:11 +08:00
parent b6910ee15b
commit 18d60cae93
2 changed files with 9 additions and 3 deletions

View File

@ -17,6 +17,7 @@ from typing import Dict, Any, Optional, List, cast
from pathlib import Path from pathlib import Path
import mysql.connector import mysql.connector
from mysql.connector import pooling from mysql.connector import pooling
import random
from core.config import ConfigManager, ResourceConfig from core.config import ConfigManager, ResourceConfig
from utils.prompts import BasePromptBuilder, PromptTemplate from utils.prompts import BasePromptBuilder, PromptTemplate
@ -341,10 +342,15 @@ class PromptService:
full_path = self._get_full_path(path_str) full_path = self._get_full_path(path_str)
if full_path.exists() and full_path.is_file(): if full_path.exists() and full_path.is_file():
content = full_path.read_text('utf-8') with open(full_path, 'r', encoding='utf-8') as f:
refer_content += f"--- {full_path.name} ---\n{content}\n\n" 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: except Exception as e:
logger.error(f"读取参考文件失败 {ref_item.path}: {e}") logger.error(f"读取或采样参考文件失败 {ref_item.path}: {e}")
except Exception as e: except Exception as e:
logger.error(f"获取参考内容失败: {e}") logger.error(f"获取参考内容失败: {e}")