Skip to content

Commit 0ee327c

Browse files
committed
refactor: 重构 config 模块
1 parent 48cf8e2 commit 0ee327c

File tree

13 files changed

+900
-317
lines changed

13 files changed

+900
-317
lines changed

docs/.vitepress/config.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default defineConfig({
3333
{
3434
text: '高级配置',
3535
items: [
36+
{ text: '配置系统详解', link: '/advanced/configuration' },
3637
{ text: '文档解析', link: '/advanced/document-processing' },
3738
{ text: '智能体', link: '/advanced/agents' },
3839
{ text: '品牌自定义', link: '/advanced/branding' },
@@ -42,10 +43,10 @@ export default defineConfig({
4243
{
4344
text: '更新日志',
4445
items: [
46+
{ text: '版本说明 v0.3', link: '/changelog/0.3-release-notes' },
4547
{ text: '路线图', link: '/changelog/roadmap' },
4648
{ text: '参与贡献', link: '/changelog/contributing' },
47-
{ text: '常见问题', link: '/changelog/faq' },
48-
{ text: '版本说明 v0.3', link: '/changelog/0.3-release-notes' }
49+
{ text: '常见问题', link: '/changelog/faq' }
4950
]
5051
}
5152
],

docs/advanced/configuration.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# 配置系统详解
2+
3+
## 概述
4+
5+
Yuxi-Know 从 v0.3.x 版本开始采用了全新的配置系统,基于 Pydantic BaseModel 和 TOML 格式,提供了类型安全、智能提示和选择性持久化等现代化特性。
6+
7+
## 架构设计
8+
9+
### 配置层次结构
10+
11+
```
12+
配置系统架构
13+
├── 默认配置 (代码定义)
14+
│ ├── src/config/static/models.py (模型配置)
15+
│ └── src/config/app.py (应用配置)
16+
├── 用户配置 (TOML 文件)
17+
│ └── saves/config/base.toml (仅保存用户修改)
18+
└── 环境变量 (运行时覆盖)
19+
└── .env 文件
20+
```
21+
22+
### 核心组件
23+
24+
#### 1. Config 类 (`src/config/app.py`)
25+
26+
主配置类,继承自 Pydantic BaseModel,提供:
27+
28+
- **类型验证**: 自动检查配置项类型
29+
- **默认值管理**: 内置合理的默认配置
30+
- **选择性持久化**: 仅保存用户修改的配置项
31+
- **向后兼容**: 支持旧的字典式访问方式
32+
33+
```python
34+
class Config(BaseModel):
35+
# 功能开关
36+
enable_reranker: bool = Field(default=False, description="是否开启重排序")
37+
enable_content_guard: bool = Field(default=False, description="是否启用内容审查")
38+
39+
# 模型配置
40+
default_model: str = Field(default="siliconflow/deepseek-ai/DeepSeek-V3.2-Exp")
41+
embed_model: str = Field(default="siliconflow/BAAI/bge-m3")
42+
43+
# 运行时状态 (不持久化)
44+
model_provider_status: dict[str, bool] = Field(exclude=True)
45+
```
46+
47+
#### 2. 模型配置类 (`src/config/static/models.py`)
48+
49+
定义了三种类型的模型配置:
50+
51+
- **ChatModelProvider**: 聊天模型提供商
52+
- **EmbedModelInfo**: 嵌入模型信息
53+
- **RerankerInfo**: 重排序模型信息
54+
55+
```python
56+
class ChatModelProvider(BaseModel):
57+
name: str = Field(..., description="提供商显示名称")
58+
url: str = Field(..., description="提供商文档或模型列表 URL")
59+
base_url: str = Field(..., description="API 基础 URL")
60+
default: str = Field(..., description="默认模型名称")
61+
env: str = Field(..., description="API Key 环境变量名")
62+
models: list[str] = Field(default_factory=list, description="支持的模型列表")
63+
```
64+
65+
添加配置:
66+
67+
68+
```python
69+
# 1. 在 DEFAULT_CHAT_MODEL_PROVIDERS 中添加
70+
"new-provider": ChatModelProvider(
71+
name="新提供商",
72+
url="https://provider.com/docs",
73+
base_url="https://api.provider.com/v1",
74+
default="default-model",
75+
env="NEW_PROVIDER_API_KEY",
76+
models=["model1", "model2"],
77+
),
78+
79+
# 2. 在 .env 中配置 API Key
80+
# NEW_PROVIDER_API_KEY=your_api_key
81+
82+
# 3. 重启服务或重新加载配置
83+
```
84+
85+
## 配置管理特性
86+
87+
系统只会保存用户修改过的配置项:
88+
89+
```python
90+
# 用户只修改了 enable_reranker
91+
config.enable_reranker = True
92+
config.save() # 只保存 enable_reranker 到 TOML 文件
93+
94+
# TOML 文件内容
95+
# enable_reranker = true
96+
```
97+
98+
### 默认模型配置 (`src/config/static/models.py`)
99+
100+
包含所有支持的模型提供商的默认配置,开发者可以直接修改此文件添加新的模型:
101+
102+
```python
103+
DEFAULT_CHAT_MODEL_PROVIDERS: dict[str, ChatModelProvider] = {
104+
"siliconflow": ChatModelProvider(
105+
name="SiliconFlow",
106+
url="https://cloud.siliconflow.cn/models",
107+
base_url="https://api.siliconflow.cn/v1",
108+
default="deepseek-ai/DeepSeek-V3.2-Exp",
109+
env="SILICONFLOW_API_KEY",
110+
models=[
111+
"deepseek-ai/DeepSeek-V3.2-Exp",
112+
"Qwen/Qwen3-235B-A22B-Instruct-2507",
113+
# ...
114+
],
115+
),
116+
# 更多提供商...
117+
}
118+
```
119+
120+
### 用户配置 (`saves/config/base.toml`)
121+
122+
只包含用户修改过的配置项,使用 TOML 格式:
123+
124+
```toml
125+
# 用户只修改了这些配置项
126+
enable_reranker = true
127+
default_agent_id = "MyCustomAgent"
128+
enable_content_guard = true
129+
130+
# 模型配置修改
131+
[model_names.siliconflow]
132+
models = [
133+
"deepseek-ai/DeepSeek-V3.2-Exp",
134+
"custom-model-name",
135+
]
136+
```
137+
138+
## 高级配置
139+
140+
### 动态配置更新
141+
142+
```python
143+
from src.config import config
144+
145+
# 更新配置
146+
config.enable_reranker = True
147+
config.default_agent_id = "CustomAgent"
148+
149+
# 更新模型列表
150+
config.model_names["siliconflow"].models.append("new-model")
151+
152+
# 保存配置
153+
config.save()
154+
155+
# 或者只保存特定提供商的模型配置
156+
config._save_models_to_file("siliconflow")
157+
```
158+
159+
### 配置验证
160+
161+
```python
162+
# 验证配置
163+
from src.config import config
164+
165+
# 检查模型提供商可用性
166+
for provider, status in config.model_provider_status.items():
167+
print(f"{provider}: {'' if status else ''}")
168+
169+
# 获取可用模型列表
170+
available_models = config.get_model_choices()
171+
available_embed_models = config.get_embed_model_choices()
172+
available_rerankers = config.get_reranker_choices()
173+
```
174+
175+
### 配置导出
176+
177+
```python
178+
# 导出完整配置(包含运行时状态)
179+
full_config = config.dump_config()
180+
181+
# 导出用户配置(仅保存到文件的部分)
182+
user_config = {
183+
field: getattr(config, field)
184+
for field in config._user_modified_fields
185+
}

docs/changelog/0.3-release-notes.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,56 @@ Yuxi-Know v0.3 是一个重要的里程碑版本,包含了多项架构重构
1818
cp .env.template .env
1919
```
2020

21+
### 配置文件管理调整
22+
23+
配置系统从 YAML 格式迁移到了基于 Pydantic BaseModel + TOML 的现代化配置系统。
24+
25+
| 项目 | v0.2.x | v0.3.x |
26+
|------|--------|--------|
27+
| 默认模型配置 | `src/config/static/models.yaml` | `src/config/static/models.py` |
28+
| 用户配置 | `saves/config/base.yaml` | `saves/config/base.toml` |
29+
| 配置格式 | YAML | Python 代码 + TOML |
30+
| 类型安全 | ❌ 无 | ✅ Pydantic 验证 |
31+
| IDE 支持 | ❌ 基础 | ✅ 完整智能提示 |
32+
| 持久化策略 | 全量保存 | 选择性保存 |
33+
34+
35+
示例:迁移自定义模型提供商
36+
37+
假设你的旧 `models.yaml` 中有:
38+
39+
```yaml
40+
# 旧的 models.yaml
41+
MODEL_NAMES:
42+
custom-provider:
43+
name: "My Custom Provider"
44+
base_url: "https://api.custom.com/v1"
45+
default: "custom-model"
46+
env: "CUSTOM_API_KEY"
47+
models:
48+
- "custom-model"
49+
- "another-model"
50+
```
51+
52+
需要在新的 `src/config/static/models.py` 中添加:
53+
54+
```python
55+
# 新的 models.py
56+
DEFAULT_CHAT_MODEL_PROVIDERS: dict[str, ChatModelProvider] = {
57+
# ... 现有配置 ...
58+
59+
"custom-provider": ChatModelProvider(
60+
name="My Custom Provider",
61+
url="https://custom.com/docs",
62+
base_url="https://api.custom.com/v1",
63+
default="custom-model",
64+
env="CUSTOM_API_KEY",
65+
models=["custom-model", "another-model"],
66+
),
67+
}
68+
```
69+
70+
2171
### 数据库存储架构重构
2272
- **变更内容**: 重新实现了对话管理的存储与管理,不再依赖于 MemorySaver
2373
- **影响**: 使用新的存储结构,之前存储的历史记录无法直接迁移

docs/changelog/roadmap.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
## Bugs
99

10-
- [ ] 当前 ReAct 智能体有消息顺序错乱的 bug,且不会默认调用工具
11-
10+
-
1211

1312
## Next
1413

@@ -20,7 +19,6 @@
2019
- [ ] 集成智能体评估,首先使用命令行来实现,然后考虑放在 UI 里面展示
2120
- [ ] 开发与生产环境隔离,构建生产镜像 <Badge type="info" text="0.4" />
2221
- [ ] 支持 MinerU 2.5 的解析方法 <Badge type="info" text="0.3.5" />
23-
- [ ] 优化全局配置的管理模型,优化配置管理
2422

2523
## Later
2624

@@ -34,3 +32,5 @@
3432
- [x] 添加测试脚本,覆盖最常见的功能(已覆盖API)
3533
- [x] 新建 tasker 模块,用来管理所有的后台任务,UI 上使用侧边栏管理。
3634
- [x] 优化对文档信息的检索展示(检索结果页、详情页)
35+
- [x] 当前 ReAct 智能体有消息顺序错乱的 bug,且不会默认调用工具
36+
- [x] 优化全局配置的管理模型,优化配置管理

0 commit comments

Comments
 (0)