#!/usr/bin/env python3 # -*- coding: utf-8 -*- """专题一 · 步骤 4 · 修改后:patch llm.py 克隆 chat schema,重启 plugin_daemon。""" from __future__ import annotations import sys import _bootstrap # noqa: F401 import _config as C import _plugin_util as PU from dify_api import Dify def main() -> int: d = Dify(C.DIFY_EMAIL, C.DIFY_PASSWORD) try: d.login() paths = PU.find_all_plugin_files(d, "models/llm/llm.py") touched = 0 for path in paths: print("target:", path) content = PU.container_read(d, C.PLUGIN_CONTAINER, path) if PU.LLM_PATCH_MARKER in content: print(" already patched") continue d.sh( "docker exec %s cp %s %s.bak_qwenfix" % (C.PLUGIN_CONTAINER, path, path) ) new_content = PU.patch_llm_schema(content) PU.container_write(d, C.PLUGIN_CONTAINER, path, new_content) out, err = d.sh( "docker exec %s python3 -m py_compile %s 2>/dev/null || " "docker exec %s sh -c 'command -v python3 >/dev/null && python3 -m py_compile %s || true'" % (C.PLUGIN_CONTAINER, path, C.PLUGIN_CONTAINER, path) ) print(" py_compile:", out or err or "ok") touched += 1 if touched: print("restarting plugin_daemon ...") PU.restart_plugin_daemon(d) else: print("all versions already patched — 无需重启") print("done — 回控制台确认 qwen 不再「不兼容」") return 0 finally: d.close() if __name__ == "__main__": sys.exit(main())