fix: remove api_key from config.yaml, read from EMBED_API_KEY env var; unify config path constant
This commit is contained in:
+2
-2
@@ -13,7 +13,7 @@ if sys.stdout.encoding != "utf-8":
|
||||
# 确保 src 在路径中
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from src.core.config import load_config
|
||||
from src.core.config import load_config, DEFAULT_CONFIG_PATH
|
||||
from src.core.db import VectorDB
|
||||
from src.core.embedder import create_embedder
|
||||
from src.core.ingest import DocumentIngestor
|
||||
@@ -23,7 +23,7 @@ from src.core.search import Searcher
|
||||
app = typer.Typer(name="md-vector-db", help="Markdown 文档向量数据库管理工具")
|
||||
|
||||
|
||||
def _get_components(config_path: str = "config.yaml"):
|
||||
def _get_components(config_path: str = DEFAULT_CONFIG_PATH):
|
||||
"""初始化所有组件."""
|
||||
cfg = load_config(config_path)
|
||||
db = VectorDB(persist_dir=cfg.chroma.persist_dir)
|
||||
|
||||
+8
-2
@@ -1,11 +1,15 @@
|
||||
"""应用配置加载模块."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
# 默认配置文件路径
|
||||
DEFAULT_CONFIG_PATH = "config.yaml"
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChromaConfig:
|
||||
@@ -22,7 +26,9 @@ class EmbedConfig:
|
||||
mode: str = "local" # "local" | "api"
|
||||
local_model: str = "BAAI/bge-small-zh-v1.5"
|
||||
api_base: str = ""
|
||||
api_key: str = ""
|
||||
api_key: str = field(
|
||||
default_factory=lambda: os.environ.get("EMBED_API_KEY", "")
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -59,7 +65,7 @@ class AppConfig:
|
||||
|
||||
def load_config(path: str | None = None) -> AppConfig:
|
||||
"""从 YAML 文件加载配置, 若文件不存在则返回默认配置."""
|
||||
config_path = path or "config.yaml"
|
||||
config_path = path or DEFAULT_CONFIG_PATH
|
||||
if not Path(config_path).exists():
|
||||
return AppConfig()
|
||||
|
||||
|
||||
+4
-4
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.core.config import load_config, EmbedConfig
|
||||
from src.core.config import load_config, EmbedConfig, DEFAULT_CONFIG_PATH
|
||||
from src.core.db import VectorDB
|
||||
from src.core.embedder import create_embedder
|
||||
from src.core.ingest import DocumentIngestor
|
||||
@@ -42,9 +42,9 @@ def _get_db():
|
||||
def _get_embedder():
|
||||
global _embedder
|
||||
if _embedder is None:
|
||||
# 先尝试从 config.yaml 加载, 失败则用默认 local
|
||||
# 先尝试从配置文件加载, 失败则用默认 local
|
||||
try:
|
||||
cfg = load_config("config.yaml")
|
||||
cfg = load_config(DEFAULT_CONFIG_PATH)
|
||||
embed_cfg = cfg.embed
|
||||
except Exception:
|
||||
embed_cfg = EmbedConfig(mode="local")
|
||||
@@ -56,7 +56,7 @@ def _init_services():
|
||||
global _searcher, _ingestor
|
||||
collection = os.getenv("MD_VECTOR_DB_COLLECTION", "markdown_docs")
|
||||
try:
|
||||
cfg = load_config("config.yaml")
|
||||
cfg = load_config(DEFAULT_CONFIG_PATH)
|
||||
collection = cfg.chroma.collection_name
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user