#!/usr/bin/env python3 """在 New-API 中创建指向本机 vLLM 的 OpenAI 兼容渠道。重复执行会再新增一条渠道。""" import json import urllib.request BASE = "http://127.0.0.1:3000" def req(method, path, body=None, headers=None, timeout=120): data = None if body is None else json.dumps(body).encode() request = urllib.request.Request(BASE + path, data=data, method=method) request.add_header("Content-Type", "application/json") for key, value in (headers or {}).items(): request.add_header(key, value) try: with urllib.request.urlopen(request, timeout=timeout) as resp: raw = resp.read().decode() return resp.status, json.loads(raw) if raw else {} except urllib.error.HTTPError as exc: raw = exc.read().decode() try: parsed = json.loads(raw) except json.JSONDecodeError: parsed = {"raw": raw[:500]} return exc.code, parsed _, login = req( "POST", "/api/user/login", {"username": "admin", "password": "L4o4986784$"}, ) auth = { "Authorization": "Bearer " + login["data"]["access_token"], "New-Api-User": "1", } status, created = req( "POST", "/api/channel/", { "mode": "single", "channel": { "name": "vLLM-Qwen3.6-27B", "type": 1, "key": "sk-local-vllm", "base_url": "http://172.17.0.1:8000", "models": "qwen3.6-27b", "groups": ["default"], "group": "default", "priority": 0, "weight": 0, "status": 1, }, }, auth, ) print("create", status, json.dumps(created, ensure_ascii=False)[:400]) status, channels = req("GET", "/api/channel/?p=1&page_size=20", headers=auth) print("list", status, json.dumps(channels, ensure_ascii=False)[:800])