#!/usr/bin/env python3 # -*- coding: utf-8 -*- """专题四 · 步骤 3:直连 vLLM 复现多条/空 system 与合并后成功。""" from __future__ import annotations import json import sys import _bootstrap # noqa: F401 import _config as C from dify_api import Dify def curl_chat(d: Dify, messages: list[dict], label: str) -> None: body = json.dumps({"model": C.LLM_MODEL, "messages": messages}) remote = "/tmp/vllm_sys_test.json" with d.sftp.open(remote, "w") as f: f.write(body) cmd = ( "curl -s -X POST %s/v1/chat/completions " "-H 'Content-Type: application/json' " "-H 'Authorization: Bearer %s' " "-d @%s" % (C.VLLM_URL, C.VLLM_KEY, remote) ) out, err = d.sh(cmd, t=120) print("\n=== %s ===" % label) print(out or err) try: j = json.loads(out) err_msg = j.get("error", {}).get("message") or j.get("message") if err_msg: print("[error]", err_msg) except Exception: pass def main() -> int: d = Dify(C.DIFY_EMAIL, C.DIFY_PASSWORD) try: d.login() curl_chat( d, [ {"role": "system", "content": "A"}, {"role": "system", "content": "B"}, {"role": "user", "content": "hi"}, ], "TEST1 两条 system(期望 400)", ) curl_chat( d, [ {"role": "system", "content": "A"}, {"role": "system", "content": ""}, {"role": "system", "content": "C"}, {"role": "user", "content": "hi"}, ], "TEST2 含空 system(期望 400)", ) curl_chat( d, [ {"role": "system", "content": "A\n\nB\n\nC"}, {"role": "user", "content": "hi"}, ], "TEST3 合并单条 system(期望成功)", ) return 0 finally: d.close() if __name__ == "__main__": sys.exit(main())