From 917ff1c357ef85dce375f6d34ef7ad713fb59759 Mon Sep 17 00:00:00 2001 From: ViperEkura <3081035982@qq.com> Date: Sun, 14 Jun 2026 20:51:00 +0800 Subject: [PATCH] refactor server.py to use vLLM Python API and add permission config server.py: switch from os.execvp(vllm) to make_arg_parser + asyncio.run(run_server(args)) for better error handling; add GPU selection via CLI argument opencode.json: add default model field and permission.question=ask to prevent auto-triggering question tool --- opencode.json | 4 ++++ server.py | 30 ++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/opencode.json b/opencode.json index 9f04ff5..7419506 100644 --- a/opencode.json +++ b/opencode.json @@ -1,5 +1,6 @@ { "$schema": "https://opencode.ai/config.json", + "model": "Agent/Qwen2.5-7B-Instruct", "provider": { "Agent": { "api": "openai", @@ -17,6 +18,9 @@ } } }, + "permission": { + "question": "ask" + }, "experimental": { "mcp_timeout": 60000 } diff --git a/server.py b/server.py index c745f1f..da2903d 100644 --- a/server.py +++ b/server.py @@ -1,10 +1,18 @@ -import os, subprocess +import os, sys +import asyncio +import subprocess from pathlib import Path +from vllm.utils.argparse_utils import FlexibleArgumentParser +from vllm.entrypoints.openai.api_server import run_server +from vllm.entrypoints.openai.cli_args import make_arg_parser + + def _pick_free_gpu(): if "CUDA_VISIBLE_DEVICES" in os.environ: return os.environ["CUDA_VISIBLE_DEVICES"] try: + out = subprocess.check_output( ["nvidia-smi", "--query-gpu=index,memory.free", "--format=csv,noheader"], text=True, @@ -14,19 +22,23 @@ def _pick_free_gpu(): gpu = best.split(",")[0].strip() except Exception: gpu = "0" - os.environ["CUDA_VISIBLE_DEVICES"] = gpu + return gpu + if __name__ == "__main__": - _pick_free_gpu() + gpu = _pick_free_gpu() + if len(sys.argv) > 1: + gpu = sys.argv[1] + + os.environ["CUDA_VISIBLE_DEVICES"] = gpu + os.environ["FLASHINFER_DISABLE_VERSION_CHECK"] = "1" model = str(Path(__file__).resolve().parent / "models/Qwen2.5-7B-Instruct") - os.environ["FLASHINFER_DISABLE_VERSION_CHECK"] = "1" - - os.execvp("vllm", [ - "vllm", "serve", - model, + parser = make_arg_parser(FlexibleArgumentParser()) + args = parser.parse_args([ + "--model", model, "--host", "0.0.0.0", "--port", "8000", "--served-model-name", "Qwen2.5-7B-Instruct", @@ -38,3 +50,5 @@ if __name__ == "__main__": "--enable-auto-tool-choice", "--tool-call-parser", "hermes", ]) + + asyncio.run(run_server(args))