93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
"""
|
|
Unified entry point.
|
|
|
|
Re-exports from sub-modules for backward compatibility.
|
|
"""
|
|
|
|
import logging
|
|
|
|
from prompt import build_analysis_prompt, language_rule
|
|
from llm import query_analysis
|
|
|
|
__all__ = ["language_rule", "build_analysis_prompt", "query_analysis"]
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _setup_logging() -> None:
|
|
"""配置日志格式和级别,同时输出到终端和文件。"""
|
|
from pathlib import Path
|
|
|
|
log_path = Path(__file__).parent.parent / "llmwiki.log"
|
|
|
|
formatter = logging.Formatter(
|
|
"%(asctime)s [%(levelname)s] %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
)
|
|
|
|
file_handler = logging.FileHandler(log_path, encoding="utf-8")
|
|
file_handler.setFormatter(formatter)
|
|
|
|
stream_handler = logging.StreamHandler()
|
|
stream_handler.setFormatter(formatter)
|
|
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s [%(levelname)s] %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
handlers=[file_handler, stream_handler],
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
"""执行入口:遍历 raw 目录,对每个源文件进行分析。"""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
_setup_logging()
|
|
|
|
from config import load_config
|
|
|
|
config = load_config()
|
|
raw_dir = Path(config.get("raw_dir", "raw"))
|
|
wiki_dir = Path(config.get("wiki_dir", "wiki"))
|
|
|
|
# 读取 wiki 上下文
|
|
purpose_path = wiki_dir / "purpose.md"
|
|
index_path = wiki_dir / "index.md"
|
|
|
|
purpose = purpose_path.read_text(encoding="utf-8") if purpose_path.exists() else ""
|
|
index = index_path.read_text(encoding="utf-8") if index_path.exists() else ""
|
|
|
|
logger.info("purpose.md: %s", "已加载" if purpose else "未找到")
|
|
logger.info("index.md: %s", "已加载" if index else "未找到")
|
|
|
|
# 收集 raw 目录下的 .md 文件
|
|
md_files = sorted(raw_dir.rglob("*.md"))
|
|
if not md_files:
|
|
logger.info("未找到 %s 下的 .md 文件", raw_dir)
|
|
sys.exit(0)
|
|
|
|
logger.info("共发现 %d 个源文件", len(md_files))
|
|
|
|
for fp in md_files:
|
|
source_content = fp.read_text(encoding="utf-8")
|
|
logger.info("=" * 60)
|
|
logger.info("分析: %s", fp.relative_to(raw_dir.parent))
|
|
logger.info("=" * 60)
|
|
|
|
try:
|
|
result = query_analysis(
|
|
purpose=purpose,
|
|
index=index,
|
|
source_content=source_content,
|
|
)
|
|
logger.info("分析完成,结果长度: %d 字符", len(result))
|
|
logger.info("结果: %s", result)
|
|
except Exception as e:
|
|
logger.error("分析失败: %s", e, exc_info=True)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |