67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
"""
|
|
使用 Pydantic Settings 的应用程序配置。
|
|
"""
|
|
from typing import Optional
|
|
from pydantic_settings import BaseSettings
|
|
from pydantic import Field
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""
|
|
从环境变量加载的应用程序设置。
|
|
"""
|
|
|
|
# 应用程序
|
|
app_name: str = Field(default="LangChain Learning Kit", description="Application name")
|
|
environment: str = Field(default="development", description="Environment (development/production)")
|
|
debug: bool = Field(default=False, description="Debug mode")
|
|
log_level: str = Field(default="INFO", description="Logging level")
|
|
|
|
# 服务器
|
|
host: str = Field(default="0.0.0.0", description="Server host")
|
|
port: int = Field(default=8000, description="Server port")
|
|
|
|
# 数据库
|
|
database_url: str = Field(
|
|
...,
|
|
description="Database connection URL",
|
|
examples=["mysql+pymysql://root:123456@localhost:3306/langchain_learning"]
|
|
)
|
|
|
|
# OpenAI
|
|
openai_api_key: str = Field(..., description="OpenAI API key")
|
|
openai_base_url: Optional[str] = Field(
|
|
default=None,
|
|
description="OpenAI API base URL (for proxy or custom endpoints)"
|
|
)
|
|
|
|
# FAISS 存储
|
|
faiss_base_path: str = Field(default="data/faiss", description="Base path for FAISS indexes")
|
|
|
|
# 文本分割
|
|
chunk_size: int = Field(default=1000, description="Default chunk size for text splitting")
|
|
chunk_overlap: int = Field(default=200, description="Default chunk overlap")
|
|
|
|
model_config = {
|
|
"env_file": ".env",
|
|
"env_file_encoding": "utf-8",
|
|
"case_sensitive": False,
|
|
}
|
|
|
|
|
|
# 全局设置实例
|
|
_settings: Optional[Settings] = None
|
|
|
|
|
|
def get_settings() -> Settings:
|
|
"""
|
|
获取或创建全局设置实例。
|
|
|
|
Returns:
|
|
Settings: 应用程序设置
|
|
"""
|
|
global _settings
|
|
if _settings is None:
|
|
_settings = Settings()
|
|
return _settings
|