Spaces:
Runtime error
Runtime error
| """命令行入口 - 可直接在终端交互式判定。""" | |
| import json | |
| import sys | |
| from .config import SILICONFLOW_API_KEY, SILICONFLOW_MODEL | |
| from .knowledge_loader import build_chunks | |
| from .retriever import KnowledgeIndex | |
| from .llm_client import judge_event | |
| def main(): | |
| if not SILICONFLOW_API_KEY: | |
| print("错误:请先设置 SILICONFLOW_API_KEY 环境变量,或在 .env 文件中配置。") | |
| sys.exit(1) | |
| print("正在加载周礼知识库并构建向量索引...") | |
| chunks = build_chunks() | |
| index = KnowledgeIndex() | |
| index.build(chunks, SILICONFLOW_API_KEY) | |
| print(f"索引构建完成,共 {len(chunks)} 条知识。\n") | |
| print("=" * 50) | |
| print(" 周礼判定系统(RAG 模式)") | |
| print(" 输入一个事件或行为,系统将判断是否符合周礼") | |
| print(" 输入 q 退出") | |
| print("=" * 50) | |
| while True: | |
| print() | |
| event = input("请输入事件> ").strip() | |
| if event.lower() == "q": | |
| print("再见。") | |
| break | |
| if not event: | |
| continue | |
| print("正在检索相关知识...") | |
| hits = index.search(event, SILICONFLOW_API_KEY) | |
| print(f"命中 {len(hits)} 条知识:") | |
| for h in hits: | |
| print(f" [{h['score']:.4f}] {h['title']}") | |
| knowledge = "\n\n---\n\n".join(h["text"] for h in hits) | |
| print("\n正在判定...\n") | |
| try: | |
| result = judge_event( | |
| event=event, | |
| knowledge=knowledge, | |
| api_key=SILICONFLOW_API_KEY, | |
| model=SILICONFLOW_MODEL, | |
| ) | |
| print(json.dumps(result, ensure_ascii=False, indent=2)) | |
| except Exception as e: | |
| print(f"判定失败:{e}") | |
| if __name__ == "__main__": | |
| main() | |