634 lines
12 KiB
Markdown
634 lines
12 KiB
Markdown
# 开发指南
|
||||
|
|
|
|||
|
|
**项目**: 催收 RAG 评估系统
|
|||
|
|
**版本**: v1.0.0
|
|||
|
|
**日期**: 2025-01-07
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 目录
|
|||
|
|
|
|||
|
|
1. [开发环境搭建](#开发环境搭建)
|
|||
|
|
2. [代码规范](#代码规范)
|
|||
|
|
3. [开发流程](#开发流程)
|
|||
|
|
4. [测试指南](#测试指南)
|
|||
|
|
5. [常见问题](#常见问题)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 开发环境搭建
|
|||
|
|
|
|||
|
|
### 1. 系统要求
|
|||
|
|
|
|||
|
|
- **操作系统**: Windows 10+, macOS 10.15+, Linux
|
|||
|
|
- **Python**: 3.9+
|
|||
|
|
- **Git**: 2.0+
|
|||
|
|
- **编辑器**: VS Code / PyCharm(推荐)
|
|||
|
|
|
|||
|
|
### 2. 克隆项目
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
git clone http://fn.maiplan.site:8418/hughhuang/cuishou-rag-v2.git
|
|||
|
|
cd cuishou-rag-v2
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 创建虚拟环境
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 使用 venv
|
|||
|
|
python -m venv venv
|
|||
|
|
|
|||
|
|
# 激活虚拟环境
|
|||
|
|
# Windows
|
|||
|
|
venv\Scripts\activate
|
|||
|
|
# macOS/Linux
|
|||
|
|
source venv/bin/activate
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 安装依赖
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 安装项目依赖
|
|||
|
|
pip install -r requirements.txt
|
|||
|
|
|
|||
|
|
# 安装开发依赖
|
|||
|
|
pip install black flake8 pytest pytest-cov mypy
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 5. 配置环境变量
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 复制配置模板
|
|||
|
|
cp .env.example .env
|
|||
|
|
|
|||
|
|
# 编辑 .env 文件,填入您的配置
|
|||
|
|
# OPENAI_API_BASE=https://api.openai.com/v1
|
|||
|
|
# OPENAI_API_KEY=your-api-key
|
|||
|
|
# OPENAI_MODEL=gpt-4o-mini
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 6. 验证安装
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 运行测试(待开发)
|
|||
|
|
pytest tests/
|
|||
|
|
|
|||
|
|
# 检查代码格式
|
|||
|
|
black --check src/ tests/
|
|||
|
|
flake8 src/ tests/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 代码规范
|
|||
|
|
|
|||
|
|
### 1. Python 风格指南
|
|||
|
|
|
|||
|
|
遵循 **PEP 8** 规范:
|
|||
|
|
|
|||
|
|
#### 命名规范
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 类名:大驼峰
|
|||
|
|
class RAGEvaluator:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
# 函数名:小写+下划线
|
|||
|
|
def evaluate_system():
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
# 常量:大写+下划线
|
|||
|
|
MAX_RETRY_COUNT = 3
|
|||
|
|
|
|||
|
|
# 私有变量:前缀下划线
|
|||
|
|
_internal_cache = {}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 代码格式
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 使用 Black 格式化(line-length=100)
|
|||
|
|
# 安装:pip install black
|
|||
|
|
# 运行:black src/ tests/
|
|||
|
|
|
|||
|
|
# 示例
|
|||
|
|
def long_function_name(
|
|||
|
|
parameter_one: str,
|
|||
|
|
parameter_two: int,
|
|||
|
|
parameter_three: Optional[Dict[str, Any]] = None,
|
|||
|
|
) -> Tuple[str, int]:
|
|||
|
|
"""函数文档字符串
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
parameter_one: 参数说明
|
|||
|
|
parameter_two: 参数说明
|
|||
|
|
parameter_three: 可选参数说明
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
返回值说明
|
|||
|
|
"""
|
|||
|
|
pass
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 文档字符串
|
|||
|
|
|
|||
|
|
使用 **Google 风格** 文档字符串:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def evaluate_rag_system(
|
|||
|
|
system_name: str,
|
|||
|
|
test_set: TestSet,
|
|||
|
|
metrics: List[str]
|
|||
|
|
) -> EvaluationResult:
|
|||
|
|
"""评估 RAG 系统性能
|
|||
|
|
|
|||
|
|
对指定的 RAG 系统使用给定的测试集进行评估,计算指定的评估指标。
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
system_name: RAG 系统名称(如 'coze', 'kbs')
|
|||
|
|
test_set: 测试集对象
|
|||
|
|
metrics: 要计算的评估指标列表
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
EvaluationResult: 包含所有评估指标的结果对象
|
|||
|
|
|
|||
|
|
Raises:
|
|||
|
|
ValueError: 如果 system_name 不支持
|
|||
|
|
ConnectionError: 如果无法连接到 RAG 系统
|
|||
|
|
|
|||
|
|
Example:
|
|||
|
|
>>> test_set = load_test_set('v3.1')
|
|||
|
|
>>> result = evaluate_rag_system('coze', test_set, ['faithfulness'])
|
|||
|
|
>>> print(result.metrics['faithfulness'])
|
|||
|
|
0.85
|
|||
|
|
"""
|
|||
|
|
pass
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 类型注解
|
|||
|
|
|
|||
|
|
使用 **类型注解** 提高代码可读性:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from typing import List, Dict, Optional, Tuple, Union
|
|||
|
|
|
|||
|
|
def process_results(
|
|||
|
|
results: List[EvaluationResult],
|
|||
|
|
threshold: float = 0.8
|
|||
|
|
) -> Dict[str, Union[float, int]]:
|
|||
|
|
"""处理评估结果"""
|
|||
|
|
pass
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 错误处理
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 使用具体的异常类型
|
|||
|
|
try:
|
|||
|
|
result = evaluate_system(system_name)
|
|||
|
|
except ConnectionError as e:
|
|||
|
|
logger.error(f"Failed to connect to {system_name}: {e}")
|
|||
|
|
raise
|
|||
|
|
except ValueError as e:
|
|||
|
|
logger.warning(f"Invalid parameter: {e}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
# 自定义异常
|
|||
|
|
class EvaluationError(Exception):
|
|||
|
|
"""评估过程中的错误"""
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
class InvalidTestSetError(EvaluationError):
|
|||
|
|
"""无效的测试集"""
|
|||
|
|
pass
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 5. 日志记录
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
import logging
|
|||
|
|
|
|||
|
|
logger = logging.getLogger(__name__)
|
|||
|
|
|
|||
|
|
def evaluate_system(system_name: str) -> EvaluationResult:
|
|||
|
|
"""评估系统"""
|
|||
|
|
logger.info(f"Starting evaluation for {system_name}")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
result = _do_evaluation(system_name)
|
|||
|
|
logger.info(f"Evaluation completed: {result.summary()}")
|
|||
|
|
return result
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.error(f"Evaluation failed: {e}", exc_info=True)
|
|||
|
|
raise
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 开发流程
|
|||
|
|
|
|||
|
|
### 1. Git 工作流
|
|||
|
|
|
|||
|
|
#### 分支策略
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 主分支
|
|||
|
|
master # 生产环境代码
|
|||
|
|
|
|||
|
|
# 功能分支
|
|||
|
|
feature/xxx # 新功能开发
|
|||
|
|
fix/xxx # Bug 修复
|
|||
|
|
hotfix/xxx # 紧急修复
|
|||
|
|
docs/xxx # 文档更新
|
|||
|
|
refactor/xxx # 代码重构
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### 开发流程
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 1. 创建功能分支
|
|||
|
|
git checkout -b feature/add-evaluation-engine
|
|||
|
|
|
|||
|
|
# 2. 开发并提交
|
|||
|
|
git add .
|
|||
|
|
git commit -m "feat: 添加评估引擎核心功能"
|
|||
|
|
|
|||
|
|
# 3. 推送到远程
|
|||
|
|
git push origin feature/add-evaluation-engine
|
|||
|
|
|
|||
|
|
# 4. 创建 Pull Request
|
|||
|
|
# 在 GitLab/GitHub 上创建 PR
|
|||
|
|
|
|||
|
|
# 5. 代码审查通过后合并
|
|||
|
|
git checkout master
|
|||
|
|
git merge feature/add-evaluation-engine
|
|||
|
|
git push origin master
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 提交规范
|
|||
|
|
|
|||
|
|
遵循 **约定式提交(Conventional Commits)**:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 格式
|
|||
|
|
<type>(<scope>): <subject>
|
|||
|
|
|
|||
|
|
<body>
|
|||
|
|
|
|||
|
|
<footer>
|
|||
|
|
|
|||
|
|
# 类型(type)
|
|||
|
|
feat: 新功能
|
|||
|
|
fix: Bug 修复
|
|||
|
|
docs: 文档更新
|
|||
|
|
style: 代码格式(不影响功能)
|
|||
|
|
refactor: 重构
|
|||
|
|
test: 测试相关
|
|||
|
|
chore: 构建/工具配置
|
|||
|
|
|
|||
|
|
# 示例
|
|||
|
|
feat(evaluator): 添加 RAGAs 评估器
|
|||
|
|
|
|||
|
|
实现基于 RAGAs 框架的评估器,支持以下指标:
|
|||
|
|
- Faithfulness
|
|||
|
|
- Answer Relevancy
|
|||
|
|
- Context Precision
|
|||
|
|
|
|||
|
|
Closes #123
|
|||
|
|
|
|||
|
|
# 简短提交
|
|||
|
|
fix: 修复测试集加载错误
|
|||
|
|
docs: 更新 README 安装说明
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 代码审查清单
|
|||
|
|
|
|||
|
|
提交 PR 前检查:
|
|||
|
|
|
|||
|
|
- [ ] 代码符合 PEP 8 规范
|
|||
|
|
- [ ] 添加了必要的文档字符串
|
|||
|
|
- [ ] 添加了类型注解
|
|||
|
|
- [ ] 编写了单元测试
|
|||
|
|
- [ ] 所有测试通过
|
|||
|
|
- [ ] 更新了相关文档
|
|||
|
|
- [ ] 提交信息符合规范
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 测试指南
|
|||
|
|
|
|||
|
|
### 1. 测试结构
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
tests/
|
|||
|
|
├── __init__.py
|
|||
|
|
├── conftest.py # pytest 配置和 fixtures
|
|||
|
|
├── test_evaluators/ # 评估器测试
|
|||
|
|
│ ├── test_ragas.py
|
|||
|
|
│ └── test_custom.py
|
|||
|
|
├── test_analyzers/ # 分析器测试
|
|||
|
|
│ └── test_statistical.py
|
|||
|
|
├── test_reporters/ # 报告生成器测试
|
|||
|
|
│ └── test_html.py
|
|||
|
|
└── test_utils/ # 工具函数测试
|
|||
|
|
└── test_metrics.py
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 编写测试
|
|||
|
|
|
|||
|
|
#### 单元测试示例
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# tests/test_evaluators/test_ragas.py
|
|||
|
|
import pytest
|
|||
|
|
from src.evaluators.ragas import RAGEvaluator
|
|||
|
|
from src.models import TestSet, TestCase
|
|||
|
|
|
|||
|
|
class TestRAGEvaluator:
|
|||
|
|
"""RAG 评估器测试"""
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def test_set(self):
|
|||
|
|
"""测试集 fixture"""
|
|||
|
|
return TestSet(
|
|||
|
|
version="3.1",
|
|||
|
|
cases=[
|
|||
|
|
TestCase(
|
|||
|
|
question_id="q001",
|
|||
|
|
question="签约流程是什么?",
|
|||
|
|
ground_truth="...",
|
|||
|
|
category="positive"
|
|||
|
|
)
|
|||
|
|
]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
def evaluator(self, test_set):
|
|||
|
|
"""评估器 fixture"""
|
|||
|
|
return RAGEvaluator(system_name="coze", test_set=test_set)
|
|||
|
|
|
|||
|
|
def test_evaluate_returns_result(self, evaluator):
|
|||
|
|
"""测试评估返回结果"""
|
|||
|
|
result = evaluator.evaluate()
|
|||
|
|
assert result is not None
|
|||
|
|
assert hasattr(result, 'metrics')
|
|||
|
|
|
|||
|
|
def test_calculate_metrics(self, evaluator):
|
|||
|
|
"""测试指标计算"""
|
|||
|
|
metrics = evaluator.calculate_metrics()
|
|||
|
|
assert 'faithfulness' in metrics
|
|||
|
|
assert 0 <= metrics['faithfulness'] <= 1
|
|||
|
|
|
|||
|
|
@pytest.mark.parametrize("metric_name,expected_range", [
|
|||
|
|
("faithfulness", (0, 1)),
|
|||
|
|
("answer_relevancy", (0, 1)),
|
|||
|
|
])
|
|||
|
|
def test_metric_ranges(self, evaluator, metric_name, expected_range):
|
|||
|
|
"""测试指标范围"""
|
|||
|
|
metrics = evaluator.calculate_metrics()
|
|||
|
|
value = metrics[metric_name]
|
|||
|
|
assert expected_range[0] <= value <= expected_range[1]
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### Mock 外部依赖
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from unittest.mock import Mock, patch
|
|||
|
|
|
|||
|
|
def test_llm_refiner_with_mock():
|
|||
|
|
"""测试 LLM 精修器(使用 mock)"""
|
|||
|
|
# Mock LLM 客户端
|
|||
|
|
mock_client = Mock()
|
|||
|
|
mock_client.chat.completions.create.return_value = Mock(
|
|||
|
|
choices=[Mock(message=Mock(content='{"selected_faqs": [...]}'))]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
refiner = LLMRefiner(llm_client=mock_client)
|
|||
|
|
result = refiner.refine_ground_truth("问题", [])
|
|||
|
|
|
|||
|
|
assert result is not None
|
|||
|
|
mock_client.chat.completions.create.assert_called_once()
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 运行测试
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 运行所有测试
|
|||
|
|
pytest
|
|||
|
|
|
|||
|
|
# 运行特定文件
|
|||
|
|
pytest tests/test_evaluators/test_ragas.py
|
|||
|
|
|
|||
|
|
# 运行特定测试
|
|||
|
|
pytest tests/test_evaluators/test_ragas.py::TestRAGEvaluator::test_evaluate_returns_result
|
|||
|
|
|
|||
|
|
# 查看覆盖率
|
|||
|
|
pytest --cov=src --cov-report=html
|
|||
|
|
|
|||
|
|
# 详细输出
|
|||
|
|
pytest -v
|
|||
|
|
|
|||
|
|
# 显示 print 输出
|
|||
|
|
pytest -s
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 测试覆盖率要求
|
|||
|
|
|
|||
|
|
- **核心逻辑**: 80%+
|
|||
|
|
- **工具函数**: 70%+
|
|||
|
|
- **整体覆盖率**: 75%+
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 常见问题
|
|||
|
|
|
|||
|
|
### 1. 环境问题
|
|||
|
|
|
|||
|
|
#### Q: pip install 失败
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 升级 pip
|
|||
|
|
python -m pip install --upgrade pip
|
|||
|
|
|
|||
|
|
# 使用国内镜像
|
|||
|
|
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### Q: 虚拟环境激活失败(Windows)
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 如果提示权限错误,以管理员身份运行 PowerShell
|
|||
|
|
Set-ExecutionPolicy RemoteSigned
|
|||
|
|
|
|||
|
|
# 然后激活
|
|||
|
|
venv\Scripts\activate
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 代码格式问题
|
|||
|
|
|
|||
|
|
#### Q: Black 和 Flake8 冲突
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 在 .flake8 配置文件中忽略 Black 格式化的规则
|
|||
|
|
[flake8]
|
|||
|
|
max-line-length = 100
|
|||
|
|
extend-ignore = E203, W503
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### Q: 如何自动格式化
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# VS Code 配置(.vscode/settings.json)
|
|||
|
|
{
|
|||
|
|
"python.formatting.provider": "black",
|
|||
|
|
"python.formatting.blackArgs": ["--line-length", "100"],
|
|||
|
|
"editor.formatOnSave": true
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 测试问题
|
|||
|
|
|
|||
|
|
#### Q: 测试运行很慢
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 并行运行测试
|
|||
|
|
pip install pytest-xdist
|
|||
|
|
pytest -n auto
|
|||
|
|
|
|||
|
|
# 只运行失败的测试
|
|||
|
|
pytest --lf
|
|||
|
|
|
|||
|
|
# 跳过慢速测试
|
|||
|
|
pytest -m "not slow"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### Q: Mock 不生效
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 确保 patch 路径正确
|
|||
|
|
# 错误:patch('openai.OpenAI')
|
|||
|
|
# 正确:patch('src.refiners.llm.OpenAI')
|
|||
|
|
|
|||
|
|
# 使用 where 参数
|
|||
|
|
from src.refiners import llm
|
|||
|
|
with patch.object(llm, 'OpenAI') as mock:
|
|||
|
|
pass
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. Git 问题
|
|||
|
|
|
|||
|
|
#### Q: 提交信息写错了
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 修改最后一次提交
|
|||
|
|
git commit --amend -m "正确的提交信息"
|
|||
|
|
|
|||
|
|
# 如果已经推送,需要强制推送(谨慎使用)
|
|||
|
|
git push --force-with-lease
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### Q: 合并冲突
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 1. 拉取最新代码
|
|||
|
|
git pull origin master
|
|||
|
|
|
|||
|
|
# 2. 解决冲突
|
|||
|
|
# 编辑冲突文件,保留需要的代码
|
|||
|
|
|
|||
|
|
# 3. 标记为已解决
|
|||
|
|
git add .
|
|||
|
|
git commit -m "fix: 解决合并冲突"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 开发工具推荐
|
|||
|
|
|
|||
|
|
### VS Code 扩展
|
|||
|
|
|
|||
|
|
- **Python**: 官方 Python 扩展
|
|||
|
|
- **Pylance**: 类型检查和智能提示
|
|||
|
|
- **Black Formatter**: 代码格式化
|
|||
|
|
- **autoDocstring**: 自动生成文档字符串
|
|||
|
|
- **GitLens**: Git 增强工具
|
|||
|
|
|
|||
|
|
### PyCharm 配置
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 设置 Black 为格式化工具
|
|||
|
|
# File -> Settings -> Tools -> Black
|
|||
|
|
# 勾选 "On code reformat" 和 "On save"
|
|||
|
|
|
|||
|
|
# 设置 pytest 为测试运行器
|
|||
|
|
# File -> Settings -> Tools -> Python Integrated Tools
|
|||
|
|
# Default test runner: pytest
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 最佳实践
|
|||
|
|
|
|||
|
|
### 1. 代码组织
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 好的做法
|
|||
|
|
from src.evaluators import RAGEvaluator
|
|||
|
|
from src.models import TestSet
|
|||
|
|
|
|||
|
|
evaluator = RAGEvaluator(system_name="coze", test_set=test_set)
|
|||
|
|
result = evaluator.evaluate()
|
|||
|
|
|
|||
|
|
# 避免
|
|||
|
|
from src.evaluators.ragas import RAGEvaluator, calculate_metrics, process_results
|
|||
|
|
# 太多导入,不清晰
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 错误处理
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 好的做法
|
|||
|
|
try:
|
|||
|
|
result = evaluate_system(system_name)
|
|||
|
|
except ConnectionError:
|
|||
|
|
logger.error(f"Failed to connect to {system_name}")
|
|||
|
|
raise
|
|||
|
|
except ValueError as e:
|
|||
|
|
logger.warning(f"Invalid parameter: {e}")
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
# 避免
|
|||
|
|
try:
|
|||
|
|
result = evaluate_system(system_name)
|
|||
|
|
except Exception:
|
|||
|
|
pass # 吞掉所有异常
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 配置管理
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 好的做法
|
|||
|
|
from src.utils.config import load_config
|
|||
|
|
|
|||
|
|
config = load_config()
|
|||
|
|
api_key = config.get('openai_api_key')
|
|||
|
|
|
|||
|
|
# 避免
|
|||
|
|
api_key = "sk-xxxxx" # 硬编码
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 参考资源
|
|||
|
|
|
|||
|
|
- **Python 官方文档**: https://docs.python.org/3/
|
|||
|
|
- **PEP 8 风格指南**: https://pep8.org/
|
|||
|
|
- **pytest 文档**: https://docs.pytest.org/
|
|||
|
|
- **Black 文档**: https://black.readthedocs.io/
|
|||
|
|
- **RAGAs 文档**: https://docs.ragas.io/
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
**文档版本**: v1.0.0
|
|||
|
|
**最后更新**: 2025-01-07
|
|||
|
|
**维护者**: Hugh Huang
|