8303cea018
- Add enable_semantic_segmentation: bool field with default=True - Allow override via KB_ENABLE_SEMANTIC_SEGMENTATION env var - Pydantic automatically parses boolean strings (true/false, yes/no, 1/0) - Update docstring to document new field CONFIG-01: LLM segmentation can be enabled/disabled via configuration Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
"""Configuration management for KB Pipeline
|
|
|
|
Provides centralized configuration using pydantic-settings.
|
|
Configuration is loaded from ~/.kb_env file and environment variables.
|
|
"""
|
|
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
from pydantic import Field, SecretStr
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
class KBSettings(BaseSettings):
|
|
"""KB 工作流配置
|
|
|
|
使用 pydantic-settings 从环境变量和 ~/.kb_env 文件加载配置。
|
|
|
|
Attributes:
|
|
base_path: 知识库根目录(必需)
|
|
archive_path: 归档目录(必需)
|
|
git_root: Git 仓库根目录(可选)
|
|
git_user: Git 用户名(可选)
|
|
git_email: Git 邮箱(可选)
|
|
hf_token: HuggingFace Token(敏感信息)
|
|
zhihu_cookie: 知乎 Cookie(敏感信息)
|
|
"""
|
|
|
|
model_config = SettingsConfigDict(
|
|
env_file=str(Path.home() / ".kb_env"),
|
|
env_file_encoding="utf-8",
|
|
env_prefix="KB_",
|
|
extra="ignore",
|
|
)
|
|
|
|
# 必需配置
|
|
base_path: str = Field(..., description="知识库根目录")
|
|
archive_path: str = Field(..., description="归档目录")
|
|
intermediate_path: str = Field(
|
|
default_factory=lambda: str(Path.home() / "work" / "intermediate" / "analysis"),
|
|
description="中间文件存储目录"
|
|
)
|
|
|
|
# 可选配置
|
|
git_root: str = Field(default="", description="Git 仓库根目录")
|
|
git_user: str = Field(default="", description="Git 用户名")
|
|
git_email: str = Field(default="", description="Git 邮箱")
|
|
|
|
# 敏感信息(从环境变量读取)
|
|
hf_token: SecretStr = Field(
|
|
default=SecretStr(""), description="HuggingFace Token"
|
|
)
|
|
zhihu_cookie: SecretStr = Field(
|
|
default=SecretStr(""), description="知乎 Cookie"
|
|
)
|
|
bilibili_cookie: SecretStr = Field(
|
|
default=SecretStr(""), description="B站 Cookie (for video download)"
|
|
)
|
|
|
|
# 工作流行为配置
|
|
overwrite_archives: bool = Field(
|
|
default=False, description="覆盖已存在的归档文件(非交互模式)"
|
|
)
|
|
enable_semantic_segmentation: bool = Field(
|
|
default=True,
|
|
description="启用 LLM 语义分段(默认启用)"
|
|
)
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> KBSettings:
|
|
"""获取配置单例
|
|
|
|
使用 lru_cache 确保配置只加载一次。
|
|
如需重新加载配置,调用 get_settings.cache_clear()。
|
|
|
|
Returns:
|
|
KBSettings 实例
|
|
"""
|
|
return KBSettings()
|