import json import os from typing import Dict, List import httpx from config import Config def load_templates(dir_path: str) -> List[Dict[str, any]]: items = [] for name in os.listdir(dir_path): if not name.endswith(".json"): continue with open(os.path.join(dir_path, name), "r", encoding="utf-8") as f: items.append(json.load(f)) return items def build_document(item: Dict[str, any]) -> str: table = item.get("table", "") templates = item.get("templates", []) lines = [f"table: {table}"] for t in templates: lines.append(f"- {t}") return "\n".join(lines) def main(): cfg = Config.get_section("ragflow") base_url = cfg.get("url", "").rstrip("/") api_key = cfg.get("api_key", "") dataset_ids = cfg.get("dataset_ids", "") upload_path = cfg.get("upload", "") if not upload_path: raise RuntimeError("未配置 ragflow.upload 上传接口,请在 config/config.ini 中设置") url = base_url + "/" + upload_path.lstrip("/") headers = {"Authorization": f"Bearer {api_key}"} if api_key else {} root = os.path.dirname(os.path.dirname(__file__)) templates_dir = os.path.join(root, "config", "ragflow_templates") items = load_templates(templates_dir) payload = [] for item in items: payload.append( { "dataset_ids": dataset_ids, "content": build_document(item), "metadata": {"table": item.get("table")}, } ) with httpx.Client(timeout=60) as client: response = client.post(url, json={"documents": payload}, headers=headers) response.raise_for_status() print("同步完成") if __name__ == "__main__": main()